trace-dev_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C) 2016 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 <sys/types.h>
  17. #include <unistd.h>
  18. #include <memory>
  19. #include <string>
  20. #include <android-base/file.h>
  21. #include <android-base/stringprintf.h>
  22. #include <gtest/gtest.h>
  23. #include "../trace-dev.cpp"
  24. class TraceDevTest : public ::testing::Test {
  25. protected:
  26. void SetUp() override {
  27. lseek(tmp_file_.fd, 0, SEEK_SET);
  28. atrace_marker_fd = tmp_file_.fd;
  29. }
  30. void TearDown() override {
  31. atrace_marker_fd = -1;
  32. }
  33. TemporaryFile tmp_file_;
  34. static std::string MakeName(size_t length) {
  35. std::string name;
  36. for (size_t i = 0; i < length; i++) {
  37. name += '0' + (i % 10);
  38. }
  39. return name;
  40. }
  41. };
  42. TEST_F(TraceDevTest, atrace_begin_body_normal) {
  43. atrace_begin_body("fake_name");
  44. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  45. std::string actual;
  46. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  47. std::string expected = android::base::StringPrintf("B|%d|fake_name", getpid());
  48. ASSERT_STREQ(expected.c_str(), actual.c_str());
  49. }
  50. TEST_F(TraceDevTest, atrace_begin_body_exact) {
  51. std::string expected = android::base::StringPrintf("B|%d|", getpid());
  52. std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1);
  53. atrace_begin_body(name.c_str());
  54. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  55. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  56. std::string actual;
  57. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  58. expected += name;
  59. ASSERT_STREQ(expected.c_str(), actual.c_str());
  60. // Add a single character and verify we get the exact same value as before.
  61. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  62. name += '*';
  63. atrace_begin_body(name.c_str());
  64. EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  65. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  66. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  67. ASSERT_STREQ(expected.c_str(), actual.c_str());
  68. }
  69. TEST_F(TraceDevTest, atrace_begin_body_truncated) {
  70. std::string expected = android::base::StringPrintf("B|%d|", getpid());
  71. std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
  72. atrace_begin_body(name.c_str());
  73. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  74. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  75. std::string actual;
  76. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  77. int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 1;
  78. expected += android::base::StringPrintf("%.*s", expected_len, name.c_str());
  79. ASSERT_STREQ(expected.c_str(), actual.c_str());
  80. }
  81. TEST_F(TraceDevTest, atrace_async_begin_body_normal) {
  82. atrace_async_begin_body("fake_name", 12345);
  83. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  84. std::string actual;
  85. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  86. std::string expected = android::base::StringPrintf("S|%d|fake_name|12345", getpid());
  87. ASSERT_STREQ(expected.c_str(), actual.c_str());
  88. }
  89. TEST_F(TraceDevTest, atrace_async_begin_body_exact) {
  90. std::string expected = android::base::StringPrintf("S|%d|", getpid());
  91. std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
  92. atrace_async_begin_body(name.c_str(), 12345);
  93. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  94. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  95. std::string actual;
  96. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  97. expected += name + "|12345";
  98. ASSERT_STREQ(expected.c_str(), actual.c_str());
  99. // Add a single character and verify we get the exact same value as before.
  100. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  101. name += '*';
  102. atrace_async_begin_body(name.c_str(), 12345);
  103. EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  104. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  105. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  106. ASSERT_STREQ(expected.c_str(), actual.c_str());
  107. }
  108. TEST_F(TraceDevTest, atrace_async_begin_body_truncated) {
  109. std::string expected = android::base::StringPrintf("S|%d|", getpid());
  110. std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
  111. atrace_async_begin_body(name.c_str(), 12345);
  112. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  113. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  114. std::string actual;
  115. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  116. int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
  117. expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
  118. ASSERT_STREQ(expected.c_str(), actual.c_str());
  119. }
  120. TEST_F(TraceDevTest, atrace_async_end_body_normal) {
  121. atrace_async_end_body("fake_name", 12345);
  122. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  123. std::string actual;
  124. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  125. std::string expected = android::base::StringPrintf("F|%d|fake_name|12345", getpid());
  126. ASSERT_STREQ(expected.c_str(), actual.c_str());
  127. }
  128. TEST_F(TraceDevTest, atrace_async_end_body_exact) {
  129. std::string expected = android::base::StringPrintf("F|%d|", getpid());
  130. std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
  131. atrace_async_end_body(name.c_str(), 12345);
  132. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  133. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  134. std::string actual;
  135. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  136. expected += name + "|12345";
  137. ASSERT_STREQ(expected.c_str(), actual.c_str());
  138. // Add a single character and verify we get the exact same value as before.
  139. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  140. name += '*';
  141. atrace_async_end_body(name.c_str(), 12345);
  142. EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  143. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  144. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  145. ASSERT_STREQ(expected.c_str(), actual.c_str());
  146. }
  147. TEST_F(TraceDevTest, atrace_async_end_body_truncated) {
  148. std::string expected = android::base::StringPrintf("F|%d|", getpid());
  149. std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
  150. atrace_async_end_body(name.c_str(), 12345);
  151. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  152. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  153. std::string actual;
  154. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  155. int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
  156. expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
  157. ASSERT_STREQ(expected.c_str(), actual.c_str());
  158. }
  159. TEST_F(TraceDevTest, atrace_int_body_normal) {
  160. atrace_int_body("fake_name", 12345);
  161. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  162. std::string actual;
  163. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  164. std::string expected = android::base::StringPrintf("C|%d|fake_name|12345", getpid());
  165. ASSERT_STREQ(expected.c_str(), actual.c_str());
  166. }
  167. TEST_F(TraceDevTest, atrace_int_body_exact) {
  168. std::string expected = android::base::StringPrintf("C|%d|", getpid());
  169. std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
  170. atrace_int_body(name.c_str(), 12345);
  171. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  172. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  173. std::string actual;
  174. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  175. expected += name + "|12345";
  176. ASSERT_STREQ(expected.c_str(), actual.c_str());
  177. // Add a single character and verify we get the exact same value as before.
  178. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  179. name += '*';
  180. atrace_int_body(name.c_str(), 12345);
  181. EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  182. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  183. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  184. ASSERT_STREQ(expected.c_str(), actual.c_str());
  185. }
  186. TEST_F(TraceDevTest, atrace_int_body_truncated) {
  187. std::string expected = android::base::StringPrintf("C|%d|", getpid());
  188. std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
  189. atrace_int_body(name.c_str(), 12345);
  190. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  191. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  192. std::string actual;
  193. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  194. int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
  195. expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
  196. ASSERT_STREQ(expected.c_str(), actual.c_str());
  197. }
  198. TEST_F(TraceDevTest, atrace_int64_body_normal) {
  199. atrace_int64_body("fake_name", 17179869183L);
  200. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  201. std::string actual;
  202. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  203. std::string expected = android::base::StringPrintf("C|%d|fake_name|17179869183", getpid());
  204. ASSERT_STREQ(expected.c_str(), actual.c_str());
  205. }
  206. TEST_F(TraceDevTest, atrace_int64_body_exact) {
  207. std::string expected = android::base::StringPrintf("C|%d|", getpid());
  208. std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 13);
  209. atrace_int64_body(name.c_str(), 17179869183L);
  210. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  211. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  212. std::string actual;
  213. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  214. expected += name + "|17179869183";
  215. ASSERT_STREQ(expected.c_str(), actual.c_str());
  216. // Add a single character and verify we get the exact same value as before.
  217. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  218. name += '*';
  219. atrace_int64_body(name.c_str(), 17179869183L);
  220. EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  221. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  222. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  223. ASSERT_STREQ(expected.c_str(), actual.c_str());
  224. }
  225. TEST_F(TraceDevTest, atrace_int64_body_truncated) {
  226. std::string expected = android::base::StringPrintf("C|%d|", getpid());
  227. std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
  228. atrace_int64_body(name.c_str(), 17179869183L);
  229. ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
  230. ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
  231. std::string actual;
  232. ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
  233. int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 13;
  234. expected += android::base::StringPrintf("%.*s|17179869183", expected_len, name.c_str());
  235. ASSERT_STREQ(expected.c_str(), actual.c_str());
  236. }