| 1 |
// Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 |
|
| 3 |
#include <inttypes.h> |
| 4 |
#include <stdbool.h> |
| 5 |
|
| 6 |
#include "windows.h" |
| 7 |
|
| 8 |
#include "azure_c_shared_utility/xlogging.h" |
| 9 |
#include "azure_c_shared_utility/gballoc.h" |
| 10 |
#include "azure_c_shared_utility/timer.h" |
| 11 |
#include "azure_c_shared_utility/string_utils.h" |
| 12 |
|
| 13 |
#include "azure_c_shared_utility/srw_lock.h" |
| 14 |
|
| 15 |
/* |
| 16 |
vocabulary: |
| 17 |
blaBlaCount = values of counter for event blabla |
| 18 |
blablaCounts = sum of individual blablaCountEnd-blablaCountBegin (always a sum of durations of Counts) |
| 19 |
time = blablaCounts/freq |
| 20 |
*/ |
| 21 |
|
| 22 |
#define TIME_BETWEEN_STATISTICS_LOG 600 /*in seconds, so every 10 minutes*/ |
| 23 |
|
| 24 |
typedef struct SRW_LOCK_HANDLE_DATA_TAG |
| 25 |
{ |
| 26 |
|
| 27 |
SRWLOCK lock; |
| 28 |
|
| 29 |
LARGE_INTEGER freq; |
| 30 |
|
| 31 |
volatile LONG64 nCalls_AcquireSRWLockExclusive; /*number of calls to AcquireSRWLockExclusive*/ |
| 32 |
volatile LONG64 totalCounts_AcquireSRWLockExclusive; /*how many counts were spent taking the lock (that is, just before and after AcquireSRWLockExclusive call*/ |
| 33 |
volatile LONG64 totalCounts_ReleaseSRWLockExclusive; /*how many counts were spent releasing the exclusive SRW that is, just before and after ReleaseSRWLockExclusive call*/ |
| 34 |
volatile LONG64 lastCount_AcquireSRWLockExclusive; /*last time the lock was taken exclusively*/ |
| 35 |
volatile LONG64 totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive; /*how much time the lock was taken in total in exclusive mode*/ |
| 36 |
|
| 37 |
volatile LONG64 nCalls_AcquireSRWLockShared; /*number of calls to AcquireSRWLockShared*/ |
| 38 |
volatile LONG64 totalCounts_AcquireSRWLockShared; /*how many counts were spent taking the lock (that is, just before and after AcquireSRWLockShared */ |
| 39 |
volatile LONG64 totalCounts_ReleaseSRWLockShared; /*how many counts were spent releasing the shared SRW that is, just before and after ReleaseSRWLockShared call*/ |
| 40 |
volatile LONG nSharedReaders; /*if lock_shared is granted, then there are 1 more readers... when readers gets to 0, the shared "locked" count can be updated*/ |
| 41 |
volatile LONG64 firstCount_AcquireSRWLockShared; /*first time the lock was taken in shared mode*/ |
| 42 |
volatile LONG64 totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared; /*how much time the lock was taken from the first granted shared access to the last released shared access*/ |
| 43 |
|
| 44 |
LARGE_INTEGER handleCreateCount; /*when the handle was created*/ |
| 45 |
|
| 46 |
TIMER_HANDLE timer; |
| 47 |
|
| 48 |
char* lockName; |
| 49 |
bool doStatistics; |
| 50 |
|
| 51 |
}SRW_LOCK_HANDLE_DATA; |
| 52 |
|
| 53 |
|
| 54 |
static void LogStatistics(SRW_LOCK_HANDLE handle, const char* reason) |
| 55 |
{ |
| 56 |
LARGE_INTEGER now; |
| 57 |
(void)QueryPerformanceCounter(&now); |
| 58 |
|
| 59 |
/*print statistics*/ |
| 60 |
|
| 61 |
LogInfo("srw_lock_statistics reason:%s SRW_LOCK_HANDLE handle %p lock_name=%s\n" |
| 62 |
"freq=%" PRId64 ",\n" |
| 63 |
"nCalls_AcquireSRWLockExclusive=%" PRId64 ",\n" |
| 64 |
"totalCounts_AcquireSRWLockExclusive=%" PRId64 ",\n" |
| 65 |
"totalCounts_ReleaseSRWLockExclusive=%" PRId64 ",\n" |
| 66 |
"totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive=%" PRId64 ",\n" |
| 67 |
"nCalls_AcquireSRWLockShared=%" PRId64 ",\n" |
| 68 |
"totalCounts_AcquireSRWLockShared=%" PRId64 ",\n" |
| 69 |
"totalCounts_ReleaseSRWLockShared=%" PRId64 ",\n" |
| 70 |
"totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared=%" PRId64 ",\n" |
| 71 |
"totalCountsBetween_Create_and_Now=%" PRId64 ",\n" |
| 72 |
"totalTimeBetween_Create_and_Now[s]=%.2f, \n" |
| 73 |
"totalTimeBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive[s]=%.2f, \n" |
| 74 |
"totalTimeBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive[%%]=%.2f, \n" |
| 75 |
"averageTimeBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive[us]=%.2f, \n" |
| 76 |
"totalTimeBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared[s]=%.2f, \n" |
| 77 |
"totalTimeBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared[%%]=%.2f", |
| 78 |
|
| 79 |
reason, |
| 80 |
handle, |
| 81 |
handle->lockName, |
| 82 |
handle->freq.QuadPart, |
| 83 |
InterlockedAdd64(&handle->nCalls_AcquireSRWLockExclusive, 0), |
| 84 |
InterlockedAdd64(&handle->totalCounts_AcquireSRWLockExclusive, 0), |
| 85 |
InterlockedAdd64(&handle->totalCounts_ReleaseSRWLockExclusive, 0), |
| 86 |
InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, 0), |
| 87 |
InterlockedAdd64(&handle->nCalls_AcquireSRWLockShared, 0), |
| 88 |
InterlockedAdd64(&handle->totalCounts_AcquireSRWLockShared, 0), |
| 89 |
InterlockedAdd64(&handle->totalCounts_ReleaseSRWLockShared, 0), |
| 90 |
InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared, 0), |
| 91 |
/*"totalTimeBetween_Create_and_Now[s]*/ |
| 92 |
now.QuadPart - handle->handleCreateCount.QuadPart, |
| 93 |
(double)(now.QuadPart -handle->handleCreateCount.QuadPart)/ (double)handle->freq.QuadPart, |
| 94 |
(double)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, 0) / (double)handle->freq.QuadPart, |
| 95 |
(double)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, 0) / (double)(now.QuadPart - handle->handleCreateCount.QuadPart) *100, |
| 96 |
(InterlockedAdd64(&handle->nCalls_AcquireSRWLockExclusive, 0)>0?(double)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, 0)*1000000 / (double)(now.QuadPart - handle->handleCreateCount.QuadPart) / InterlockedAdd64(&handle->nCalls_AcquireSRWLockExclusive, 0):-1), |
| 97 |
(double)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared , 0) / (double)handle->freq.QuadPart, |
| 98 |
(double)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared, 0) / (double)(now.QuadPart - handle->handleCreateCount.QuadPart) * 100 |
| 99 |
); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
SRW_LOCK_HANDLE srw_lock_create(bool do_statistics, const char* lock_name) |
| 104 |
{ |
| 105 |
SRW_LOCK_HANDLE result; |
| 106 |
/*Codes_SRS_SRW_LOCK_02_001: [ srw_lock_create shall allocate memory for SRW_LOCK_HANDLE. ]*/ |
| 107 |
result = calloc(1, sizeof(SRW_LOCK_HANDLE_DATA)); |
| 108 |
if (result == NULL) |
| 109 |
{ |
| 110 |
/*return as is*/ |
| 111 |
LogError("failure in malloc(sizeof(SRW_LOCK_HANDLE_DATA)=%zu)", sizeof(SRW_LOCK_HANDLE_DATA)); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
/*Codes_SRS_SRW_LOCK_02_023: [ If do_statistics is true then srw_lock_create shall copy lock_name. ]*/ |
| 116 |
if (do_statistics && |
| 117 |
((result->lockName= sprintf_char("%s", MU_P_OR_NULL(lock_name))) == NULL) |
| 118 |
) |
| 119 |
{ |
| 120 |
LogError("failure in sprintf_char(\"%%s\", lock_name=%s)", MU_P_OR_NULL(lock_name)); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
/*Codes_SRS_SRW_LOCK_02_024: [ If do_statistics is true then srw_lock_create shall create a new TIMER_HANDLE by calling timer_create. ]*/ |
| 125 |
if( |
| 126 |
do_statistics && |
| 127 |
((result->timer = timer_create())==NULL) |
| 128 |
) |
| 129 |
{ |
| 130 |
LogError("failure in timer_create()"); |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
result->doStatistics = do_statistics; |
| 135 |
/*Codes_SRS_SRW_LOCK_02_015: [ srw_lock_create shall call InitializeSRWLock. ]*/ |
| 136 |
|
| 137 |
InitializeSRWLock(&result->lock); |
| 138 |
|
| 139 |
(void)QueryPerformanceFrequency(&result->freq); |
| 140 |
|
| 141 |
(void)InterlockedExchange64(&result->nCalls_AcquireSRWLockExclusive, 0); |
| 142 |
(void)InterlockedExchange64(&result->nCalls_AcquireSRWLockShared, 0); |
| 143 |
|
| 144 |
(void)InterlockedExchange64(&result->totalCounts_AcquireSRWLockExclusive, 0); |
| 145 |
(void)InterlockedExchange64(&result->totalCounts_ReleaseSRWLockExclusive, 0); |
| 146 |
|
| 147 |
(void)InterlockedExchange64(&result->totalCounts_AcquireSRWLockShared, 0); |
| 148 |
(void)InterlockedExchange64(&result->totalCounts_ReleaseSRWLockShared, 0); |
| 149 |
|
| 150 |
(void)InterlockedExchange64(&result->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, 0); |
| 151 |
|
| 152 |
(void)InterlockedExchange(&result->nSharedReaders, 0); |
| 153 |
(void)InterlockedExchange64(&result->totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared, 0); |
| 154 |
|
| 155 |
|
| 156 |
(void)QueryPerformanceCounter(&result->handleCreateCount); |
| 157 |
|
| 158 |
if (do_statistics) |
| 159 |
{ |
| 160 |
LogInfo("srw_lock_create returns %p for lock_name=%s", result, MU_P_OR_NULL(lock_name)); |
| 161 |
} |
| 162 |
/*Codes_SRS_SRW_LOCK_02_003: [ srw_lock_create shall succeed and return a non-NULL value. ]*/ |
| 163 |
goto allOk; |
| 164 |
|
| 165 |
//timer_destroy(result->timer); |
| 166 |
} |
| 167 |
/*Codes_SRS_SRW_LOCK_02_004: [ If there are any failures then srw_lock_create shall fail and return NULL. ]*/ |
| 168 |
do_statistics ? free(result->lockName) : (void)0; |
| 169 |
} |
| 170 |
free(result); |
| 171 |
result = NULL; |
| 172 |
} |
| 173 |
allOk:; |
| 174 |
return result; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
void srw_lock_acquire_exclusive(SRW_LOCK_HANDLE handle) |
| 179 |
{ |
| 180 |
/*Codes_SRS_SRW_LOCK_02_022: [ If handle is NULL then srw_lock_acquire_exclusive shall return. ]*/ |
| 181 |
if (handle == NULL) |
| 182 |
{ |
| 183 |
LogError("invalid argument SRW_LOCK_HANDLE handle=%p", handle); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
if (!handle->doStatistics) |
| 188 |
{ |
| 189 |
AcquireSRWLockExclusive(&handle->lock); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
LARGE_INTEGER start, stop; |
| 194 |
(void)QueryPerformanceCounter(&start); |
| 195 |
/*Codes_SRS_SRW_LOCK_02_006: [ srw_lock_acquire_exclusive shall call AcquireSRWLockExclusive. ]*/ |
| 196 |
AcquireSRWLockExclusive(&handle->lock); |
| 197 |
(void)QueryPerformanceCounter(&stop); /*measure acquire time*/ |
| 198 |
|
| 199 |
(void)InterlockedIncrement64(&handle->nCalls_AcquireSRWLockExclusive); |
| 200 |
(void)InterlockedAdd64(&handle->totalCounts_AcquireSRWLockExclusive, stop.QuadPart - start.QuadPart); |
| 201 |
|
| 202 |
/*piggyback on the lock itself to print the statistics "until now" - note - does not include the current Lock call*/ |
| 203 |
/*Codes_SRS_SRW_LOCK_02_025: [ If do_statistics is true and if the timer created has recorded more than TIME_BETWEEN_STATISTICS_LOG seconds then statistics will be logged and the timer shall be started again. ]*/ |
| 204 |
if (timer_get_elapsed(handle->timer) >= TIME_BETWEEN_STATISTICS_LOG) |
| 205 |
{ |
| 206 |
timer_start(handle->timer); |
| 207 |
LogStatistics(handle, "periodic logging almost every " MU_TOSTRING(TIME_BETWEEN_STATISTICS_LOG) " seconds"); |
| 208 |
} |
| 209 |
|
| 210 |
(void)InterlockedExchange64(&handle->lastCount_AcquireSRWLockExclusive, stop.QuadPart); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
void srw_lock_release_exclusive(SRW_LOCK_HANDLE handle) |
| 216 |
{ |
| 217 |
/*Codes_SRS_SRW_LOCK_02_009: [ If handle is NULL then srw_lock_release_exclusive shall return. ]*/ |
| 218 |
if (handle == NULL) |
| 219 |
{ |
| 220 |
LogError("invalid argument SRW_LOCK_HANDLE handle=%p", handle); |
| 221 |
} |
| 222 |
else |
| 223 |
{ |
| 224 |
if (!handle->doStatistics) |
| 225 |
{ |
| 226 |
#ifdef _MSC_VER |
| 227 |
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle->lock' before calling function 'ReleaseSRWLockExclusive'. |
| 228 |
#endif |
| 229 |
ReleaseSRWLockExclusive(&handle->lock); |
| 230 |
#ifdef _MSC_VER |
| 231 |
#pragma warning (default:26110) |
| 232 |
#endif |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
LARGE_INTEGER start, stop; |
| 237 |
|
| 238 |
LARGE_INTEGER lastAcquireCount_exclusive_copy; |
| 239 |
lastAcquireCount_exclusive_copy.QuadPart = InterlockedAdd64(&handle->lastCount_AcquireSRWLockExclusive, 0); |
| 240 |
|
| 241 |
(void)QueryPerformanceCounter(&start); |
| 242 |
/*Codes_SRS_SRW_LOCK_02_010: [ srw_lock_release_exclusive shall call ReleaseSRWLockExclusive. ]*/ |
| 243 |
#ifdef _MSC_VER |
| 244 |
#pragma warning(disable:26110) // Warning C26110: Caller failing to hold lock 'handle->lock' before calling function 'ReleaseSRWLockExclusive'. |
| 245 |
#endif |
| 246 |
ReleaseSRWLockExclusive(&handle->lock); |
| 247 |
#ifdef _MSC_VER |
| 248 |
#pragma warning (default:26110) |
| 249 |
#endif |
| 250 |
(void)QueryPerformanceCounter(&stop); /*measure release time*/ |
| 251 |
|
| 252 |
(void)InterlockedAdd64(&handle->totalCounts_ReleaseSRWLockExclusive, (stop.QuadPart - start.QuadPart)); |
| 253 |
(void)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockExclusive_and_ReleaseSRWLockExclusive, (stop.QuadPart - lastAcquireCount_exclusive_copy.QuadPart)); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
void srw_lock_acquire_shared(SRW_LOCK_HANDLE handle) |
| 259 |
{ |
| 260 |
/*Codes_SRS_SRW_LOCK_02_017: [ If handle is NULL then srw_lock_acquire_shared shall return. ]*/ |
| 261 |
if (handle == NULL) |
| 262 |
{ |
| 263 |
LogError("invalid argument SRW_LOCK_HANDLE handle=%p", handle); |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
if (!handle->doStatistics) |
| 268 |
{ |
| 269 |
AcquireSRWLockShared(&handle->lock); |
| 270 |
} |
| 271 |
else |
| 272 |
{ |
| 273 |
LARGE_INTEGER start, stop; |
| 274 |
|
| 275 |
(void)QueryPerformanceCounter(&start); |
| 276 |
/*Codes_SRS_SRW_LOCK_02_018: [ srw_lock_acquire_shared shall call AcquireSRWLockShared. ]*/ |
| 277 |
AcquireSRWLockShared(&handle->lock); |
| 278 |
(void)QueryPerformanceCounter(&stop); /*measure acquire time*/ |
| 279 |
|
| 280 |
/*there is one more shared reader*/ |
| 281 |
/*is this the first reader?*/ |
| 282 |
if (InterlockedIncrement(&handle->nSharedReaders) == 1) |
| 283 |
{ |
| 284 |
(void)InterlockedExchange64(&handle->firstCount_AcquireSRWLockShared, stop.QuadPart); |
| 285 |
} |
| 286 |
|
| 287 |
(void)InterlockedIncrement64(&handle->nCalls_AcquireSRWLockShared); |
| 288 |
(void)InterlockedAdd64(&handle->totalCounts_AcquireSRWLockShared, stop.QuadPart - start.QuadPart); |
| 289 |
|
| 290 |
/*Codes_SRS_SRW_LOCK_02_026: [ If do_statistics is true and the timer created has recorded more than TIME_BETWEEN_STATISTICS_LOG seconds then statistics will be logged and the timer shall be started again. ]*/ |
| 291 |
if (timer_get_elapsed(handle->timer) >= TIME_BETWEEN_STATISTICS_LOG) |
| 292 |
{ |
| 293 |
timer_start(handle->timer); |
| 294 |
LogStatistics(handle, "periodic logging almost every " MU_TOSTRING(TIME_BETWEEN_STATISTICS_LOG) " seconds"); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
void srw_lock_release_shared(SRW_LOCK_HANDLE handle) |
| 301 |
{ |
| 302 |
/*Codes_SRS_SRW_LOCK_02_020: [ If handle is NULL then srw_lock_release_shared shall return. ]*/ |
| 303 |
if (handle == NULL) |
| 304 |
{ |
| 305 |
LogError("invalid argument SRW_LOCK_HANDLE handle=%p", handle); |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
if (!handle->doStatistics) |
| 310 |
{ |
| 311 |
ReleaseSRWLockShared(&handle->lock); |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
LARGE_INTEGER start, stop; |
| 316 |
|
| 317 |
LARGE_INTEGER firstCount_AcquireSRWLockShared_copy; |
| 318 |
firstCount_AcquireSRWLockShared_copy.QuadPart = InterlockedAdd64(&handle->firstCount_AcquireSRWLockShared, 0); |
| 319 |
|
| 320 |
(void)QueryPerformanceCounter(&start); |
| 321 |
/*Codes_SRS_SRW_LOCK_02_021: [ srw_lock_release_shared shall call ReleaseSRWLockShared. ]*/ |
| 322 |
ReleaseSRWLockShared(&handle->lock); |
| 323 |
(void)QueryPerformanceCounter(&stop); /*measure release time*/ |
| 324 |
|
| 325 |
if (InterlockedDecrement(&handle->nSharedReaders) == 0) |
| 326 |
{ |
| 327 |
(void)InterlockedAdd64(&handle->totalCountsBetween_AcquireSRWLockShared_and_ReleaseSRWLockShared, (stop.QuadPart - firstCount_AcquireSRWLockShared_copy.QuadPart)); |
| 328 |
} |
| 329 |
|
| 330 |
(void)InterlockedAdd64(&handle->totalCounts_ReleaseSRWLockShared, (stop.QuadPart - start.QuadPart)); |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
void srw_lock_destroy(SRW_LOCK_HANDLE handle) |
| 336 |
{ |
| 337 |
/*Codes_SRS_SRW_LOCK_02_011: [ If handle is NULL then srw_lock_destroy shall return. ]*/ |
| 338 |
if (handle == NULL) |
| 339 |
{ |
| 340 |
LogError("invalid arguments SRW_LOCK_HANDLE handle=%p", handle); |
| 341 |
} |
| 342 |
else |
| 343 |
{ |
| 344 |
if (handle->doStatistics) |
| 345 |
{ |
| 346 |
LogStatistics(handle, "srw_lock_destroy was called"); |
| 347 |
timer_destroy(handle->timer); |
| 348 |
free(handle->lockName); |
| 349 |
} |
| 350 |
|
| 351 |
/*Codes_SRS_SRW_LOCK_02_012: [ srw_lock_destroy shall free all used resources. ]*/ |
| 352 |
/*there's no deinit for a SRW, since it is a static construct*/ |
| 353 |
free(handle); |
| 354 |
} |
| 355 |
} |
| 356 |
|