blob: 152ea6157ef42a58d21980dc6300b047d40d577a [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
17#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <pthread.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/types.h>
24#include <cutils/atomic.h>
25#include <cutils/compiler.h>
26#include <cutils/properties.h>
27#include <cutils/trace.h>
28
29#define LOG_TAG "cutils-trace"
30#include <cutils/log.h>
31
32int32_t atrace_is_ready = 0;
33int atrace_marker_fd = -1;
34uint64_t atrace_enabled_tags = ATRACE_TAG_NOT_READY;
35static pthread_once_t atrace_once_control = PTHREAD_ONCE_INIT;
Alex Raye7bb7bc2012-11-20 01:39:09 -080036static pthread_mutex_t atrace_tags_mutex = PTHREAD_MUTEX_INITIALIZER;
Alex Ray0a346432012-11-14 17:25:28 -080037
38// Read the sysprop and return the value tags should be set to
39static uint64_t atrace_get_property()
40{
41 char value[PROPERTY_VALUE_MAX];
42 char *endptr;
43 uint64_t tags;
44
45 property_get("debug.atrace.tags.enableflags", value, "0");
46 errno = 0;
47 tags = strtoull(value, &endptr, 0);
48 if (value[0] == '\0' || *endptr != '\0') {
49 ALOGE("Error parsing trace property: Not a number: %s", value);
50 return 0;
51 } else if (errno == ERANGE || tags == ULLONG_MAX) {
52 ALOGE("Error parsing trace property: Number too large: %s", value);
53 return 0;
54 }
55 return (tags | ATRACE_TAG_ALWAYS) & ATRACE_TAG_VALID_MASK;
56}
57
Alex Raye7bb7bc2012-11-20 01:39:09 -080058// Update tags if tracing is ready. Useful as a sysprop change callback.
59void atrace_update_tags()
60{
61 uint64_t tags;
62 if (CC_UNLIKELY(android_atomic_acquire_load(&atrace_is_ready))) {
63 tags = atrace_get_property();
64 pthread_mutex_lock(&atrace_tags_mutex);
65 atrace_enabled_tags = tags;
66 pthread_mutex_unlock(&atrace_tags_mutex);
67 }
68}
69
Alex Ray0a346432012-11-14 17:25:28 -080070static void atrace_init_once()
71{
72 atrace_marker_fd = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY);
73 if (atrace_marker_fd == -1) {
74 ALOGE("Error opening trace file: %s (%d)", strerror(errno), errno);
75 atrace_enabled_tags = 0;
76 goto done;
77 }
78
79 atrace_enabled_tags = atrace_get_property();
80
81done:
82 android_atomic_release_store(1, &atrace_is_ready);
83}
84
85void atrace_setup()
86{
87 pthread_once(&atrace_once_control, atrace_init_once);
88}