PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/itdk/kapar-0.6/lib/MemoryInfo.cc

https://bitbucket.org/MaxCam/anchordist
C++ | 129 lines | 100 code | 9 blank | 20 comment | 22 complexity | 8450e5de32bfbcd79c41da45b9d8e1d3 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright (C) 2011-2018 The Regents of the University of California.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. // Track memory and CPU usage
  19. #include "config.h"
  20. static const char *cvsID UNUSED = "$Id: MemoryInfo.cc,v 1.7 2015/09/18 19:19:01 kkeys Exp $";
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <sys/time.h>
  24. #include <errno.h>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <iomanip>
  28. #include <stdlib.h>
  29. #include <cstdio>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #define __STDC_FORMAT_MACROS
  33. #include <inttypes.h>
  34. #include "MemoryInfo.h"
  35. using namespace std;
  36. #if !NO_DEBUG_MEMORY
  37. MemoryInfo::MemoryInfo() {
  38. FILE *f = fopen("/proc/self/stat", "r");
  39. if (f) {
  40. // linux
  41. use_proc = true;
  42. fclose(f);
  43. initMem = 0;
  44. initTime = 0;
  45. tickspersec = sysconf(_SC_CLK_TCK);
  46. prevMem = initMem;
  47. prevTime = initTime;
  48. return;
  49. }
  50. use_proc = false;
  51. initTime = getTimeMillis();
  52. sprintf(ps_cmd, "/bin/ps -ovsz -p%ld", long(getpid()));
  53. f = popen(ps_cmd, "r");
  54. if (f) {
  55. char buf[2048];
  56. if (fgets(buf, sizeof(buf), f)) {
  57. if (strcmp(buf, " VSZ\n") == 0) {
  58. use_bsd_ps = true;
  59. }
  60. }
  61. pclose(f);
  62. }
  63. // TODO: implement with getrusage
  64. initMem = (char*)sbrk(0) - (char*)0;
  65. prevMem = initMem;
  66. prevTime = initTime;
  67. }
  68. void MemoryInfo::print(const char *label) {
  69. FILE *f = 0;
  70. int64_t nowMem;
  71. uint64_t nowTime;
  72. if (use_proc) {
  73. f = fopen("/proc/self/stat", "r");
  74. char buf[2048];
  75. if (!fgets(buf, sizeof(buf), f)) {
  76. cerr << "# perf: error: " << strerror(errno) << endl;
  77. goto err;
  78. }
  79. const char *p = strstr(buf, ") ");
  80. if (!p) {
  81. cerr << "# perf: error: bad format" << endl;
  82. goto err;
  83. }
  84. p += 2;
  85. int i = 3;
  86. uint64_t vsize, utime, stime;
  87. while (i < 14) { while (*p && !isspace(*p++)) {/*nop*/} i++; }
  88. sscanf(p, "%"SCNu64 " %"SCNu64, &utime, &stime);
  89. while (i < 23) { while (*p && !isspace(*p++)) {/*nop*/} i++; }
  90. sscanf(p, "%"SCNu64, &vsize);
  91. nowMem = vsize;
  92. nowTime = (utime + stime) * 1000 / tickspersec;
  93. } else if (use_bsd_ps) {
  94. f = popen(ps_cmd, "r");
  95. char buf[2048];
  96. for (int i = 0; i < 2; i++) {
  97. if (!fgets(buf, sizeof(buf), f)) {
  98. cerr << "# perf: error: " << strerror(errno) << endl;
  99. goto err;
  100. }
  101. }
  102. nowMem = strtoull(buf, 0, 10) * 1024;
  103. nowTime = getTimeMillis();
  104. } else {
  105. nowMem = (char*)sbrk(0) - (char*)0;
  106. nowTime = getTimeMillis();
  107. }
  108. cerr << "# perf: " << setw(18) << label << ": " <<
  109. setw(8) << (nowMem - prevMem) / 1024 << " / " << setw(8) << (nowMem - initMem) / 1024 << " kiB, " <<
  110. setw(9) << (nowTime - prevTime) << " / " << setw(9) << (nowTime - initTime) << " ms" << endl;
  111. prevMem = nowMem;
  112. prevTime = nowTime;
  113. err:
  114. if (use_proc) {
  115. fclose(f);
  116. } else if (use_bsd_ps) {
  117. pclose(f);
  118. }
  119. }
  120. #endif