| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/tracer.cc |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 159 | 165 | 96.4% |
| Branches: | 103 | 162 | 63.6% |
| 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 <cassert> | ||
| 11 | #include <cerrno> | ||
| 12 | #include <cstdio> | ||
| 13 | #include <cstdlib> | ||
| 14 | #include <cstring> | ||
| 15 | #include <string> | ||
| 16 | |||
| 17 | #include "util/atomic.h" | ||
| 18 | #include "util/concurrency.h" | ||
| 19 | #include "util/posix.h" | ||
| 20 | #include "util/string.h" | ||
| 21 | |||
| 22 | using namespace std; // NOLINT | ||
| 23 | |||
| 24 | |||
| 25 | 182 | void Tracer::Activate(const int buffer_size, | |
| 26 | const int flush_threshold, | ||
| 27 | const string &trace_file) { | ||
| 28 | 182 | trace_file_ = trace_file; | |
| 29 | 182 | buffer_size_ = buffer_size; | |
| 30 | 182 | flush_threshold_ = flush_threshold; | |
| 31 |
3/6✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 182 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 182 times.
✗ Branch 5 not taken.
|
182 | assert(buffer_size_ > 1 && flush_threshold_ >= 0 |
| 32 | && flush_threshold_ < buffer_size_); | ||
| 33 | |||
| 34 |
4/8✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
✓ Branch 4 taken 54522 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 54522 times.
✓ Branch 7 taken 182 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
54704 | ring_buffer_ = new BufferEntry[buffer_size_]; |
| 35 |
1/2✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
|
182 | commit_buffer_ = new atomic_int32[buffer_size_]; |
| 36 |
2/2✓ Branch 0 taken 54522 times.
✓ Branch 1 taken 182 times.
|
54704 | for (int i = 0; i < buffer_size_; i++) |
| 37 | 54522 | atomic_init32(&commit_buffer_[i]); | |
| 38 | |||
| 39 | int retval; | ||
| 40 | 182 | retval = pthread_cond_init(&sig_continue_trace_, NULL); | |
| 41 | 182 | retval |= pthread_mutex_init(&sig_continue_trace_mutex_, NULL); | |
| 42 | 182 | retval |= pthread_cond_init(&sig_flush_, NULL); | |
| 43 | 182 | retval |= pthread_mutex_init(&sig_flush_mutex_, NULL); | |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(retval == 0); |
| 45 | |||
| 46 | 182 | active_ = true; | |
| 47 | 182 | } | |
| 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 | 4485975 | int32_t Tracer::DoTrace(const int event, | |
| 68 | const PathString &path, | ||
| 69 | const string &msg) { | ||
| 70 | 4485975 | const int32_t my_seq_no = atomic_xadd32(&seq_no_, 1); | |
| 71 | timeval now; | ||
| 72 | 4642144 | gettimeofday(&now, NULL); | |
| 73 | 4639856 | const int pos = my_seq_no % buffer_size_; | |
| 74 | |||
| 75 |
2/2✓ Branch 1 taken 1468883 times.
✓ Branch 2 taken 4641988 times.
|
6168123 | while (my_seq_no - atomic_read32(&flushed_) >= buffer_size_) { |
| 76 | timespec timeout; | ||
| 77 | int retval; | ||
| 78 | 1468883 | GetTimespecRel(25, &timeout); | |
| 79 | 1448642 | retval = pthread_mutex_lock(&sig_continue_trace_mutex_); | |
| 80 |
1/2✓ Branch 1 taken 1561157 times.
✗ Branch 2 not taken.
|
1561157 | retval |= pthread_cond_timedwait(&sig_continue_trace_, |
| 81 | &sig_continue_trace_mutex_, &timeout); | ||
| 82 | 1561157 | retval |= pthread_mutex_unlock(&sig_continue_trace_mutex_); | |
| 83 |
2/4✓ Branch 0 taken 1528644 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1528644 times.
|
1528267 | assert(retval == ETIMEDOUT || retval == 0); |
| 84 | } | ||
| 85 | |||
| 86 | 4641988 | ring_buffer_[pos].time_stamp = now; | |
| 87 | 4641988 | ring_buffer_[pos].code = event; | |
| 88 |
1/2✓ Branch 1 taken 4474340 times.
✗ Branch 2 not taken.
|
4641988 | ring_buffer_[pos].path = path; |
| 89 |
1/2✓ Branch 1 taken 4566250 times.
✗ Branch 2 not taken.
|
4474340 | ring_buffer_[pos].msg = msg; |
| 90 | 4566250 | atomic_inc32(&commit_buffer_[pos]); | |
| 91 | |||
| 92 |
2/2✓ Branch 1 taken 486460 times.
✓ Branch 2 taken 4105673 times.
|
4657978 | if (my_seq_no - atomic_read32(&flushed_) == flush_threshold_) { |
| 93 | 486460 | const MutexLockGuard m(&sig_flush_mutex_); | |
| 94 | const int err_code | ||
| 95 | 486473 | __attribute__((unused)) = pthread_cond_signal(&sig_flush_); | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 486473 times.
|
486473 | assert(err_code == 0 && "Could not signal flush thread"); |
| 97 | 486473 | } | |
| 98 | |||
| 99 | 4590794 | return my_seq_no; | |
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | 18655 | void Tracer::Flush() { | |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18655 times.
|
18655 | if (!active_) |
| 105 | ✗ | return; | |
| 106 | |||
| 107 |
3/6✓ Branch 2 taken 18655 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 19123 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 19019 times.
✗ Branch 9 not taken.
|
18655 | const int32_t save_seq_no = DoTrace(kEventFlush, PathString("Tracer", 6), |
| 108 | "flushed ring buffer"); | ||
| 109 |
2/2✓ Branch 1 taken 16575 times.
✓ Branch 2 taken 18902 times.
|
35802 | while (atomic_read32(&flushed_) <= save_seq_no) { |
| 110 | timespec timeout; | ||
| 111 | int retval; | ||
| 112 | |||
| 113 | 16575 | atomic_cas32(&flush_immediately_, 0, 1); | |
| 114 | { | ||
| 115 | 16770 | const MutexLockGuard m(&sig_flush_mutex_); | |
| 116 | 17043 | retval = pthread_cond_signal(&sig_flush_); | |
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17043 times.
|
17043 | assert(retval == 0); |
| 118 | 17043 | } | |
| 119 | |||
| 120 | 16965 | GetTimespecRel(250, &timeout); | |
| 121 | 16978 | retval = pthread_mutex_lock(&sig_continue_trace_mutex_); | |
| 122 |
1/2✓ Branch 1 taken 17043 times.
✗ Branch 2 not taken.
|
17043 | retval |= pthread_cond_timedwait(&sig_continue_trace_, |
| 123 | &sig_continue_trace_mutex_, &timeout); | ||
| 124 | 17043 | retval |= pthread_mutex_unlock(&sig_continue_trace_mutex_); | |
| 125 |
3/4✓ Branch 0 taken 15899 times.
✓ Branch 1 taken 858 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15899 times.
|
16757 | assert(retval == ETIMEDOUT || retval == 0); |
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | 1874002 | void Tracer::GetTimespecRel(const int64_t ms, timespec *ts) { | |
| 131 | timeval now; | ||
| 132 | 1874002 | gettimeofday(&now, NULL); | |
| 133 | 1872559 | int64_t nsecs = now.tv_usec * 1000 + (ms % 1000) * 1000 * 1000; | |
| 134 | 1872559 | int carry = 0; | |
| 135 |
2/2✓ Branch 0 taken 66794 times.
✓ Branch 1 taken 1805765 times.
|
1872559 | if (nsecs >= 1000 * 1000 * 1000) { |
| 136 | 66794 | carry = 1; | |
| 137 | 66794 | nsecs -= 1000 * 1000 * 1000; | |
| 138 | } | ||
| 139 | 1872559 | ts->tv_sec = now.tv_sec + ms / 1000 + carry; | |
| 140 | 1872559 | ts->tv_nsec = nsecs; | |
| 141 | 1872559 | } | |
| 142 | |||
| 143 | |||
| 144 | 182 | void *Tracer::MainFlush(void *data) { | |
| 145 | 182 | Tracer *tracer = reinterpret_cast<Tracer *>(data); | |
| 146 | int retval; | ||
| 147 | 182 | const MutexLockGuard m(&tracer->sig_flush_mutex_); | |
| 148 |
1/2✓ Branch 2 taken 182 times.
✗ Branch 3 not taken.
|
182 | FILE *f = fopen(tracer->trace_file_.c_str(), "a"); |
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(f != NULL && "Could not open trace file"); |
| 150 | struct timespec timeout; | ||
| 151 | |||
| 152 | do { | ||
| 153 | 1233817 | while ( | |
| 154 | 1641757 | (atomic_read32(&tracer->terminate_flush_thread_) == 0) | |
| 155 |
2/2✓ Branch 1 taken 1636284 times.
✓ Branch 2 taken 5291 times.
|
1641575 | && (atomic_read32(&tracer->flush_immediately_) == 0) |
| 156 |
4/4✓ Branch 0 taken 1641575 times.
✓ Branch 1 taken 182 times.
✓ Branch 4 taken 407940 times.
✓ Branch 5 taken 1233817 times.
|
4919616 | && (atomic_read32(&tracer->seq_no_) - atomic_read32(&tracer->flushed_) |
| 157 |
2/2✓ Branch 0 taken 407940 times.
✓ Branch 1 taken 1228344 times.
|
1636284 | <= tracer->flush_threshold_)) { |
| 158 | 407940 | tracer->GetTimespecRel(2000, &timeout); | |
| 159 |
1/2✓ Branch 1 taken 407940 times.
✗ Branch 2 not taken.
|
407940 | retval = pthread_cond_timedwait(&tracer->sig_flush_, |
| 160 | &tracer->sig_flush_mutex_, &timeout); | ||
| 161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 407940 times.
|
407940 | assert(retval != EINVAL); |
| 162 | } | ||
| 163 | |||
| 164 | 1233817 | const int base = atomic_read32(&tracer->flushed_) % tracer->buffer_size_; | |
| 165 | 1233817 | int pos, i = 0; | |
| 166 | 1233817 | while ((i <= tracer->flush_threshold_) | |
| 167 |
6/6✓ Branch 0 taken 5357911 times.
✓ Branch 1 taken 720083 times.
✓ Branch 2 taken 4844177 times.
✓ Branch 3 taken 513734 times.
✓ Branch 4 taken 4844177 times.
✓ Branch 5 taken 1233817 times.
|
11435905 | && (atomic_read32( |
| 168 | 5357911 | &tracer->commit_buffer_[pos = ((base + i) | |
| 169 | 5357911 | % tracer->buffer_size_)]) | |
| 170 | == 1)) { | ||
| 171 | 4844177 | string tmp; | |
| 172 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | tmp = StringifyTimeval(tracer->ring_buffer_[pos].time_stamp); |
| 173 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval = tracer->WriteCsvFile(f, tmp); |
| 174 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval |= fputc(',', f) - ','; |
| 175 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | tmp = StringifyInt(tracer->ring_buffer_[pos].code); |
| 176 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval = tracer->WriteCsvFile(f, tmp); |
| 177 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval |= fputc(',', f) - ','; |
| 178 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval |= tracer->WriteCsvFile(f, |
| 179 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
9688354 | tracer->ring_buffer_[pos].path.ToString()); |
| 180 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval |= fputc(',', f) - ','; |
| 181 |
1/2✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
|
4844177 | retval |= tracer->WriteCsvFile(f, tracer->ring_buffer_[pos].msg); |
| 182 |
2/4✓ Branch 1 taken 4844177 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4844177 times.
✗ Branch 5 not taken.
|
4844177 | retval |= (fputc(13, f) - 13) | (fputc(10, f) - 10); |
| 183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4844177 times.
|
4844177 | assert(retval == 0); |
| 184 | |||
| 185 | 4844177 | atomic_dec32(&tracer->commit_buffer_[pos]); | |
| 186 | 4844177 | ++i; | |
| 187 | 4844177 | } | |
| 188 |
1/2✓ Branch 1 taken 1233817 times.
✗ Branch 2 not taken.
|
1233817 | retval = fflush(f); |
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1233817 times.
|
1233817 | assert(retval == 0); |
| 190 | 1233817 | atomic_xadd32(&tracer->flushed_, i); | |
| 191 | 1233817 | atomic_cas32(&tracer->flush_immediately_, 1, 0); | |
| 192 | |||
| 193 | { | ||
| 194 | 1233817 | const MutexLockGuard l(&tracer->sig_continue_trace_mutex_); | |
| 195 | 1233817 | retval = pthread_cond_broadcast(&tracer->sig_continue_trace_); | |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1233817 times.
|
1233817 | assert(retval == 0); |
| 197 | 1233817 | } | |
| 198 | } while ( | ||
| 199 | 1233817 | (atomic_read32(&tracer->terminate_flush_thread_) == 0) | |
| 200 |
6/6✓ Branch 0 taken 208 times.
✓ Branch 1 taken 1233609 times.
✓ Branch 4 taken 26 times.
✓ Branch 5 taken 182 times.
✓ Branch 6 taken 1233635 times.
✓ Branch 7 taken 182 times.
|
1233817 | || (atomic_read32(&tracer->flushed_) < atomic_read32(&tracer->seq_no_))); |
| 201 | |||
| 202 |
1/2✓ Branch 1 taken 182 times.
✗ Branch 2 not taken.
|
182 | retval = fclose(f); |
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(retval == 0); |
| 204 | 182 | return NULL; | |
| 205 | 182 | } | |
| 206 | |||
| 207 | |||
| 208 | 195 | void Tracer::Spawn() { | |
| 209 |
2/2✓ Branch 0 taken 182 times.
✓ Branch 1 taken 13 times.
|
195 | if (active_) { |
| 210 | 182 | const int retval = pthread_create(&thread_flush_, NULL, MainFlush, this); | |
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(retval == 0); |
| 212 | |||
| 213 | 182 | spawned_ = true; | |
| 214 |
3/6✓ Branch 2 taken 182 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 182 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 182 times.
✗ Branch 9 not taken.
|
182 | DoTrace(kEventStart, PathString("Tracer", 6), "Trace buffer created"); |
| 215 | } | ||
| 216 | 195 | } | |
| 217 | |||
| 218 | |||
| 219 | 610 | Tracer::Tracer() | |
| 220 | 610 | : active_(false) | |
| 221 | 610 | , spawned_(false) | |
| 222 | 610 | , buffer_size_(0) | |
| 223 | 610 | , flush_threshold_(0) | |
| 224 | 610 | , ring_buffer_(NULL) | |
| 225 | 610 | , commit_buffer_(NULL) { | |
| 226 | 610 | memset(&thread_flush_, 0, sizeof(thread_flush_)); | |
| 227 | 610 | atomic_init32(&seq_no_); | |
| 228 | 610 | atomic_init32(&flushed_); | |
| 229 | 610 | atomic_init32(&terminate_flush_thread_); | |
| 230 | 610 | atomic_init32(&flush_immediately_); | |
| 231 | 610 | } | |
| 232 | |||
| 233 | |||
| 234 | 792 | Tracer::~Tracer() { | |
| 235 |
2/2✓ Branch 0 taken 428 times.
✓ Branch 1 taken 182 times.
|
610 | if (!active_) |
| 236 | 428 | return; | |
| 237 | int retval; | ||
| 238 | |||
| 239 |
1/2✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
|
182 | if (spawned_) { |
| 240 | 182 | DoTrace(kEventStop, PathString("Tracer", 6), "Destroying trace buffer..."); | |
| 241 | |||
| 242 | // Trigger flushing and wait for it | ||
| 243 | 182 | atomic_inc32(&terminate_flush_thread_); | |
| 244 | { | ||
| 245 | 182 | const MutexLockGuard m(&sig_flush_mutex_); | |
| 246 | 182 | retval = pthread_cond_signal(&sig_flush_); | |
| 247 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(retval == 0); |
| 248 | 182 | } | |
| 249 | 182 | retval = pthread_join(thread_flush_, NULL); | |
| 250 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(retval == 0); |
| 251 | } | ||
| 252 | |||
| 253 | 182 | retval = pthread_cond_destroy(&sig_continue_trace_); | |
| 254 | 182 | retval |= pthread_mutex_destroy(&sig_continue_trace_mutex_); | |
| 255 | 182 | retval |= pthread_cond_destroy(&sig_flush_); | |
| 256 | 182 | retval |= pthread_mutex_destroy(&sig_flush_mutex_); | |
| 257 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 182 times.
|
182 | assert(retval == 0); |
| 258 | |||
| 259 |
3/4✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 54522 times.
✓ Branch 3 taken 182 times.
|
54704 | delete[] ring_buffer_; |
| 260 |
1/2✓ Branch 0 taken 182 times.
✗ Branch 1 not taken.
|
182 | delete[] commit_buffer_; |
| 261 |
2/2✓ Branch 1 taken 182 times.
✓ Branch 2 taken 428 times.
|
610 | } |
| 262 | |||
| 263 | |||
| 264 | 19376708 | int Tracer::WriteCsvFile(FILE *fp, const string &field) { | |
| 265 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19376708 times.
|
19376708 | if (fp == NULL) |
| 266 | ✗ | return 0; | |
| 267 | |||
| 268 | int retval; | ||
| 269 | |||
| 270 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 19376708 times.
|
19376708 | if ((retval = fputc('"', fp)) != '"') |
| 271 | ✗ | return retval; | |
| 272 | |||
| 273 |
2/2✓ Branch 1 taken 279363344 times.
✓ Branch 2 taken 19376708 times.
|
298740052 | for (unsigned i = 0, l = field.length(); i < l; ++i) { |
| 274 |
2/2✓ Branch 1 taken 3393000 times.
✓ Branch 2 taken 275970344 times.
|
279363344 | if (field[i] == '"') { |
| 275 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3393000 times.
|
3393000 | if ((retval = fputc('"', fp)) != '"') |
| 276 | ✗ | return retval; | |
| 277 | } | ||
| 278 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 279363344 times.
|
279363344 | if ((retval = fputc(field[i], fp)) != field[i]) |
| 279 | ✗ | return retval; | |
| 280 | } | ||
| 281 | |||
| 282 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 19376708 times.
|
19376708 | if ((retval = fputc('"', fp)) != '"') |
| 283 | ✗ | return retval; | |
| 284 | |||
| 285 | 19376708 | return 0; | |
| 286 | } | ||
| 287 |