memtrack.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "memtrack"
  17. #include <android/hardware/memtrack/1.0/IMemtrack.h>
  18. #include <memtrack/memtrack.h>
  19. #include <errno.h>
  20. #include <malloc.h>
  21. #include <vector>
  22. #include <string.h>
  23. #include <mutex>
  24. #include <log/log.h>
  25. using android::hardware::memtrack::V1_0::IMemtrack;
  26. using android::hardware::memtrack::V1_0::MemtrackType;
  27. using android::hardware::memtrack::V1_0::MemtrackRecord;
  28. using android::hardware::memtrack::V1_0::MemtrackFlag;
  29. using android::hardware::memtrack::V1_0::MemtrackStatus;
  30. using android::hardware::hidl_vec;
  31. using android::hardware::Return;
  32. struct memtrack_proc_type {
  33. MemtrackType type;
  34. std::vector<MemtrackRecord> records;
  35. };
  36. struct memtrack_proc {
  37. pid_t pid;
  38. memtrack_proc_type types[static_cast<int>(MemtrackType::NUM_TYPES)];
  39. };
  40. //TODO(b/31632518)
  41. static android::sp<IMemtrack> get_instance() {
  42. static android::sp<IMemtrack> module = IMemtrack::getService();
  43. if (module == nullptr) {
  44. ALOGE("Couldn't load memtrack module");
  45. }
  46. return module;
  47. }
  48. memtrack_proc *memtrack_proc_new(void)
  49. {
  50. return new memtrack_proc();
  51. }
  52. void memtrack_proc_destroy(memtrack_proc *p)
  53. {
  54. delete(p);
  55. }
  56. static int memtrack_proc_get_type(memtrack_proc_type *t,
  57. pid_t pid, MemtrackType type)
  58. {
  59. int err = 0;
  60. android::sp<IMemtrack> memtrack = get_instance();
  61. if (memtrack == nullptr)
  62. return -1;
  63. Return<void> ret = memtrack->getMemory(pid, type,
  64. [&t, &err](MemtrackStatus status, hidl_vec<MemtrackRecord> records) {
  65. if (status != MemtrackStatus::SUCCESS) {
  66. err = -1;
  67. t->records.resize(0);
  68. }
  69. t->records.resize(records.size());
  70. for (size_t i = 0; i < records.size(); i++) {
  71. t->records[i].sizeInBytes = records[i].sizeInBytes;
  72. t->records[i].flags = records[i].flags;
  73. }
  74. });
  75. return ret.isOk() ? err : -1;
  76. }
  77. /* TODO: sanity checks on return values from HALs:
  78. * make sure no records have invalid flags set
  79. * - unknown flags
  80. * - too many flags of a single category
  81. * - missing ACCOUNTED/UNACCOUNTED
  82. * make sure there are not overlapping SHARED and SHARED_PSS records
  83. */
  84. static int memtrack_proc_sanity_check(memtrack_proc* /*p*/)
  85. {
  86. return 0;
  87. }
  88. int memtrack_proc_get(memtrack_proc *p, pid_t pid)
  89. {
  90. if (!p) {
  91. return -EINVAL;
  92. }
  93. p->pid = pid;
  94. for (uint32_t i = 0; i < (uint32_t)MemtrackType::NUM_TYPES; i++) {
  95. int ret = memtrack_proc_get_type(&p->types[i], pid, (MemtrackType)i);
  96. if (ret != 0)
  97. return ret;
  98. }
  99. return memtrack_proc_sanity_check(p);
  100. }
  101. static ssize_t memtrack_proc_sum(memtrack_proc *p,
  102. const std::vector<MemtrackType>& types, uint32_t flags)
  103. {
  104. ssize_t sum = 0;
  105. for (size_t i = 0; i < types.size(); i++) {
  106. memtrack_proc_type type = p->types[static_cast<int>(types[i])];
  107. std::vector<MemtrackRecord> records = type.records;
  108. for (size_t j = 0; j < records.size(); j++) {
  109. if ((records[j].flags & flags) == flags) {
  110. sum += records[j].sizeInBytes;
  111. }
  112. }
  113. }
  114. return sum;
  115. }
  116. ssize_t memtrack_proc_graphics_total(memtrack_proc *p)
  117. {
  118. std::vector<MemtrackType> types = {MemtrackType::GRAPHICS};
  119. return memtrack_proc_sum(p, types, 0);
  120. }
  121. ssize_t memtrack_proc_graphics_pss(memtrack_proc *p)
  122. {
  123. std::vector<MemtrackType> types = { MemtrackType::GRAPHICS };
  124. return memtrack_proc_sum(p, types,
  125. (uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
  126. }
  127. ssize_t memtrack_proc_gl_total(memtrack_proc *p)
  128. {
  129. std::vector<MemtrackType> types = { MemtrackType::GL };
  130. return memtrack_proc_sum(p, types, 0);
  131. }
  132. ssize_t memtrack_proc_gl_pss(memtrack_proc *p)
  133. {
  134. std::vector<MemtrackType> types = { MemtrackType::GL };
  135. return memtrack_proc_sum(p, types,
  136. (uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
  137. }
  138. ssize_t memtrack_proc_other_total(memtrack_proc *p)
  139. {
  140. std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
  141. MemtrackType::CAMERA, MemtrackType::OTHER };
  142. return memtrack_proc_sum(p, types, 0);
  143. }
  144. ssize_t memtrack_proc_other_pss(memtrack_proc *p)
  145. {
  146. std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
  147. MemtrackType::CAMERA, MemtrackType::OTHER };
  148. return memtrack_proc_sum(p, types,
  149. (uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
  150. }