Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <pthread.h>
  7. #include </usr/src/linux/include/linux/memstats.h>
  8.  
  9. #define PAGESIZE 4096
  10.  
  11. int main()
  12. {
  13. int status, i;
  14. int nr_pages = 100000;
  15. char **page_array;
  16. struct memstats stats;
  17.  
  18. printf("\n'test.c'\n\n");
  19.  
  20. status = syscall (328, &stats);
  21.  
  22. printf( "stats:\n" );
  23. printf(
  24. " Number of free pages = %lu\n"
  25. " Number of pages used by slab allocator = %lu\n"
  26. " Number of pages in active list = %lu\n"
  27. " Number of pages in inactive list = %lu\n"
  28. " Number of pages in active list with set bit = %lu\n"
  29. " Number of pages in inactive list with set bit = %lu\n"
  30. " Number of pages moved from active to inactive list = %lu\n"
  31. " Number of pages evicted from inactive list = %lu\n",
  32. stats.u1, stats.u2, stats.u3, stats.u4, stats.u5, stats.u6, stats.u7,
  33. stats.u8);
  34.  
  35.  
  36. page_array = (char **)malloc(nr_pages * sizeof(char *));
  37.  
  38. if ( page_array == NULL )
  39. {
  40. printf("ERROR ALLOCATING MEMORY TO FILL PAGES!\n");
  41. return -1;
  42. }
  43.  
  44.  
  45. for( i = 0; i < nr_pages; i++)
  46. {
  47. page_array[i] = (char *)malloc( sizeof(char) * PAGESIZE );
  48.  
  49. if ( page_array[i] == NULL )
  50. {
  51. printf("ERROR ALLOCATING MEMORY TO PAGE %d!\n", i);
  52. return -1;
  53. }
  54. }
  55.  
  56. printf("\nMemory has been allocated for %d pages\n\n", nr_pages);
  57.  
  58. sleep(2);
  59.  
  60. status = syscall (328, &stats);
  61.  
  62. printf( "stats:\n" );
  63. printf(
  64. " Number of free pages = %lu\n"
  65. " Number of pages used by slab allocator = %lu\n"
  66. " Number of pages in active list = %lu\n"
  67. " Number of pages in inactive list = %lu\n"
  68. " Number of pages in active list with set bit = %lu\n"
  69. " Number of pages in inactive list with set bit = %lu\n"
  70. " Number of pages moved from active to inactive list = %lu\n"
  71. " Number of pages evicted from inactive list = %lu\n",
  72. stats.u1, stats.u2, stats.u3, stats.u4, stats.u5, stats.u6, stats.u7,
  73. stats.u8);
  74.  
  75. printf("\nFreeing allocated memory...\n\n", nr_pages);
  76.  
  77. for( i = 0; i < nr_pages; i++)
  78. {
  79. free( page_array[i] );
  80. }
  81.  
  82. free( page_array );
  83.  
  84. sleep(2);
  85.  
  86. printf("Stats after freeing %d pages:\n\n", nr_pages);
  87.  
  88. status = syscall (328, &stats);
  89.  
  90. printf( "stats:\n" );
  91. printf(
  92. " Number of free pages = %lu\n"
  93. " Number of pages used by slab allocator = %lu\n"
  94. " Number of pages in active list = %lu\n"
  95. " Number of pages in inactive list = %lu\n"
  96. " Number of pages in active list with set bit = %lu\n"
  97. " Number of pages in inactive list with set bit = %lu\n"
  98. " Number of pages moved from active to inactive list = %lu\n"
  99. " Number of pages evicted from inactive list = %lu\n",
  100. stats.u1, stats.u2, stats.u3, stats.u4, stats.u5, stats.u6, stats.u7,
  101. stats.u8);
  102.  
  103.  
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement