Advertisement
m4ly

SHM and HugePages

Mar 15th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. /*
  2. Author Dawid Mocek
  3. SHM exmaple code from https://beej.us/guide/bgipc/output/html/multipage/shm.html
  4.  
  5. Testing Hugepages:
  6.  
  7. [oracle@we-pr-002-2 ~]$ ulimit -l
  8. 1048576
  9. [oracle@we-pr-002-2 ~]$ cat /proc/meminfo
  10. MemTotal:        8176208 kB
  11. MemFree:         5985184 kB
  12. Buffers:           13556 kB
  13. Cached:           952644 kB
  14. SwapCached:            0 kB
  15. Active:           806600 kB
  16. Inactive:         212156 kB
  17. Active(anon):      52588 kB
  18. Inactive(anon):      324 kB
  19. Active(file):     754012 kB
  20. Inactive(file):   211832 kB
  21. Unevictable:           0 kB
  22. Mlocked:               0 kB
  23. SwapTotal:       2097148 kB
  24. SwapFree:        2097148 kB
  25. Dirty:               244 kB
  26. Writeback:             0 kB
  27. AnonPages:         52584 kB
  28. Mapped:             9144 kB
  29. Shmem:               336 kB
  30. Slab:              62416 kB
  31. SReclaimable:      17324 kB
  32. SUnreclaim:        45092 kB
  33. KernelStack:         840 kB
  34. PageTables:         3728 kB
  35. NFS_Unstable:          0 kB
  36. Bounce:                0 kB
  37. WritebackTmp:          0 kB
  38. CommitLimit:     5660964 kB
  39. Committed_AS:     112860 kB
  40. VmallocTotal:   34359738367 kB
  41. VmallocUsed:       15700 kB
  42. VmallocChunk:   34359711467 kB
  43. HardwareCorrupted:     0 kB
  44. HugePages_Total:     512
  45. HugePages_Free:      507
  46. HugePages_Rsvd:      251
  47. HugePages_Surp:        0
  48. Hugepagesize:       2048 kB
  49. DirectMap4k:        8192 kB
  50. DirectMap2M:     8380416 kB
  51. [oracle@we-pr-002-2 ~]$ ipcs
  52.  
  53. ------ Shared Memory Segments --------
  54. key        shmid      owner      perms      bytes      nattch     status
  55. 0x52030b0f 131073     oracle     644        536870912  0
  56.  
  57. ------ Semaphore Arrays --------
  58. key        semid      owner      perms      nsems
  59.  
  60. ------ Message Queues --------
  61. key        msqid      owner      perms      used-bytes   messages
  62.  
  63.  
  64.  
  65.  
  66.  
  67. */
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <sys/types.h>
  72. #include <sys/ipc.h>
  73. #include <sys/shm.h>
  74.  
  75. #define SHM_SIZE 536870912   /* make it a 512M shared memory segment */
  76.  
  77.  
  78.  
  79. int main(int argc, char *argv[])
  80. {
  81.     key_t key;
  82.     int shmid;
  83.     char *data;
  84.     int mode;
  85.     char c;
  86.     FILE *fh = NULL;
  87.     long size;
  88.     if (argc > 2) {
  89.         fprintf(stderr, "usage: shmdemo [big_text_file]\n");
  90.         exit(1);
  91.     }
  92.  
  93.     /* make the key: */
  94.     if ((key = ftok("shm.c", 'R')) == -1) {
  95.         perror("ftok");
  96.         exit(1);
  97.     }
  98.  
  99.     printf("Geting shared memory for key....\n");
  100.     /* connect to (and possibly create) the segment: */
  101.     if ((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT | SHM_HUGETLB)) == -1) {
  102.         perror("shmget");
  103.         exit(1);
  104.     }
  105.     printf("... got. Waiting to attach to shared memory\n");
  106.     c = getc(stdin);
  107.     /* attach to the segment to get a pointer to it: */
  108.     data = shmat(shmid, (void *)0, 0);
  109.     if (data == (char *)(-1)) {
  110.         perror("shmat");
  111.         exit(1);
  112.     }
  113.     printf("Attached to shared memory\n");
  114.  
  115.  
  116.     /* read or modify the segment, based on the command line: */
  117.     if (argc == 2) {
  118.         printf("Modifing shared memory...\n");
  119.         printf("Writing to segment file: \"%s\"\n", argv[1]);
  120.         fh = fopen(argv[1], "r+");
  121.         fseek(fh, 0L, SEEK_END);
  122.         size = ftell(fh);
  123.         fseek(fh, 0L, SEEK_SET);
  124.         fread(data, size, sizeof(char), fh);
  125.         fclose(fh);
  126.  
  127. //      strncpy(data, argv[1], SHM_SIZE);
  128.         printf("Shared memory modified\n");
  129.     } else {
  130.         printf("Reading shared memory\n");
  131.         printf("Segment contains: \"%s\"\n", data);
  132.         printf("Done reading shared memory\n");
  133.         }
  134.     printf("Detaching shared memory\n");
  135.     /* detach from the segment: */
  136.     if (shmdt(data) == -1) {
  137.         perror("shmdt");
  138.         exit(1);
  139.     }
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement