Go’LegOS Alternative Memory Management Schemes for LegOS Project Hao Ji Yanhao Zhu Gowri Mereddy.

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Understanding Operating Systems Fifth Edition
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Memory Management Chapter 7.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 C Review and Dissection V: Dynamic Memory Allocation and Linked Lists These lecture notes created by Dr. Alex Dean, NCSU.
1 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Fixed/Variable Partitioning
Memory Management Chapter 7. Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated efficiently to pack as.
Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable supply of ready processes to.
Allocating Memory.
CPSC 388 – Compiler Design and Construction
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
Memory Management Professor Yihjia Tsai Tamkang University.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Management Chapter 4. Memory hierarchy Programmers want a lot of fast, non- volatile memory But, here is what we have:
Memory Management Memory Areas and their use Memory Manager Tasks:
CSCI2413 Lecture 5 Operating Systems Memory Management 1 phones off (please)
Memory Management Chapter 7 B.Ramamurthy. Memory Management Subdividing memory to accommodate multiple processes Memory needs to allocated efficiently.
1 Optimizing Malloc and Free Professor Jennifer Rexford
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
CS61C L06 C Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Memory Management Chapter 5.
CS61C L06 C Memory Management (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
Thrashing and Memory Management
Memory Management Five Requirements for Memory Management to satisfy: –Relocation Users generally don’t know where they will be placed in main memory May.
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
1 Lecture 8: Memory Mangement Operating System I Spring 2008.
Memory Allocation CS Introduction to Operating Systems.
Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is a heap? What are best-fit, first-fit, worst-fit, and.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 32 Paging Read Ch. 9.4.
Memory Management Chapter 7.
Heap Management. What is really stored on the heap? Housekeeping Users Data Buffer Next Block Data Housekeeping 0x7000 0x7008 int main() { int *x,*y;
Rensselaer Polytechnic Institute CSC 432 – Operating Systems David Goldschmidt, Ph.D.
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
1 Address Translation Memory Allocation –Linked lists –Bit maps Options for managing memory –Base and Bound –Segmentation –Paging Paged page tables Inverted.
PA0 due 60 hours. Lecture 4 Memory Management OSTEP Virtualization CPU: illusion of private CPU RAM: illusion of private memory Concurrency Persistence.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
Memory Management Problem: Records (of various lengths) need to be stored. Model: A big array of space to store them, managed by a memory manager. Like.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Basic Memory Management 1. Readings r Silbershatz et al: chapters
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
CS6502 Operating Systems - Dr. J. Garrido Memory Management – Part 1 Class Will Start Momentarily… Lecture 8b CS6502 Operating Systems Dr. Jose M. Garrido.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
Memory Management Chapter 5 Advanced Operating System.
External fragmentation in a paging system Use paging circuitry to map groups of noncontiguous free pages into logically contiguous addresses (remap your.
MEMORY MANAGEMENT. memory management  In a multiprogramming system, in order to share the processor, a number of processes must be kept in memory. 
Chapter 17 Free-Space Management
Memory Management Memory Areas and their use Memory Manager Tasks:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
C Programming Language Review and Dissection IV
Operating Systems (CS 340 D)
Memory Management Memory Areas and their use Memory Manager Tasks:
Chapter 8: Main Memory.
Optimizing Malloc and Free
CS Introduction to Operating Systems
Memory Allocation CS 217.
Lecture 3: Main Memory.
Management From the memory view, we can list four important tasks that the OS is responsible for ; To know the used and unused memory partitions To allocate.
Chapter 10-1: Dynamic Memory Allocation
Memory Management Memory Areas and their use Memory Manager Tasks:
Presentation transcript:

Go’LegOS Alternative Memory Management Schemes for LegOS Project Hao Ji Yanhao Zhu Gowri Mereddy

Problem Statement  To introduces a new system call mm_algorithm_used() to allow the user to select the type of memory management required depending up to the type of user programs in order to have efficient memory management.

The Basic memory block Process ID Size Data Field The unit of the memory is 2 byte word 4 bytes Head Consisting of Process ID and Size Size indicating the length of Data Field &mm_start Free 0xEf50 LCD DATA 0xef50 FREE 0xF000 Motor 0xF010 Free 0xFB80 Vectors 0xFE00 Free 0xFF00 Onchip Register

Current Mechanism  Straightforward continuous allocation scheme  No advanced memory schemes (which imply more overheads)  The kernel code and the kernel data starts at the location 0x8000 up to the global variable mm_start ( User memory space)  The user memory starts from mm_start to 0xFFFF.

 The Technique that is currently implemented is First fit. (i.e the first enough free block for The alternative schemes to be implemented is the process)  The function mm_try_join takes the address of a free memory blocks as the parameters and merges with the following free memory blocks.  The merging requires locking of the mm_semophore to stop the other process requesting memory space in multitasking mode

Techniques  BEST FIT: The Memory is searched from the mm_start till the end to find a free memory which is of the nearest size to the required size. This type of technique leads to minimize external fragmention,but may lead to lot of internal fragmentations.  WORST FIT: The memory is searched to find the biggest whole available and the split into two parts so that the other process can use the space. May be a solution to internal fragmentation. The memory is searched to find the biggest whole available and the split into two parts so that the other process can use the space. May be a solution to internal fragmentation.

 NEXT FIT: The alteration of the First fit is the next fit. Never it finds the hole it stores the location, and when requested for allocation starts allocating from that position

 Files modified:  stdlib.h –declare function mm_algorithm_used as an extern function. –declare function mm_scan as extern function  mm.c – Added #define FIRST_FIT 0, #define NEXT_FIT 1, #define BEST_FIT 2, #define WORST_FIT 3 #define BEST_FIT 2, #define WORST_FIT 3 – Added kernel variable mm_policy, which remember which algorithm should be used. During kernel startup, it is initialized to FIRST_FIT. – Added kernel variable mm_next_free, which always points to the next free block during the search. It is initialized to &mm_start at the very beginning. –Add function mm_algorithm_used(int a) as following:

 void mm_algorithm_used(unsigned short policy){  if(policy WORST_FIT) return ;  #ifdef CONF_TM  sem_wait(&mm_semaphore);  #endif  /*  if current policy is not NEXT_FIT and we want to change it to NEXT_FIT,  we need to change the pointer mm_next_free to point to mm_first_free  */  if (policy == NEXT_FIT )  if (mm_policy !=NEXT_FIT) mm_next_free = mm_first_free;  mm_policy = policy;  #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif   }

-Modified malloc function  void *malloc(size_t size) { if (mm_policy == FIRST_FIT) return malloc_first_fit(size); If (mm_policy == NEXT_FIT) return malloc_next_fit(size); if (mm_policy == BEST_FIT) return malloc_best_fit(size); if (mm_policy == WORST_FIT) return malloc_worst_fit(size); } malloc_best_fit() malloc_worst_fit() malloc_first_fit() malloc_next_fit() malloc()

 Add function malloc_next_fit() as following  This algorithm search the free memory block from mm_next_free, which is the pointer pointing to the next free block. If it found one suitable free block, it will put the new block there. If the free block size is bigger than request size plus MM_SPLIT_THRESH (MM_HEADER_SIZE+8), the block will be split. If memory block allocation is succeed, the pointer, mm_next_free, is updated to the addres of next free block if there is one. Otherwise, mm_next_free is reset to &mm_start

 void *malloc_next_fit(size_t size) {  size_t *ptr,*next;   size=(size+1)>>1;// only multiples of 2   #ifdef CONF_TM  sem_wait(&mm_semaphore);// tasksafe  #endif  ptr=mm_next_free;  next = mm_next_free;   while(ptr>=&mm_start) {  if(*(ptr++)==MM_FREE) {// free block?  #ifdef CONF_TM  mm_try_join(ptr);// unite with later blocks  #endif  if(*ptr>=size) {// big enough?   *(ptr-1)=(size_t)cpid; // set owner  // split this block?  if((*ptr-size)>=MM_SPLIT_THRESH) {  next=ptr+size+1;  *(next++)=MM_FREE;  *(next)=*ptr-size-MM_HEADER_SIZE;  mm_try_join(next);   *ptr=size;

 next--;   }else  /* move mm_next_free pointer to next available free block*/  {  next = ptr+MM_HEADER_SIZE+*(ptr)-1;  }  // was it the first free one?  if(ptr==mm_first_free+1)  mm_update_first_free(ptr+*ptr+1);   while(*(next)!=MM_FREE && next >= ptr)  next = next+MM_HEADER_SIZE+*(next+1);  if (next < ptr) next = mm_first_free;  mm_next_free = next;   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return (void*) (ptr+1);  }   ptr+=(*ptr)+1;// find next block.

 /* in case we didn't find a usable block between mm_next_free and 0xFFFF,  we need to go back from the pointer of mm_first_free to search from the beginning */  ptr = mm_first_free ;  while(ptr >= mm_first_free && ptr = mm_first_free && ptr <mm_next_free ){  if(*(ptr++)==MM_FREE) {// free block?  #ifdef CONF_TM  mm_try_join(ptr);// unite with later blocks  #endif  if(*ptr>=size) {// big enough?  *(ptr-1)=(size_t)cpid;// set owner   // split this block?  if((*ptr-size)>=MM_SPLIT_THRESH) {  next=ptr+size+1;  *(next++)=MM_FREE;  *(next)=*ptr-size-MM_HEADER_SIZE;  mm_try_join(next);   *ptr=size;  }else  /* move mm_next_free pointer to next available free block*/  {   next = ptr+MM_HEADER_SIZE+*(ptr)-1;

 // was it the first free one?  if(ptr==mm_first_free+1)   mm_update_first_free(ptr+*ptr+1);   while(*(next)!=MM_FREE && next >= ptr)  next = next+MM_HEADER_SIZE+*(next+1);  if (next < ptr) next = mm_first_free;  mm_next_free = next;   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return (void*) (ptr+1);  }   ptr+=(*ptr)+1;// find next block.   }   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return NULL;  }  //find

 Add function malloc_best_fit() as following:  //! allocate a block of memory using Best _fit POLICY  /*! \param size requested block size  \return 0 on error, else pointer to block.  */  void *malloc_best_fit(size_t size) {  size_t *ptr,*next,*bestptr;  size_t bestsize;   size=(size+1)>>1;// only multiples of 2   #ifdef CONF_TM  sem_wait(&mm_semaphore);// tasksafe  #endif  ptr=mm_first_free;  bestptr=ptr;  bestsize=0xFFFF;   while(ptr>=&mm_start) {  if(*(ptr++)==MM_FREE) {// free block?  #ifdef CONF_TM

 mm_try_join(ptr);// unite with later blocks  #endif  if(*ptr>=size) {// big enough?  if(*ptr<bestsize){  bestsize=*ptr;  bestptr=ptr-1;  }    ptr+=(*ptr)+1;// find next block.  }  if (bestsize != 0xFFFF) {  ptr = bestptr +1;   *(ptr-1)=(size_t)cpid;// set owner   // split this block?  if((*ptr-size)>=MM_SPLIT_THRESH) {  next=ptr+size+1;  *(next++)=MM_FREE;  *(next)=*ptr-size-MM_HEADER_SIZE;  mm_try_join(next);   *ptr=size;  }   // was it the first free one?

 if(ptr==mm_first_free+1)  mm_update_first_free(ptr+*ptr+1);   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return (void*) (bestptr);  }   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return NULL ;

 Add function malloc_worst_fit() as following:  //! allocate a block of memory using Worst _fit POLICY  /*! \param size requested block size  \return 0 on error, else pointer to block.  */  void *malloc_worst_fit(size_t size) {  size_t *ptr,*next,*worstptr;  size_t worstsize;   size=(size+1)>>1;// only multiples of 2   #ifdef CONF_TM  sem_wait(&mm_semaphore);// tasksafe  #endif  ptr=mm_first_free;  worstptr=ptr;  worstsize=0;   while(ptr>=&mm_start) {  if(*(ptr++)==MM_FREE) {// free block?

 while(ptr>=&mm_start) {  if(*(ptr++)==MM_FREE) {// free block?  #ifdef CONF_TM  mm_try_join(ptr);// unite with later blocks  #endif  if(*ptr>=size) {// big enough?  if(*ptr>worstsize){  worstsize=*ptr;  worstptr=ptr-1;  }    ptr+=(*ptr)+1;// find next block.  }  if (worstsize != 0) {  ptr = worstptr +1;   *(ptr-1)=(size_t)cpid;// set owner   // split this block?  if((*ptr-size)>=MM_SPLIT_THRESH) {  next=ptr+size+1;  *(next++)=MM_FREE;  *(next)=*ptr-size-MM_HEADER_SIZE;  mm_try_join(next);   *ptr=size;  } 

 if(ptr==mm_first_free+1)  mm_update_first_free(ptr+*ptr+1);   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return (void*) (worstptr);  }   #ifdef CONF_TM  sem_post(&mm_semaphore);  #endif  return NULL;  }

Testing  (1). First Fit  Testing Program:  mm_algorithm_used(FIRST_FIT);  cputs("malloc0");  mm_scan();  sleep(1);  n=(char*)malloc(sizeof(char)*15388); //15388 bytes = 0x1E0E words  m= (char*)malloc(sizeof(char)*160); //160 bytes = 0x50 words  o= (char*)malloc(sizeof(char)*16); //16 bytes = 0x08 words  mm_scan();  free(n);  free(m);  free(o);  cputs("free0");  sleep(1);  mm_scan();

Before malloc();

After malloc();

 (2). Next Fit  Testing program:  mm_algorithm_used(NEXT_FIT);  mm_scan();  // sleep(1);  n=(char*)malloc(sizeof(char)*15394); //15394 bytes = 0x1E11 words  m= (char*)malloc(sizeof(char)*160);//160 bytes = 0x50 words//  o= (char*)malloc(sizeof(char)*16); //16 bytes = 0x08 words  mm_scan();  free(n);  free(m);  free(o);  cputs("free1");  sleep(1);  mm_scan();

Before malloc();

After malloc();

 (3) Best_FIT  mm_algorithm_used(BEST_FIT);  cputs("malloc2");  mm_scan();  sleep(1);  n=(char*)malloc(sizeof(char)*170); //170 bytes = 0x0055 words  m= (char*)malloc(sizeof(char)*8); //8 bytes = 0x0004 words  mm_scan();  free(n);  cputs("free2");  sleep(1);  mm_scan();

Before malloc();

After malloc();

 (4)worst_fit  mm_algorithm_used(3);  cputs("malloc3");  mm_scan();  sleep(1);  n=(char*)malloc(sizeof(char)*12878); //12878 bytes = 0x1927 words  m= (char*)malloc(sizeof(char)*1024); //1024 bytes = 0x0200 words  mm_scan();  free(n);  cputs("free3");  sleep(1);  mm_scan();

Before malloc();

After malloc();

Test Conclusion  We keep the same interface to programmer so that they don’t need to modify any of their source codes.(Seamless hacking)  The new memory allocation mechanism we developed works fine.  LNP may save a lot of test time and be more straightforward. /* we got crazy when pressing the button for hundreds of times to keep record of our testing result. */

Thank you and Merry Xmas