Benchmark.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2015 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. #include "Benchmark.h"
  17. #include "BenchmarkGen.h"
  18. #include "VolumeManager.h"
  19. #include <android-base/chrono_utils.h>
  20. #include <android-base/file.h>
  21. #include <android-base/logging.h>
  22. #include <cutils/iosched_policy.h>
  23. #include <hardware_legacy/power.h>
  24. #include <private/android_filesystem_config.h>
  25. #include <thread>
  26. #include <sys/resource.h>
  27. #include <sys/time.h>
  28. #include <unistd.h>
  29. using android::base::ReadFileToString;
  30. using android::base::WriteStringToFile;
  31. namespace android {
  32. namespace vold {
  33. // Benchmark currently uses chdir(), which means we can only
  34. // safely run one at a time.
  35. static std::mutex kBenchmarkLock;
  36. static const char* kWakeLock = "Benchmark";
  37. // Reasonable cards are able to complete the create/run stages
  38. // in under 20 seconds.
  39. constexpr auto kTimeout = 20s;
  40. // RAII class for boosting device performance during benchmarks.
  41. class PerformanceBoost {
  42. private:
  43. int orig_prio;
  44. int orig_ioprio;
  45. IoSchedClass orig_clazz;
  46. public:
  47. PerformanceBoost() {
  48. errno = 0;
  49. orig_prio = getpriority(PRIO_PROCESS, 0);
  50. if (errno != 0) {
  51. PLOG(WARNING) << "Failed to getpriority";
  52. orig_prio = 0;
  53. }
  54. if (setpriority(PRIO_PROCESS, 0, -10) != 0) {
  55. PLOG(WARNING) << "Failed to setpriority";
  56. }
  57. if (android_get_ioprio(0, &orig_clazz, &orig_ioprio)) {
  58. PLOG(WARNING) << "Failed to android_get_ioprio";
  59. orig_ioprio = 0;
  60. orig_clazz = IoSchedClass_NONE;
  61. }
  62. if (android_set_ioprio(0, IoSchedClass_RT, 0)) {
  63. PLOG(WARNING) << "Failed to android_set_ioprio";
  64. }
  65. }
  66. ~PerformanceBoost() {
  67. if (android_set_ioprio(0, orig_clazz, orig_ioprio)) {
  68. PLOG(WARNING) << "Failed to android_set_ioprio";
  69. }
  70. if (setpriority(PRIO_PROCESS, 0, orig_prio) != 0) {
  71. PLOG(WARNING) << "Failed to setpriority";
  72. }
  73. }
  74. };
  75. static status_t benchmarkInternal(const std::string& rootPath,
  76. const android::sp<android::os::IVoldTaskListener>& listener,
  77. android::os::PersistableBundle* extras) {
  78. status_t res = 0;
  79. auto path = rootPath;
  80. path += "/misc";
  81. if (android::vold::PrepareDir(path, 01771, AID_SYSTEM, AID_MISC)) {
  82. return -1;
  83. }
  84. path += "/vold";
  85. if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
  86. return -1;
  87. }
  88. path += "/bench";
  89. if (android::vold::PrepareDir(path, 0700, AID_ROOT, AID_ROOT)) {
  90. return -1;
  91. }
  92. char orig_cwd[PATH_MAX];
  93. if (getcwd(orig_cwd, PATH_MAX) == NULL) {
  94. PLOG(ERROR) << "Failed getcwd";
  95. return -1;
  96. }
  97. if (chdir(path.c_str()) != 0) {
  98. PLOG(ERROR) << "Failed chdir";
  99. return -1;
  100. }
  101. sync();
  102. extras->putString(String16("path"), String16(path.c_str()));
  103. extras->putString(String16("ident"), String16(BenchmarkIdent().c_str()));
  104. // Always create
  105. {
  106. android::base::Timer timer;
  107. LOG(INFO) << "Creating " << path;
  108. res |= BenchmarkCreate([&](int progress) -> bool {
  109. if (listener) {
  110. listener->onStatus(progress, *extras);
  111. }
  112. return (timer.duration() < kTimeout);
  113. });
  114. sync();
  115. if (res == OK) extras->putLong(String16("create"), timer.duration().count());
  116. }
  117. // Only drop when we haven't aborted
  118. if (res == OK) {
  119. android::base::Timer timer;
  120. LOG(DEBUG) << "Before drop_caches";
  121. if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) {
  122. PLOG(ERROR) << "Failed to drop_caches";
  123. res = -1;
  124. }
  125. LOG(DEBUG) << "After drop_caches";
  126. sync();
  127. if (res == OK) extras->putLong(String16("drop"), timer.duration().count());
  128. }
  129. // Only run when we haven't aborted
  130. if (res == OK) {
  131. android::base::Timer timer;
  132. LOG(INFO) << "Running " << path;
  133. res |= BenchmarkRun([&](int progress) -> bool {
  134. if (listener) {
  135. listener->onStatus(progress, *extras);
  136. }
  137. return (timer.duration() < kTimeout);
  138. });
  139. sync();
  140. if (res == OK) extras->putLong(String16("run"), timer.duration().count());
  141. }
  142. // Always destroy
  143. {
  144. android::base::Timer timer;
  145. LOG(INFO) << "Destroying " << path;
  146. res |= BenchmarkDestroy();
  147. sync();
  148. if (res == OK) extras->putLong(String16("destroy"), timer.duration().count());
  149. }
  150. if (chdir(orig_cwd) != 0) {
  151. PLOG(ERROR) << "Failed to chdir";
  152. return -1;
  153. }
  154. return res;
  155. }
  156. void Benchmark(const std::string& path,
  157. const android::sp<android::os::IVoldTaskListener>& listener) {
  158. std::lock_guard<std::mutex> lock(kBenchmarkLock);
  159. acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
  160. PerformanceBoost boost;
  161. android::os::PersistableBundle extras;
  162. status_t res = benchmarkInternal(path, listener, &extras);
  163. if (listener) {
  164. listener->onFinished(res, extras);
  165. }
  166. release_wake_lock(kWakeLock);
  167. }
  168. } // namespace vold
  169. } // namespace android