subcontext_benchmark.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (C) 2017 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 "subcontext.h"
  17. #include <benchmark/benchmark.h>
  18. #include <selinux/selinux.h>
  19. #include "test_function_map.h"
  20. namespace android {
  21. namespace init {
  22. static void BenchmarkSuccess(benchmark::State& state) {
  23. if (getuid() != 0) {
  24. state.SkipWithError("Skipping benchmark, must be run as root.");
  25. return;
  26. }
  27. char* context;
  28. if (getcon(&context) != 0) {
  29. state.SkipWithError("getcon() failed");
  30. return;
  31. }
  32. auto subcontext = Subcontext("path", context);
  33. free(context);
  34. while (state.KeepRunning()) {
  35. subcontext.Execute(std::vector<std::string>{"return_success"}).IgnoreError();
  36. }
  37. if (subcontext.pid() > 0) {
  38. kill(subcontext.pid(), SIGTERM);
  39. kill(subcontext.pid(), SIGKILL);
  40. }
  41. }
  42. BENCHMARK(BenchmarkSuccess);
  43. TestFunctionMap BuildTestFunctionMap() {
  44. TestFunctionMap test_function_map;
  45. test_function_map.Add("return_success", 0, 0, true,
  46. [](const BuiltinArguments& args) { return Success(); });
  47. return test_function_map;
  48. }
  49. } // namespace init
  50. } // namespace android
  51. int main(int argc, char** argv) {
  52. if (argc > 1 && !strcmp(basename(argv[1]), "subcontext")) {
  53. auto test_function_map = android::init::BuildTestFunctionMap();
  54. return android::init::SubcontextMain(argc, argv, &test_function_map);
  55. }
  56. ::benchmark::Initialize(&argc, argv);
  57. if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
  58. ::benchmark::RunSpecifiedBenchmarks();
  59. }