| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/tracer.cc |
| Date: | 2026-11-08 02:41:18 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 160 | 166 | 96.4% |
| Branches: | 105 | 164 | 64.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | |||
| 6 | #include "tracer.h" | ||
| 7 | |||
| 8 | #include <pthread.h> | ||
| 9 | |||
| 10 | #include <atomic> | ||
| 11 | #include <cassert> | ||
| 12 | #include <cerrno> | ||
| 13 | #include <cstdio> | ||
| 14 | #include <cstdlib> | ||
| 15 | #include <cstring> | ||
| 16 | #include <string> | ||
| 17 | |||
| 18 | #include "util/concurrency.h" | ||
| 19 | #include "util/posix.h" | ||
| 20 | #include "util/string.h" | ||
| 21 | |||
| 22 | using namespace std; // NOLINT | ||
| 23 | |||
| 24 | |||
| 25 | 224 | void Tracer::Activate(const int buffer_size, | |
| 26 | const int flush_threshold, | ||
| 27 | const string &trace_file) { | ||
| 28 | 224 | trace_file_ = trace_file; | |
| 29 | 224 | buffer_size_ = buffer_size; | |
| 30 | 224 | flush_threshold_ = flush_threshold; | |
| 31 |
3/6✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 224 times.
✗ Branch 5 not taken.
|
224 | assert(buffer_size_ > 1 && flush_threshold_ >= 0 |
| 32 | && flush_threshold_ < buffer_size_); | ||
| 33 | |||
| 34 |
4/8✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 67104 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 67104 times.
✓ Branch 7 taken 224 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
67328 | ring_buffer_ = new BufferEntry[buffer_size_]; |
| 35 |
3/4✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 67104 times.
✓ Branch 5 taken 224 times.
|
67328 | commit_buffer_ = new std::atomic<int32_t>[buffer_size_]; |
| 36 |
2/2✓ Branch 0 taken 67104 times.
✓ Branch 1 taken 224 times.
|
67328 | for (int i = 0; i < buffer_size_; i++) |
| 37 | 67104 | commit_buffer_[i].store(0); | |
| 38 | |||
| 39 | int retval; | ||
| 40 | 224 | retval = pthread_cond_init(&sig_continue_trace_, NULL); | |
| 41 | 224 | retval |= pthread_mutex_init(&sig_continue_trace_mutex_, NULL); | |
| 42 | 224 | retval |= pthread_cond_init(&sig_flush_, NULL); | |
| 43 | 224 | retval |= pthread_mutex_init(&sig_flush_mutex_, NULL); | |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(retval == 0); |
| 45 | |||
| 46 | 224 | active_ = true; | |
| 47 | 224 | } | |
| 48 | |||
| 49 | |||
| 50 | /** | ||
| 51 | * Trace a message. This is usually a lock-free procedure that just | ||
| 52 | * requires two fetch_and_add operations and a gettimeofday syscall. | ||
| 53 | * There are two exceptions: | ||
| 54 | * -# If the ring buffer is full, the function blocks until the flush | ||
| 55 | * thread made some space. Avoid that by carefully choosing size | ||
| 56 | * and threshold. | ||
| 57 | * -# If this message reaches the threshold, the flush thread gets | ||
| 58 | * signaled. | ||
| 59 | * | ||
| 60 | * \param[in] event Arbitrary code, for consistency applications should use one | ||
| 61 | * of the TraceEvents constants. Negative codes are reserved | ||
| 62 | * for internal use. | ||
| 63 | * \param[in] id Arbitrary id, for example file name or module name which is | ||
| 64 | * doing the trace. | ||
| 65 | * \return The sequence number which was used to trace the record | ||
| 66 | */ | ||
| 67 | 5551424 | int32_t Tracer::DoTrace(const int event, | |
| 68 | const PathString &path, | ||
| 69 | const string &msg) { | ||
| 70 | 5551424 | const int32_t my_seq_no = seq_no_.fetch_add(1); | |
| 71 | timeval now; | ||
| 72 | 5551424 | gettimeofday(&now, NULL); | |
| 73 | 5729680 | const int pos = my_seq_no % buffer_size_; | |
| 74 | |||
| 75 |
2/2✓ Branch 1 taken 1656800 times.
✓ Branch 2 taken 5674192 times.
|
14832576 | while (my_seq_no - flushed_.load() >= buffer_size_) { |
| 76 | timespec timeout; | ||
| 77 | int retval; | ||
| 78 | 1656800 | GetTimespecRel(25, &timeout); | |
| 79 | 1636016 | retval = pthread_mutex_lock(&sig_continue_trace_mutex_); | |
| 80 |
1/2✓ Branch 1 taken 1816032 times.
✗ Branch 2 not taken.
|
1816032 | retval |= pthread_cond_timedwait(&sig_continue_trace_, |
| 81 | &sig_continue_trace_mutex_, &timeout); | ||
| 82 | 1816032 | retval |= pthread_mutex_unlock(&sig_continue_trace_mutex_); | |
| 83 |
2/4✓ Branch 0 taken 1774512 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1774512 times.
|
1771904 | assert(retval == ETIMEDOUT || retval == 0); |
| 84 | } | ||
| 85 | |||
| 86 | 5674192 | ring_buffer_[pos].time_stamp = now; | |
| 87 | 5674192 | ring_buffer_[pos].code = event; | |
| 88 |
1/2✓ Branch 1 taken 5568832 times.
✗ Branch 2 not taken.
|
5674192 | ring_buffer_[pos].path = path; |
| 89 |
1/2✓ Branch 1 taken 5644672 times.
✗ Branch 2 not taken.
|
5568832 | ring_buffer_[pos].msg = msg; |
| 90 | 5644672 | commit_buffer_[pos].fetch_add(1); | |
| 91 | |||
| 92 |
2/2✓ Branch 1 taken 623472 times.
✓ Branch 2 taken 5069392 times.
|
11337536 | if (my_seq_no - flushed_.load() == flush_threshold_) { |
| 93 | 623472 | const MutexLockGuard m(&sig_flush_mutex_); | |
| 94 | const int err_code | ||
| 95 | 623488 | __attribute__((unused)) = pthread_cond_signal(&sig_flush_); | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 623488 times.
|
623488 | assert(err_code == 0 && "Could not signal flush thread"); |
| 97 | 623488 | } | |
| 98 | |||
| 99 | 5692080 | return my_seq_no; | |
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | 23056 | void Tracer::Flush() { | |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23056 times.
|
23056 | if (!active_) |
| 105 | ✗ | return; | |
| 106 | |||
| 107 |
3/6✓ Branch 2 taken 23168 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 23712 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 23584 times.
✗ Branch 9 not taken.
|
23056 | const int32_t save_seq_no = DoTrace(kEventFlush, PathString("Tracer", 6), |
| 108 | "flushed ring buffer"); | ||
| 109 |
2/2✓ Branch 1 taken 21312 times.
✓ Branch 2 taken 23248 times.
|
89472 | while (flushed_.load() <= save_seq_no) { |
| 110 | timespec timeout; | ||
| 111 | int retval; | ||
| 112 | |||
| 113 | 21312 | int32_t expected_val = 0; | |
| 114 | 21312 | flush_immediately_.compare_exchange_strong(expected_val, 1); | |
| 115 | { | ||
| 116 | 21152 | const MutexLockGuard m(&sig_flush_mutex_); | |
| 117 | 21456 | retval = pthread_cond_signal(&sig_flush_); | |
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21456 times.
|
21456 | assert(retval == 0); |
| 119 | 21456 | } | |
| 120 | |||
| 121 | 21440 | GetTimespecRel(250, &timeout); | |
| 122 | 21360 | retval = pthread_mutex_lock(&sig_continue_trace_mutex_); | |
| 123 |
1/2✓ Branch 1 taken 21456 times.
✗ Branch 2 not taken.
|
21456 | retval |= pthread_cond_timedwait(&sig_continue_trace_, |
| 124 | &sig_continue_trace_mutex_, &timeout); | ||
| 125 | 21456 | retval |= pthread_mutex_unlock(&sig_continue_trace_mutex_); | |
| 126 |
3/4✓ Branch 0 taken 19552 times.
✓ Branch 1 taken 1568 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19552 times.
|
21120 | assert(retval == ETIMEDOUT || retval == 0); |
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | 2163760 | void Tracer::GetTimespecRel(const int64_t ms, timespec *ts) { | |
| 132 | timeval now; | ||
| 133 | 2163760 | gettimeofday(&now, NULL); | |
| 134 | 2168448 | int64_t nsecs = now.tv_usec * 1000 + (ms % 1000) * 1000 * 1000; | |
| 135 | 2168448 | int carry = 0; | |
| 136 |
2/2✓ Branch 0 taken 35440 times.
✓ Branch 1 taken 2133008 times.
|
2168448 | if (nsecs >= 1000 * 1000 * 1000) { |
| 137 | 35440 | carry = 1; | |
| 138 | 35440 | nsecs -= 1000 * 1000 * 1000; | |
| 139 | } | ||
| 140 | 2168448 | ts->tv_sec = now.tv_sec + ms / 1000 + carry; | |
| 141 | 2168448 | ts->tv_nsec = nsecs; | |
| 142 | 2168448 | } | |
| 143 | |||
| 144 | |||
| 145 | 224 | void *Tracer::MainFlush(void *data) { | |
| 146 | 224 | Tracer *tracer = reinterpret_cast<Tracer *>(data); | |
| 147 | int retval; | ||
| 148 | 224 | const MutexLockGuard m(&tracer->sig_flush_mutex_); | |
| 149 |
1/2✓ Branch 2 taken 224 times.
✗ Branch 3 not taken.
|
224 | FILE *f = fopen(tracer->trace_file_.c_str(), "a"); |
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(f != NULL && "Could not open trace file"); |
| 151 | struct timespec timeout; | ||
| 152 | |||
| 153 | do { | ||
| 154 | 2084176 | while ((tracer->terminate_flush_thread_.load() == 0) | |
| 155 |
2/2✓ Branch 1 taken 2076528 times.
✓ Branch 2 taken 7424 times.
|
4167904 | && (tracer->flush_immediately_.load() == 0) |
| 156 |
4/4✓ Branch 0 taken 2083952 times.
✓ Branch 1 taken 224 times.
✓ Branch 4 taken 521824 times.
✓ Branch 5 taken 1562352 times.
|
8321184 | && ((tracer->seq_no_).load() - (tracer->flushed_).load() |
| 157 |
2/2✓ Branch 0 taken 521824 times.
✓ Branch 1 taken 1554704 times.
|
2076528 | <= tracer->flush_threshold_)) { |
| 158 | 521824 | tracer->GetTimespecRel(2000, &timeout); | |
| 159 |
1/2✓ Branch 1 taken 521824 times.
✗ Branch 2 not taken.
|
521824 | retval = pthread_cond_timedwait(&tracer->sig_flush_, |
| 160 | &tracer->sig_flush_mutex_, &timeout); | ||
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 521824 times.
|
521824 | assert(retval != EINVAL); |
| 162 | } | ||
| 163 | |||
| 164 | 1562352 | const int base = tracer->flushed_.load() % tracer->buffer_size_; | |
| 165 | 1562352 | int pos, i = 0; | |
| 166 | 1562352 | while ( | |
| 167 | 7524416 | (i <= tracer->flush_threshold_) | |
| 168 |
4/4✓ Branch 0 taken 6638000 times.
✓ Branch 1 taken 886416 times.
✓ Branch 2 taken 5962064 times.
✓ Branch 3 taken 1562352 times.
|
14162416 | && ((tracer->commit_buffer_[pos = ((base + i) % tracer->buffer_size_)]) |
| 169 |
2/2✓ Branch 1 taken 5962064 times.
✓ Branch 2 taken 675936 times.
|
13276000 | .load() |
| 170 | == 1)) { | ||
| 171 | 5962064 | string tmp; | |
| 172 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | tmp = StringifyTimeval(tracer->ring_buffer_[pos].time_stamp); |
| 173 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval = tracer->WriteCsvFile(f, tmp); |
| 174 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval |= fputc(',', f) - ','; |
| 175 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | tmp = StringifyInt(tracer->ring_buffer_[pos].code); |
| 176 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval = tracer->WriteCsvFile(f, tmp); |
| 177 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval |= fputc(',', f) - ','; |
| 178 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval |= tracer->WriteCsvFile(f, |
| 179 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
11924128 | tracer->ring_buffer_[pos].path.ToString()); |
| 180 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval |= fputc(',', f) - ','; |
| 181 |
1/2✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
|
5962064 | retval |= tracer->WriteCsvFile(f, tracer->ring_buffer_[pos].msg); |
| 182 |
2/4✓ Branch 1 taken 5962064 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5962064 times.
✗ Branch 5 not taken.
|
5962064 | retval |= (fputc(13, f) - 13) | (fputc(10, f) - 10); |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5962064 times.
|
5962064 | assert(retval == 0); |
| 184 | |||
| 185 | 5962064 | tracer->commit_buffer_[pos].fetch_sub(1); | |
| 186 | 5962064 | ++i; | |
| 187 | 5962064 | } | |
| 188 |
1/2✓ Branch 1 taken 1562352 times.
✗ Branch 2 not taken.
|
1562352 | retval = fflush(f); |
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1562352 times.
|
1562352 | assert(retval == 0); |
| 190 | 1562352 | tracer->flushed_.fetch_add(i); | |
| 191 | 1562352 | int32_t expected_val = 1; | |
| 192 | 1562352 | tracer->flush_immediately_.compare_exchange_strong(expected_val, 0); | |
| 193 | |||
| 194 | { | ||
| 195 | 1562352 | const MutexLockGuard l(&tracer->sig_continue_trace_mutex_); | |
| 196 | 1562352 | retval = pthread_cond_broadcast(&tracer->sig_continue_trace_); | |
| 197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1562352 times.
|
1562352 | assert(retval == 0); |
| 198 | 1562352 | } | |
| 199 | 1562352 | } while ((tracer->terminate_flush_thread_.load() == 0) | |
| 200 |
6/6✓ Branch 0 taken 256 times.
✓ Branch 1 taken 1562096 times.
✓ Branch 4 taken 32 times.
✓ Branch 5 taken 224 times.
✓ Branch 6 taken 1562128 times.
✓ Branch 7 taken 224 times.
|
1562864 | || ((tracer->flushed_).load() < (tracer->seq_no_).load())); |
| 201 | |||
| 202 |
1/2✓ Branch 1 taken 224 times.
✗ Branch 2 not taken.
|
224 | retval = fclose(f); |
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(retval == 0); |
| 204 | 224 | return NULL; | |
| 205 | 224 | } | |
| 206 | |||
| 207 | |||
| 208 | 240 | void Tracer::Spawn() { | |
| 209 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 16 times.
|
240 | if (active_) { |
| 210 | 224 | const int retval = pthread_create(&thread_flush_, NULL, MainFlush, this); | |
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(retval == 0); |
| 212 | |||
| 213 | 224 | spawned_ = true; | |
| 214 |
3/6✓ Branch 2 taken 224 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 224 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 224 times.
✗ Branch 9 not taken.
|
224 | DoTrace(kEventStart, PathString("Tracer", 6), "Trace buffer created"); |
| 215 | } | ||
| 216 | 240 | } | |
| 217 | |||
| 218 | |||
| 219 | 612 | Tracer::Tracer() | |
| 220 | 612 | : active_(false) | |
| 221 | 612 | , spawned_(false) | |
| 222 | 612 | , buffer_size_(0) | |
| 223 | 612 | , flush_threshold_(0) | |
| 224 | 612 | , ring_buffer_(NULL) | |
| 225 | 612 | , commit_buffer_(NULL) { | |
| 226 | 612 | memset(&thread_flush_, 0, sizeof(thread_flush_)); | |
| 227 | 612 | seq_no_.store(0); | |
| 228 | 612 | flushed_.store(0); | |
| 229 | 612 | terminate_flush_thread_.store(0); | |
| 230 | 612 | flush_immediately_.store(0); | |
| 231 | 612 | } | |
| 232 | |||
| 233 | |||
| 234 | 836 | Tracer::~Tracer() { | |
| 235 |
2/2✓ Branch 0 taken 388 times.
✓ Branch 1 taken 224 times.
|
612 | if (!active_) |
| 236 | 388 | return; | |
| 237 | int retval; | ||
| 238 | |||
| 239 |
1/2✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
|
224 | if (spawned_) { |
| 240 | 224 | DoTrace(kEventStop, PathString("Tracer", 6), "Destroying trace buffer..."); | |
| 241 | |||
| 242 | // Trigger flushing and wait for it | ||
| 243 | 224 | terminate_flush_thread_.fetch_add(1); | |
| 244 | { | ||
| 245 | 224 | const MutexLockGuard m(&sig_flush_mutex_); | |
| 246 | 224 | retval = pthread_cond_signal(&sig_flush_); | |
| 247 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(retval == 0); |
| 248 | 224 | } | |
| 249 | 224 | retval = pthread_join(thread_flush_, NULL); | |
| 250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(retval == 0); |
| 251 | } | ||
| 252 | |||
| 253 | 224 | retval = pthread_cond_destroy(&sig_continue_trace_); | |
| 254 | 224 | retval |= pthread_mutex_destroy(&sig_continue_trace_mutex_); | |
| 255 | 224 | retval |= pthread_cond_destroy(&sig_flush_); | |
| 256 | 224 | retval |= pthread_mutex_destroy(&sig_flush_mutex_); | |
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 224 times.
|
224 | assert(retval == 0); |
| 258 | |||
| 259 |
3/4✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67104 times.
✓ Branch 3 taken 224 times.
|
67328 | delete[] ring_buffer_; |
| 260 |
1/2✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
|
224 | delete[] commit_buffer_; |
| 261 |
2/2✓ Branch 1 taken 224 times.
✓ Branch 2 taken 388 times.
|
612 | } |
| 262 | |||
| 263 | |||
| 264 | 23848256 | int Tracer::WriteCsvFile(FILE *fp, const string &field) { | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23848256 times.
|
23848256 | if (fp == NULL) |
| 266 | ✗ | return 0; | |
| 267 | |||
| 268 | int retval; | ||
| 269 | |||
| 270 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23848256 times.
|
23848256 | if ((retval = fputc('"', fp)) != '"') |
| 271 | ✗ | return retval; | |
| 272 | |||
| 273 |
2/2✓ Branch 1 taken 343831808 times.
✓ Branch 2 taken 23848256 times.
|
367680064 | for (unsigned i = 0, l = field.length(); i < l; ++i) { |
| 274 |
2/2✓ Branch 1 taken 4176000 times.
✓ Branch 2 taken 339655808 times.
|
343831808 | if (field[i] == '"') { |
| 275 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4176000 times.
|
4176000 | if ((retval = fputc('"', fp)) != '"') |
| 276 | ✗ | return retval; | |
| 277 | } | ||
| 278 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 343831808 times.
|
343831808 | if ((retval = fputc(field[i], fp)) != field[i]) |
| 279 | ✗ | return retval; | |
| 280 | } | ||
| 281 | |||
| 282 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 23848256 times.
|
23848256 | if ((retval = fputc('"', fp)) != '"') |
| 283 | ✗ | return retval; | |
| 284 | |||
| 285 | 23848256 | return 0; | |
| 286 | } | ||
| 287 | |||
| 288 |