blob: eacc8ee56b02ffbce4de6d62976a64a5ed46f756 [file] [log] [blame]
Alex Ray0a346432012-11-14 17:25:28 -08001/*
2 * Copyright (C) 2012 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
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/trace.h>
18
Earl Oue4030382016-11-22 17:04:44 +080019#include "trace-dev.inc"
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070020
Earl Oue4030382016-11-22 17:04:44 +080021static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
Jamie Gennis774f9292013-02-25 18:15:40 -080022
Jamie Gennisb13ea452013-04-15 18:50:22 -070023// Set whether tracing is enabled in this process. This is used to prevent
24// the Zygote process from tracing.
25void atrace_set_tracing_enabled(bool enabled)
26{
Yabin Cuia8ac32c2015-04-15 14:50:27 -070027 atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release);
Jamie Gennisb13ea452013-04-15 18:50:22 -070028 atrace_update_tags();
29}
30
Alex Ray0a346432012-11-14 17:25:28 -080031static void atrace_init_once()
32{
Hridya Valsaraju355ff8c2020-09-12 15:37:39 -070033 atrace_marker_fd = open("/sys/kernel/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
Alex Ray0a346432012-11-14 17:25:28 -080034 if (atrace_marker_fd == -1) {
Hridya Valsaraju355ff8c2020-09-12 15:37:39 -070035 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
Hridya Valsaraju6a879b22020-01-30 17:34:46 -080036 }
37
38 if (atrace_marker_fd == -1) {
Alex Ray0a346432012-11-14 17:25:28 -080039 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
40 atrace_enabled_tags = 0;
Florian Mayerad427b72019-10-10 18:14:44 +010041 } else {
42 atrace_enabled_tags = atrace_get_property();
Alex Ray0a346432012-11-14 17:25:28 -080043 }
Florian Mayerb06766c2019-12-10 12:20:03 +000044}
45
46static void atrace_seq_number_changed(uint32_t prev_seq_no, uint32_t seq_no) {
47 if (!atomic_load_explicit(&atrace_is_enabled, memory_order_acquire)) {
48 return;
49 }
50
51 // Someone raced us.
52 if (!atomic_compare_exchange_strong(&last_sequence_number, &prev_seq_no, seq_no)) {
53 return;
54 }
55
56 if (CC_UNLIKELY(prev_seq_no == kSeqNoNotInit)) {
57#if defined(__BIONIC__)
58 const prop_info* new_pi = __system_property_find("debug.atrace.tags.enableflags");
59 if (new_pi) atrace_property_info = new_pi;
60#endif
61 pthread_once(&atrace_once_control, atrace_init_once);
62 }
63
64 atrace_update_tags();
Alex Ray0a346432012-11-14 17:25:28 -080065}
66
67void atrace_setup()
68{
Florian Mayerb06766c2019-12-10 12:20:03 +000069 atrace_init();
Alex Ray0a346432012-11-14 17:25:28 -080070}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070071
72void atrace_begin_body(const char* name)
73{
Ray Ye9a542402022-03-15 23:28:01 +000074 WRITE_MSG("B|%d|", "%s", "", name, "");
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070075}
76
Colin Cross9993e792016-09-16 10:12:52 -070077void atrace_end_body()
78{
Ray Ye9a542402022-03-15 23:28:01 +000079 WRITE_MSG("E|%d", "%s", "", "", "");
Christopher Ferris82d84892016-02-23 18:02:20 -080080}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070081
82void atrace_async_begin_body(const char* name, int32_t cookie)
83{
Ray Ye9a542402022-03-15 23:28:01 +000084 WRITE_MSG("S|%d|", "|%" PRId32, "", name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070085}
86
87void atrace_async_end_body(const char* name, int32_t cookie)
88{
Ray Ye9a542402022-03-15 23:28:01 +000089 WRITE_MSG("F|%d|", "|%" PRId32, "", name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070090}
91
Ray Ye399465a2022-03-21 20:21:53 +000092void atrace_async_for_track_begin_body(const char* track_name, const char* name, int32_t cookie) {
93 WRITE_MSG("T|%d|", "|%" PRId32, track_name, name, cookie);
94}
95
96void atrace_async_for_track_end_body(const char* track_name, const char* name, int32_t cookie) {
97 WRITE_MSG("U|%d|", "|%" PRId32, track_name, name, cookie);
98}
99
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800100void atrace_instant_body(const char* name) {
Ray Ye9a542402022-03-15 23:28:01 +0000101 WRITE_MSG("I|%d|", "%s", "", name, "");
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800102}
103
Ray Ye9a542402022-03-15 23:28:01 +0000104void atrace_instant_for_track_body(const char* track_name, const char* name) {
105 WRITE_MSG("N|%d|", "%s", track_name, name, "");
Lucas Dupin2c2c5d92021-12-08 16:37:22 -0800106}
107
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700108void atrace_int_body(const char* name, int32_t value)
109{
Ray Ye9a542402022-03-15 23:28:01 +0000110 WRITE_MSG("C|%d|", "|%" PRId32, "", name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700111}
112
113void atrace_int64_body(const char* name, int64_t value)
114{
Ray Ye9a542402022-03-15 23:28:01 +0000115 WRITE_MSG("C|%d|", "|%" PRId64, "", name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -0700116}