procps-pmap-smaps.patch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. diff -up procps-3.2.8/pmap.c.pmap procps-3.2.8/pmap.c
  2. --- procps-3.2.8/pmap.c.pmap 2008-10-19 21:34:34.000000000 +0200
  3. +++ procps-3.2.8/pmap.c 2010-02-08 15:40:19.000000000 +0100
  4. @@ -126,24 +126,38 @@ static int one_proc(proc_t *p){
  5. char buf[32];
  6. char mapbuf[9600];
  7. char cmdbuf[512];
  8. + FILE *fp;
  9. unsigned long total_shared = 0ul;
  10. unsigned long total_private_readonly = 0ul;
  11. unsigned long total_private_writeable = 0ul;
  12. + char *cp2=NULL;
  13. + unsigned long long rss = 0ull;
  14. + unsigned long long private_dirty = 0ull;
  15. + unsigned long long shared_dirty = 0ull;
  16. + unsigned long long total_rss = 0ull;
  17. + unsigned long long total_private_dirty = 0ull;
  18. + unsigned long long total_shared_dirty = 0ull;
  19. + unsigned KLONG diff = 0;
  20. +
  21. // Overkill, but who knows what is proper? The "w" prog
  22. // uses the tty width to determine this.
  23. int maxcmd = 0xfffff;
  24. sprintf(buf,"/proc/%u/maps",p->tgid);
  25. - if(!freopen(buf, "r", stdin)) return 1;
  26. + if ( (fp = fopen(buf, "r")) == NULL) return 1;
  27. + if (x_option) {
  28. + sprintf(buf,"/proc/%u/smaps",p->tgid);
  29. + if ( (fp = freopen(buf, "r", fp)) == NULL) return 1;
  30. + }
  31. escape_command(cmdbuf, p, sizeof cmdbuf, &maxcmd, ESC_ARGS|ESC_BRACKETS);
  32. printf("%u: %s\n", p->tgid, cmdbuf);
  33. if(!q_option && (x_option|d_option)){
  34. if(x_option){
  35. - if(sizeof(KLONG)==4) printf("Address Kbytes RSS Anon Locked Mode Mapping\n");
  36. - else printf("Address Kbytes RSS Anon Locked Mode Mapping\n");
  37. + if(sizeof(KLONG)==4) printf("Address Kbytes RSS Dirty Mode Mapping\n");
  38. + else printf("Address Kbytes RSS Dirty Mode Mapping\n");
  39. }
  40. if(d_option){
  41. if(sizeof(KLONG)==4) printf("Address Kbytes Mode Offset Device Mapping\n");
  42. @@ -151,12 +165,54 @@ static int one_proc(proc_t *p){
  43. }
  44. }
  45. - while(fgets(mapbuf,sizeof mapbuf,stdin)){
  46. + while(fgets(mapbuf,sizeof mapbuf,fp)){
  47. char flags[32];
  48. char *tmp; // to clean up unprintables
  49. - unsigned KLONG start, end, diff;
  50. + unsigned KLONG start, end;
  51. unsigned long long file_offset, inode;
  52. unsigned dev_major, dev_minor;
  53. + unsigned long long smap_value;
  54. + char smap_key[20];
  55. +
  56. + /* hex values are lower case or numeric, keys are upper */
  57. + if (mapbuf[0] >= 'A' && mapbuf[0] <= 'Z') {
  58. + /* Its a key */
  59. + if (sscanf(mapbuf,"%20[^:]: %llu", smap_key, &smap_value) == 2) {
  60. + if (strncmp("Rss", smap_key, 3) == 0) {
  61. + rss = smap_value;
  62. + total_rss += smap_value;
  63. + continue;
  64. + }
  65. + if (strncmp("Shared_Dirty", smap_key, 12) == 0) {
  66. + shared_dirty = smap_value;
  67. + total_shared_dirty += smap_value;
  68. + continue;
  69. + }
  70. + if (strncmp("Private_Dirty", smap_key, 13) == 0) {
  71. + private_dirty = smap_value;
  72. + total_private_dirty += smap_value;
  73. + continue;
  74. + }
  75. + if (strncmp("Swap", smap_key, 4) == 0) { /*doesnt matter as long as last*/
  76. + printf(
  77. + (sizeof(KLONG)==8)
  78. + ? "%016"KLF"x %7lu %7llu %7llu %s %s\n"
  79. + : "%08lx %7lu %7llu %7llu %s %s\n",
  80. + start,
  81. + (unsigned long)(diff>>10),
  82. + rss,
  83. + (private_dirty + shared_dirty),
  84. + flags,
  85. + cp2
  86. + );
  87. + /* reset some counters */
  88. + rss = shared_dirty = private_dirty = 0ull;
  89. + continue;
  90. + }
  91. + /* Other keys */
  92. + continue;
  93. + }
  94. + }
  95. sscanf(mapbuf,"%"KLF"x-%"KLF"x %31s %Lx %x:%x %Lu", &start, &end, flags, &file_offset, &dev_major, &dev_minor, &inode);
  96. if(start > range_high)
  97. @@ -186,16 +242,9 @@ static int one_proc(proc_t *p){
  98. flags[5] = '\0';
  99. if(x_option){
  100. - const char *cp = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode);
  101. - printf(
  102. - (sizeof(KLONG)==8)
  103. - ? "%016"KLF"x %7lu - - - %s %s\n"
  104. - : "%08lx %7lu - - - %s %s\n",
  105. - start,
  106. - (unsigned long)(diff>>10),
  107. - flags,
  108. - cp
  109. - );
  110. + cp2 = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode);
  111. + /* printed with the keys */
  112. + continue;
  113. }
  114. if(d_option){
  115. const char *cp = mapping_name(p, start, diff, mapbuf, 0, dev_major, dev_minor, inode);
  116. @@ -232,10 +281,12 @@ static int one_proc(proc_t *p){
  117. if(!q_option){
  118. if(x_option){
  119. if(sizeof(KLONG)==8){
  120. - printf("---------------- ------ ------ ------ ------\n");
  121. + printf("---------------- ------ ------ ------\n");
  122. printf(
  123. - "total kB %15ld - - -\n",
  124. - (total_shared + total_private_writeable + total_private_readonly) >> 10
  125. + "total kB %15ld %7llu %7llu\n",
  126. + (total_shared + total_private_writeable + total_private_readonly) >> 10,
  127. + total_rss, (total_shared_dirty+total_private_dirty)
  128. +
  129. );
  130. }else{
  131. printf("-------- ------- ------- ------- -------\n");