Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 17 | #include "trace-dev.inc" |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 18 | |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 19 | static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT; |
Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 20 | |
Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 21 | // Set whether tracing is enabled in this process. This is used to prevent |
| 22 | // the Zygote process from tracing. |
| 23 | void atrace_set_tracing_enabled(bool enabled) |
| 24 | { |
Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 25 | atomic_store_explicit(&atrace_is_enabled, enabled, memory_order_release); |
Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 26 | atrace_update_tags(); |
| 27 | } |
| 28 | |
Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 29 | static void atrace_init_once() |
| 30 | { |
Nick Kralevich | dee1ef4 | 2015-12-16 12:32:26 -0800 | [diff] [blame] | 31 | atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY | O_CLOEXEC); |
Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 32 | if (atrace_marker_fd == -1) { |
| 33 | ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno); |
| 34 | atrace_enabled_tags = 0; |
| 35 | goto done; |
| 36 | } |
| 37 | |
| 38 | atrace_enabled_tags = atrace_get_property(); |
| 39 | |
| 40 | done: |
Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 41 | atomic_store_explicit(&atrace_is_ready, true, memory_order_release); |
Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void atrace_setup() |
| 45 | { |
| 46 | pthread_once(&atrace_once_control, atrace_init_once); |
| 47 | } |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 48 | |
| 49 | void atrace_begin_body(const char* name) |
| 50 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 51 | WRITE_MSG("B|%d|", "%s", name, ""); |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Colin Cross | 9993e79 | 2016-09-16 10:12:52 -0700 | [diff] [blame] | 54 | void atrace_end_body() |
| 55 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 56 | WRITE_MSG("E|%d", "%s", "", ""); |
Christopher Ferris | 82d8489 | 2016-02-23 18:02:20 -0800 | [diff] [blame] | 57 | } |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 58 | |
| 59 | void atrace_async_begin_body(const char* name, int32_t cookie) |
| 60 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 61 | WRITE_MSG("S|%d|", "|%" PRId32, name, cookie); |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void atrace_async_end_body(const char* name, int32_t cookie) |
| 65 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 66 | WRITE_MSG("F|%d|", "|%" PRId32, name, cookie); |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void atrace_int_body(const char* name, int32_t value) |
| 70 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 71 | WRITE_MSG("C|%d|", "|%" PRId32, name, value); |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void atrace_int64_body(const char* name, int64_t value) |
| 75 | { |
Earl Ou | e403038 | 2016-11-22 17:04:44 +0800 | [diff] [blame] | 76 | WRITE_MSG("C|%d|", "|%" PRId64, name, value); |
Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 77 | } |