blob: 4da82155567062b50e6a965f56a2686a09612227 [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{
Nick Kralevichdee1ef42015-12-16 12:32:26 -080033 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC);
Alex Ray0a346432012-11-14 17:25:28 -080034 if (atrace_marker_fd == -1) {
35 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
36 atrace_enabled_tags = 0;
Paul Crowley738adf72018-02-08 00:49:37 +000037 goto done;
Alex Ray0a346432012-11-14 17:25:28 -080038 }
Paul Crowley738adf72018-02-08 00:49:37 +000039
Alex Ray0a346432012-11-14 17:25:28 -080040 atrace_enabled_tags = atrace_get_property();
Paul Crowley738adf72018-02-08 00:49:37 +000041
42done:
43 atomic_store_explicit(&atrace_is_ready, true, memory_order_release);
Alex Ray0a346432012-11-14 17:25:28 -080044}
45
46void atrace_setup()
47{
Paul Crowley738adf72018-02-08 00:49:37 +000048 pthread_once(&atrace_once_control, atrace_init_once);
Alex Ray0a346432012-11-14 17:25:28 -080049}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070050
51void atrace_begin_body(const char* name)
52{
Earl Oue4030382016-11-22 17:04:44 +080053 WRITE_MSG("B|%d|", "%s", name, "");
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070054}
55
Colin Cross9993e792016-09-16 10:12:52 -070056void atrace_end_body()
57{
Earl Oue4030382016-11-22 17:04:44 +080058 WRITE_MSG("E|%d", "%s", "", "");
Christopher Ferris82d84892016-02-23 18:02:20 -080059}
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070060
61void atrace_async_begin_body(const char* name, int32_t cookie)
62{
Earl Oue4030382016-11-22 17:04:44 +080063 WRITE_MSG("S|%d|", "|%" PRId32, name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070064}
65
66void atrace_async_end_body(const char* name, int32_t cookie)
67{
Earl Oue4030382016-11-22 17:04:44 +080068 WRITE_MSG("F|%d|", "|%" PRId32, name, cookie);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070069}
70
71void atrace_int_body(const char* name, int32_t value)
72{
Earl Oue4030382016-11-22 17:04:44 +080073 WRITE_MSG("C|%d|", "|%" PRId32, name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070074}
75
76void atrace_int64_body(const char* name, int64_t value)
77{
Earl Oue4030382016-11-22 17:04:44 +080078 WRITE_MSG("C|%d|", "|%" PRId64, name, value);
Chih-Hung Hsieh2d3150e2014-10-13 16:30:24 -070079}