/Users/deen/code/yugabyte-db/src/yb/gutil/dynamic_annotations.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2008-2009, Google Inc. |
2 | | * All rights reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions are |
6 | | * met: |
7 | | * |
8 | | * * Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * * Redistributions in binary form must reproduce the above |
11 | | * copyright notice, this list of conditions and the following disclaimer |
12 | | * in the documentation and/or other materials provided with the |
13 | | * distribution. |
14 | | * * Neither the name of Google Inc. nor the names of its |
15 | | * contributors may be used to endorse or promote products derived from |
16 | | * this software without specific prior written permission. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | * |
30 | | * The following only applies to changes made to this file as part of YugaByte development. |
31 | | * |
32 | | * Portions Copyright (c) YugaByte, Inc. |
33 | | * |
34 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
35 | | * in compliance with the License. You may obtain a copy of the License at |
36 | | * |
37 | | * http://www.apache.org/licenses/LICENSE-2.0 |
38 | | * |
39 | | * Unless required by applicable law or agreed to in writing, software distributed under the License |
40 | | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
41 | | * or implied. See the License for the specific language governing permissions and limitations |
42 | | * under the License. |
43 | | * |
44 | | * |
45 | | * --- |
46 | | * Author: Kostya Serebryany |
47 | | */ |
48 | | |
49 | | #ifdef __cplusplus |
50 | | # error "This file should be built as pure C to avoid name mangling" |
51 | | #endif |
52 | | |
53 | | #include <stdlib.h> |
54 | | #include <string.h> |
55 | | |
56 | | #include "yb/gutil/dynamic_annotations.h" |
57 | | |
58 | | #ifdef __GNUC__ |
59 | | /* valgrind.h uses gcc extensions so it won't build with other compilers */ |
60 | | #include "yb/gutil/valgrind.h" |
61 | | #endif |
62 | | |
63 | | /* Compiler-based ThreadSanitizer defines |
64 | | DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL = 1 |
65 | | and provides its own definitions of the functions. */ |
66 | | |
67 | | #ifndef DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL |
68 | | # define DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL 0 |
69 | | #endif |
70 | | |
71 | | /* Each function is empty and called (via a macro) only in debug mode. |
72 | | The arguments are captured by dynamic tools at runtime. */ |
73 | | |
74 | | #if DYNAMIC_ANNOTATIONS_ENABLED == 1 \ |
75 | | && DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 |
76 | | |
77 | | void AnnotateRWLockCreate(const char *file, int line, void *lock) {} |
78 | | void AnnotateRWLockCreateStatic(const char *file, int line, void *lock) {} |
79 | | void AnnotateRWLockDestroy(const char *file, int line, void *lock) {} |
80 | | void AnnotateRWLockAcquired(const char *file, int line, void *lock, long is_w) {} |
81 | | |
82 | | void AnnotateBarrierInit(const char *file, int line, |
83 | | const volatile void *barrier, long count, |
84 | | long reinitialization_allowed) {} |
85 | | void AnnotateBarrierWaitBefore(const char *file, int line, |
86 | | const volatile void *barrier) {} |
87 | | void AnnotateBarrierWaitAfter(const char *file, int line, |
88 | | const volatile void *barrier) {} |
89 | | void AnnotateBarrierDestroy(const char *file, int line, |
90 | | const volatile void *barrier) {} |
91 | | |
92 | | void AnnotateCondVarWait(const char *file, int line, |
93 | | const volatile void *cv, |
94 | | const volatile void *lock){} |
95 | | void AnnotateCondVarSignal(const char *file, int line, |
96 | | const volatile void *cv){} |
97 | | void AnnotateCondVarSignalAll(const char *file, int line, |
98 | | const volatile void *cv){} |
99 | | void AnnotatePublishMemoryRange(const char *file, int line, |
100 | | const volatile void *address, |
101 | | long size){} |
102 | | void AnnotateUnpublishMemoryRange(const char *file, int line, |
103 | | const volatile void *address, |
104 | | long size){} |
105 | | void AnnotatePCQCreate(const char *file, int line, |
106 | | const volatile void *pcq){} |
107 | | void AnnotatePCQDestroy(const char *file, int line, |
108 | | const volatile void *pcq){} |
109 | | void AnnotatePCQPut(const char *file, int line, |
110 | | const volatile void *pcq){} |
111 | | void AnnotatePCQGet(const char *file, int line, |
112 | | const volatile void *pcq){} |
113 | | void AnnotateNewMemory(const char *file, int line, void *mem, size_t size) {} |
114 | | void AnnotateExpectRace(const char *file, int line, |
115 | | const volatile void *mem, |
116 | | const char *description){} |
117 | | void AnnotateBenignRace(const char *file, int line, |
118 | | const volatile void *mem, |
119 | | const char *description){} |
120 | | void AnnotateBenignRaceSized(const char *file, int line, |
121 | | const volatile void *mem, |
122 | | long size, |
123 | | const char *description) {} |
124 | | void AnnotateMutexIsUsedAsCondVar(const char *file, int line, |
125 | | const volatile void *mu){} |
126 | | void AnnotateTraceMemory(const char *file, int line, |
127 | | const volatile void *arg){} |
128 | | void AnnotateThreadName(const char *file, int line, |
129 | | const char *name){} |
130 | | void AnnotateIgnoreReadsBegin(const char *file, int line){} |
131 | | void AnnotateIgnoreReadsEnd(const char *file, int line){} |
132 | | void AnnotateIgnoreWritesBegin(const char *file, int line){} |
133 | | void AnnotateIgnoreWritesEnd(const char *file, int line){} |
134 | | void AnnotateEnableRaceDetection(const char *file, int line, int enable){} |
135 | | void AnnotateNoOp(const char *file, int line, |
136 | | const volatile void *arg){} |
137 | | void AnnotateFlushState(const char *file, int line){} |
138 | | |
139 | | #endif /* DYNAMIC_ANNOTATIONS_ENABLED == 1 |
140 | | && DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */ |
141 | | |
142 | | #if DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 |
143 | | |
144 | 32.7k | static int GetYbRunningOnValgrind(void) { |
145 | 32.7k | #ifdef RUNNING_ON_VALGRIND |
146 | 32.7k | if (RUNNING_ON_VALGRIND) return 10 ; |
147 | 32.7k | #endif |
148 | 32.7k | char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND"); |
149 | 32.7k | if (running_on_valgrind_str) { |
150 | 0 | return strcmp(running_on_valgrind_str, "0") != 0; |
151 | 0 | } |
152 | 32.7k | return 0; |
153 | 32.7k | } |
154 | | |
155 | | /* See the comments in dynamic_annotations.h */ |
156 | 32.7k | int YbRunningOnValgrind(void) { |
157 | 32.7k | static volatile int running_on_valgrind = -1; |
158 | 32.7k | int local_running_on_valgrind = running_on_valgrind; |
159 | | /* C doesn't have thread-safe initialization of statics, and we |
160 | | don't want to depend on pthread_once here, so hack it. */ |
161 | 32.7k | ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack"); |
162 | 32.7k | if (local_running_on_valgrind == -1) |
163 | 32.7k | running_on_valgrind = local_running_on_valgrind = GetYbRunningOnValgrind(); |
164 | 32.7k | return local_running_on_valgrind; |
165 | 32.7k | } |
166 | | |
167 | | /* See the comments in dynamic_annotations.h */ |
168 | 0 | double YbValgrindSlowdown(void) { |
169 | | /* Same initialization hack as in YbRunningOnValgrind(). */ |
170 | 0 | static volatile double slowdown = 0.0; |
171 | 0 | double local_slowdown = slowdown; |
172 | 0 | ANNOTATE_BENIGN_RACE(&slowdown, "safe hack"); |
173 | 0 | if (YbRunningOnValgrind() == 0) { |
174 | 0 | return 1.0; |
175 | 0 | } |
176 | 0 | if (local_slowdown == 0.0) { |
177 | 0 | char *env = getenv("VALGRIND_SLOWDOWN"); |
178 | 0 | slowdown = local_slowdown = env ? atof(env) : 50.0; |
179 | 0 | } |
180 | 0 | return local_slowdown; |
181 | 0 | } |
182 | | |
183 | | #endif /* DYNAMIC_ANNOTATIONS_EXTERNAL_IMPL == 0 */ |