fcached.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. // Prints OS cached details for files open by a process
  15. #include "platform.h"
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <dirent.h>
  20. #include <dirent.h>
  21. #include <sys/vfs.h>
  22. #include <sys/mman.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <time.h>
  26. #include "ctfile.hpp"
  27. static char *_itoa(unsigned long n, char *str, int b, bool sign)
  28. {
  29. char *s = str;
  30. if (sign)
  31. n = -n;
  32. do
  33. {
  34. byte d = n % b;
  35. *(s++) = d+((d<10)?'0':('a'-10));
  36. }
  37. while ((n /= b) > 0);
  38. if (sign)
  39. *(s++) = '-';
  40. *s = '\0';
  41. // reverse
  42. char *s2 = str;
  43. s--;
  44. while (s2<s)
  45. {
  46. char tc = *s2;
  47. *(s2++) = *s;
  48. *(s--) = tc;
  49. }
  50. return str;
  51. }
  52. char *itoa(int n, char *str, int b)
  53. {
  54. return _itoa(n, str, b, (n<0));
  55. }
  56. size_t page_size;
  57. inline offset_t pagetoofs(unsigned pg)
  58. {
  59. return (offset_t)page_size*(offset_t)pg;
  60. }
  61. void nodeStats(int fd,offset_t ofs,unsigned &leaves,unsigned &nonleaves)
  62. {
  63. _lseeki64(fd, ofs, SEEK_SET);
  64. NodeHdr hdr;
  65. if (_read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
  66. printf("ERROR: Could not read node at %"I64F"x", ofs);
  67. return;
  68. }
  69. if (hdr.leafFlag)
  70. leaves++;
  71. else
  72. nonleaves++;
  73. }
  74. void printFileCache(const char *fname, unsigned &globtot, unsigned &globfs, bool verbose, bool onlykey, unsigned &totlf, unsigned &totnl)
  75. {
  76. if (onlykey&&(strstr(fname,"_of_")==NULL))
  77. return;
  78. size_t page_index;
  79. unsigned leaves=0;
  80. unsigned nonleaves=0;
  81. int fd = open(fname,O_RDONLY);
  82. if (fd==-1) {
  83. int err = errno;
  84. if (err==ENOENT)
  85. return;
  86. printf("ERROR: open %s failed %d\n",fname,err);
  87. return;
  88. }
  89. struct stat file_stat;
  90. fstat(fd, &file_stat);
  91. if (!S_ISREG(file_stat.st_mode))
  92. return;
  93. void * file_mmap = mmap((void *)0, file_stat.st_size, PROT_NONE, MAP_SHARED, fd, 0);
  94. page_size = getpagesize();
  95. unsigned fs = (file_stat.st_size+page_size-1)/page_size;
  96. if (!fs)
  97. return;
  98. unsigned char *mincore_vec = (unsigned char *)calloc(1, fs);
  99. mincore(file_mmap, file_stat.st_size, mincore_vec);
  100. printf("%s:\n",fname);
  101. size_t s = 0;
  102. size_t e = (size_t)-1;
  103. unsigned tot = 0;
  104. for (size_t page_index = 0; page_index < fs; page_index++) {
  105. if (mincore_vec[page_index]&1) {
  106. if (page_index&&onlykey&&(pagetoofs(page_index)%8192==0))
  107. nodeStats(fd,pagetoofs(page_index),leaves,nonleaves);
  108. if (e!=-1) {
  109. if (page_index!=e+1) {
  110. tot += (e-s+1);
  111. if (verbose)
  112. printf(" 0x%"I64F"x-0x%"I64F"x = %"I64F"d\n",pagetoofs(s),pagetoofs(e+1)-1,pagetoofs(e-s+1));
  113. s = page_index;
  114. }
  115. }
  116. else
  117. s = page_index;
  118. e = page_index;
  119. }
  120. }
  121. if (e!=-1) {
  122. if (verbose)
  123. printf(" 0x%"I64F"x-0x%"I64F"x = %"I64F"d\n",pagetoofs(s),pagetoofs(e+1)-1,pagetoofs(e-s+1));
  124. tot += (e-s+1);
  125. }
  126. if (onlykey)
  127. printf(" Cached %"I64F"d of %"I64F"d = %0.2f% NonLeaves: %u, Leaves: %u\n",pagetoofs(tot),pagetoofs(fs),(double)tot*100.0/(double)fs,nonleaves,leaves);
  128. else
  129. printf(" Cached %"I64F"d of %"I64F"d = %0.2f%\n",pagetoofs(tot),pagetoofs(fs),(double)tot*100.0/(double)fs);
  130. free(mincore_vec);
  131. munmap(file_mmap, file_stat.st_size);
  132. close(fd);
  133. globtot += tot;
  134. globfs += fs;
  135. totlf += leaves;
  136. totnl += nonleaves;
  137. }
  138. void printPidCachedFiles(int pid,bool verbose,bool onlykey)
  139. {
  140. char path[128];
  141. char tmp[16];
  142. strcpy(path,"/proc/");
  143. strcat(path,itoa(pid,tmp,10));
  144. strcat(path,"/fd/");
  145. DIR * handle = ::opendir(path);
  146. if (!handle) {
  147. printf("ERROR: opendir %s failed %d\n",path,errno);
  148. return;
  149. }
  150. unsigned leaves = 0;
  151. unsigned nonleaves = 0;
  152. unsigned tot = 0;
  153. unsigned fs = 0;
  154. size_t pl = strlen(path);
  155. while (1) {
  156. struct dirent *entry = readdir(handle); // don't need _r here
  157. if (!entry)
  158. break;
  159. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  160. continue;
  161. strcat(path,entry->d_name);
  162. char link[258];
  163. size_t ll = readlink(path,link,sizeof(link));
  164. if (ll==(size_t)-1) {
  165. printf("ERROR readlink failed on %s with %d\n",path,errno);
  166. break;
  167. }
  168. link[ll] = 0;
  169. path[pl] = 0;
  170. printFileCache(link,tot,fs,verbose,onlykey,leaves,nonleaves);
  171. }
  172. closedir(handle);
  173. if (onlykey)
  174. printf("Total cached %"I64F"d of %"I64F"d = %0.2f% NonLeaves: %u, Leaves: %u\n",pagetoofs(tot),pagetoofs(fs),(double)tot*100.0/(double)fs,nonleaves,leaves);
  175. else
  176. printf("Total cached %"I64F"d of %"I64F"d = %0.2f%\n",pagetoofs(tot),pagetoofs(fs),(double)tot*100.0/(double)fs);
  177. }
  178. int main(int argc, const char *argv[])
  179. {
  180. offset_t nodeAddress = 0;
  181. if (argc < 2)
  182. {
  183. printf("Usage: fcached [ -v | -k ] <pid>\n");
  184. exit(2);
  185. }
  186. int arg=1;
  187. bool verbose = false;
  188. bool onlykey = false;
  189. if ((arg+1<argc)&&(stricmp(argv[arg],"-v")==0)) {
  190. verbose = true;
  191. arg++;
  192. }
  193. else if ((arg+1<argc)&&(stricmp(argv[arg],"-k")==0)) {
  194. onlykey = true;
  195. arg++;
  196. }
  197. printPidCachedFiles(atoi(argv[arg]),verbose,onlykey);
  198. return 0;
  199. }