26#ifndef SURICATA_THREADS_PROFILE_H
27#define SURICATA_THREADS_PROFILE_H
32#define PROFILING_MAX_LOCKS 64
63#define SCMutexLock_profile(mut) ({ \
67 uint64_t mutex_lock_start = UtilCpuGetTicks(); \
68 if (pthread_mutex_trylock((mut)) != 0) { \
69 mutex_lock_contention++; \
71 retl = pthread_mutex_lock(mut); \
73 uint64_t mutex_lock_end = UtilCpuGetTicks(); \
74 mutex_lock_wait_ticks += (uint64_t)(mutex_lock_end - mutex_lock_start); \
76 if (locks_idx < PROFILING_MAX_LOCKS && record_locks) { \
77 locks[locks_idx].file = (char *)__FILE__; \
78 locks[locks_idx].func = (char *)__func__; \
79 locks[locks_idx].line = (int)__LINE__; \
80 locks[locks_idx].type = LOCK_MUTEX; \
81 locks[locks_idx].cont = cont; \
82 locks[locks_idx].ticks = (uint64_t)(mutex_lock_end - mutex_lock_start); \
88#define SCMutex pthread_mutex_t
89#define SCMutexAttr pthread_mutexattr_t
90#define SCMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
91#define SCMutexLock(mut) SCMutexLock_profile(mut)
92#define SCMutexTrylock(mut) pthread_mutex_trylock(mut)
93#define SCMutexUnlock(mut) pthread_mutex_unlock(mut)
94#define SCMutexDestroy pthread_mutex_destroy
95#define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
99#define SCCondT pthread_cond_t
100#define SCCondInit pthread_cond_init
101#define SCCondSignal pthread_cond_signal
102#define SCCondDestroy pthread_cond_destroy
103#define SCCondWait(cond, mut) pthread_cond_wait(cond, mut)
112#define SCSpinLock_profile(spin) ({ \
116 uint64_t spin_lock_start = UtilCpuGetTicks(); \
117 if (pthread_spin_trylock((spin)) != 0) { \
118 spin_lock_contention++; \
120 retl = pthread_spin_lock((spin)); \
122 uint64_t spin_lock_end = UtilCpuGetTicks(); \
123 spin_lock_wait_ticks += (uint64_t)(spin_lock_end - spin_lock_start); \
125 if (locks_idx < PROFILING_MAX_LOCKS && record_locks) { \
126 locks[locks_idx].file = (char *)__FILE__; \
127 locks[locks_idx].func = (char *)__func__; \
128 locks[locks_idx].line = (int)__LINE__; \
129 locks[locks_idx].type = LOCK_SPIN; \
130 locks[locks_idx].cont = cont; \
131 locks[locks_idx].ticks = (uint64_t)(spin_lock_end - spin_lock_start); \
137#define SCSpinlock pthread_spinlock_t
138#define SCSpinLock(mut) SCSpinLock_profile(mut)
139#define SCSpinTrylock(spin) pthread_spin_trylock(spin)
140#define SCSpinUnlock(spin) pthread_spin_unlock(spin)
141#define SCSpinInit(spin, spin_attr) pthread_spin_init(spin, spin_attr)
142#define SCSpinDestroy(spin) pthread_spin_destroy(spin)
150#define SCRWLockWRLock_profile(mut) ({ \
154 uint64_t rww_lock_start = UtilCpuGetTicks(); \
155 if (pthread_rwlock_trywrlock((mut)) != 0) { \
156 rww_lock_contention++; \
158 retl = pthread_rwlock_wrlock(mut); \
160 uint64_t rww_lock_end = UtilCpuGetTicks(); \
161 rww_lock_wait_ticks += (uint64_t)(rww_lock_end - rww_lock_start); \
163 if (locks_idx < PROFILING_MAX_LOCKS && record_locks) { \
164 locks[locks_idx].file = (char *)__FILE__; \
165 locks[locks_idx].func = (char *)__func__; \
166 locks[locks_idx].line = (int)__LINE__; \
167 locks[locks_idx].type = LOCK_RWW; \
168 locks[locks_idx].cont = cont; \
169 locks[locks_idx].ticks = (uint64_t)(rww_lock_end - rww_lock_start); \
179#define SCRWLockRDLock_profile(mut) ({ \
183 uint64_t rwr_lock_start = UtilCpuGetTicks(); \
184 if (pthread_rwlock_tryrdlock((mut)) != 0) { \
185 rwr_lock_contention++; \
187 retl = pthread_rwlock_rdlock(mut); \
189 uint64_t rwr_lock_end = UtilCpuGetTicks(); \
190 rwr_lock_wait_ticks += (uint64_t)(rwr_lock_end - rwr_lock_start); \
192 if (locks_idx < PROFILING_MAX_LOCKS && record_locks) { \
193 locks[locks_idx].file = (char *)__FILE__; \
194 locks[locks_idx].func = (char *)__func__; \
195 locks[locks_idx].line = (int)__LINE__; \
196 locks[locks_idx].type = LOCK_RWR; \
197 locks[locks_idx].cont = cont; \
198 locks[locks_idx].ticks = (uint64_t)(rwr_lock_end - rwr_lock_start); \
204#define SCRWLock pthread_rwlock_t
205#define SCRWLockInit(rwl, rwlattr ) pthread_rwlock_init(rwl, rwlattr)
206#define SCRWLockWRLock(mut) SCRWLockWRLock_profile(mut)
207#define SCRWLockRDLock(mut) SCRWLockRDLock_profile(mut)
208#define SCRWLockTryWRLock(rwl) pthread_rwlock_trywrlock(rwl)
209#define SCRWLockTryRDLock(rwl) pthread_rwlock_tryrdlock(rwl)
210#define SCRWLockUnlock(rwl) pthread_rwlock_unlock(rwl)
211#define SCRWLockDestroy pthread_rwlock_destroy
214#define SCCtrlMutex pthread_mutex_t
215#define SCCtrlMutexAttr pthread_mutexattr_t
216#define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
217#define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
218#define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
219#define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
220#define SCCtrlMutexDestroy pthread_mutex_destroy
223#define SCCtrlCondT pthread_cond_t
224#define SCCtrlCondInit pthread_cond_init
225#define SCCtrlCondSignal pthread_cond_signal
226#define SCCtrlCondTimedwait pthread_cond_timedwait
227#define SCCtrlCondWait pthread_cond_wait
228#define SCCtrlCondDestroy pthread_cond_destroy
thread_local uint64_t rww_lock_cnt
thread_local uint64_t mutex_lock_cnt
thread_local uint64_t spin_lock_wait_ticks
thread_local uint64_t rwr_lock_wait_ticks
thread_local uint64_t rwr_lock_cnt
thread_local uint64_t rww_lock_wait_ticks
thread_local uint64_t mutex_lock_wait_ticks
#define PROFILING_MAX_LOCKS
thread_local ProfilingLock locks[PROFILING_MAX_LOCKS]
thread_local uint64_t spin_lock_cnt
thread_local int record_locks
thread_local uint64_t rwr_lock_contention
thread_local uint64_t mutex_lock_contention
thread_local int locks_idx
thread_local uint64_t rww_lock_contention
struct ProfilingLock_ ProfilingLock
thread_local uint64_t spin_lock_contention