You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.1 KiB

  1. /***************************************************************************************[System.cc]
  2. Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
  3. Copyright (c) 2007-2010, Niklas Sorensson
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  5. associated documentation files (the "Software"), to deal in the Software without restriction,
  6. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  7. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all copies or
  10. substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  12. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  14. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  15. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. **************************************************************************************************/
  17. #include "utils/System.h"
  18. #if defined(__linux__)
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. using namespace Minisat;
  22. // TODO: split the memory reading functions into two: one for reading high-watermark of RSS, and
  23. // one for reading the current virtual memory size.
  24. static inline int memReadStat(int field)
  25. {
  26. char name[256];
  27. pid_t pid = getpid();
  28. int value;
  29. sprintf(name, "/proc/%d/statm", pid);
  30. FILE* in = fopen(name, "rb");
  31. if (in == NULL) return 0;
  32. for (; field >= 0; field--)
  33. if (fscanf(in, "%d", &value) != 1)
  34. printf("ERROR! Failed to parse memory statistics from \"/proc\".\n"), exit(1);
  35. fclose(in);
  36. return value;
  37. }
  38. static inline int memReadPeak(void)
  39. {
  40. char name[256];
  41. pid_t pid = getpid();
  42. sprintf(name, "/proc/%d/status", pid);
  43. FILE* in = fopen(name, "rb");
  44. if (in == NULL) return 0;
  45. // Find the correct line, beginning with "VmPeak:":
  46. int peak_kb = 0;
  47. while (!feof(in) && fscanf(in, "VmPeak: %d kB", &peak_kb) != 1)
  48. while (!feof(in) && fgetc(in) != '\n')
  49. ;
  50. fclose(in);
  51. return peak_kb;
  52. }
  53. double Minisat::memUsed() { return (double)memReadStat(0) * (double)getpagesize() / (1024*1024); }
  54. double Minisat::memUsedPeak() {
  55. double peak = memReadPeak() / 1024;
  56. return peak == 0 ? memUsed() : peak; }
  57. #elif defined(__FreeBSD__)
  58. double Minisat::memUsed(void) {
  59. struct rusage ru;
  60. getrusage(RUSAGE_SELF, &ru);
  61. return (double)ru.ru_maxrss / 1024; }
  62. double MiniSat::memUsedPeak(void) { return memUsed(); }
  63. #elif defined(__APPLE__)
  64. #include <malloc/malloc.h>
  65. double Minisat::memUsed(void) {
  66. malloc_statistics_t t;
  67. malloc_zone_statistics(NULL, &t);
  68. return (double)t.max_size_in_use / (1024*1024); }
  69. #else
  70. double Minisat::memUsed() {
  71. return 0; }
  72. #endif