/src/kudu/util/os-util-test.cc

https://github.com/apache/kudu · C++ · 62 lines · 34 code · 12 blank · 16 comment · 0 complexity · 908aa84711e4983988815022fd210067 MD5 · raw file

  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include "kudu/util/os-util.h"
  18. #include <unistd.h>
  19. #include <string>
  20. #include <gtest/gtest.h>
  21. #include "kudu/gutil/strings/substitute.h"
  22. #include "kudu/util/test_macros.h"
  23. using std::string;
  24. namespace kudu {
  25. void RunTest(const string& name, int user_ticks, int kernel_ticks, int io_wait) {
  26. string buf = strings::Substitute(string("0 ($0) S 0 0 0 0 0 0 0") +
  27. " 0 0 0 $1 $2 0 0 0 0 0" +
  28. " 0 0 0 0 0 0 0 0 0 0 " +
  29. " 0 0 0 0 0 0 0 0 0 0 " +
  30. " 0 $3 0 0 0 0 0 0 0 0 " +
  31. " 0 0",
  32. name, user_ticks, kernel_ticks, io_wait);
  33. ThreadStats stats;
  34. string extracted_name;
  35. ASSERT_OK(ParseStat(buf, &extracted_name, &stats));
  36. ASSERT_EQ(name, extracted_name);
  37. ASSERT_EQ(user_ticks * (1e9 / sysconf(_SC_CLK_TCK)), stats.user_ns);
  38. ASSERT_EQ(kernel_ticks * (1e9 / sysconf(_SC_CLK_TCK)), stats.kernel_ns);
  39. ASSERT_EQ(io_wait * (1e9 / sysconf(_SC_CLK_TCK)), stats.iowait_ns);
  40. }
  41. TEST(OsUtilTest, TestSelf) {
  42. RunTest("test", 111, 222, 333);
  43. }
  44. TEST(OsUtilTest, TestSelfNameWithSpace) {
  45. RunTest("a space", 111, 222, 333);
  46. }
  47. TEST(OsUtilTest, TestSelfNameWithParens) {
  48. RunTest("a(b(c((d))e)", 111, 222, 333);
  49. }
  50. } // namespace kudu