| 213 | } |
| 214 | |
| 215 | static void |
| 216 | background_thread_sleep(tsdn_t *tsdn, background_thread_info_t *info, |
| 217 | uint64_t interval) { |
| 218 | if (config_stats) { |
| 219 | info->tot_n_runs++; |
| 220 | } |
| 221 | info->npages_to_purge_new = 0; |
| 222 | |
| 223 | struct timeval tv; |
| 224 | /* Specific clock required by timedwait. */ |
| 225 | gettimeofday(&tv, NULL); |
| 226 | nstime_t before_sleep; |
| 227 | nstime_init2(&before_sleep, tv.tv_sec, tv.tv_usec * 1000); |
| 228 | |
| 229 | int ret; |
| 230 | if (interval == BACKGROUND_THREAD_INDEFINITE_SLEEP) { |
| 231 | assert(background_thread_indefinite_sleep(info)); |
| 232 | ret = pthread_cond_wait(&info->cond, &info->mtx.lock); |
| 233 | assert(ret == 0); |
| 234 | } else { |
| 235 | assert(interval >= BACKGROUND_THREAD_MIN_INTERVAL_NS && |
| 236 | interval <= BACKGROUND_THREAD_INDEFINITE_SLEEP); |
| 237 | /* We need malloc clock (can be different from tv). */ |
| 238 | nstime_t next_wakeup; |
| 239 | nstime_init(&next_wakeup, 0); |
| 240 | nstime_update(&next_wakeup); |
| 241 | nstime_iadd(&next_wakeup, interval); |
| 242 | assert(nstime_ns(&next_wakeup) < |
| 243 | BACKGROUND_THREAD_INDEFINITE_SLEEP); |
| 244 | background_thread_wakeup_time_set(tsdn, info, |
| 245 | nstime_ns(&next_wakeup)); |
| 246 | |
| 247 | nstime_t ts_wakeup; |
| 248 | nstime_copy(&ts_wakeup, &before_sleep); |
| 249 | nstime_iadd(&ts_wakeup, interval); |
| 250 | struct timespec ts; |
| 251 | ts.tv_sec = (size_t)nstime_sec(&ts_wakeup); |
| 252 | ts.tv_nsec = (size_t)nstime_nsec(&ts_wakeup); |
| 253 | |
| 254 | assert(!background_thread_indefinite_sleep(info)); |
| 255 | ret = pthread_cond_timedwait(&info->cond, &info->mtx.lock, &ts); |
| 256 | assert(ret == ETIMEDOUT || ret == 0); |
| 257 | background_thread_wakeup_time_set(tsdn, info, |
| 258 | BACKGROUND_THREAD_INDEFINITE_SLEEP); |
| 259 | } |
| 260 | if (config_stats) { |
| 261 | gettimeofday(&tv, NULL); |
| 262 | nstime_t after_sleep; |
| 263 | nstime_init2(&after_sleep, tv.tv_sec, tv.tv_usec * 1000); |
| 264 | if (nstime_compare(&after_sleep, &before_sleep) > 0) { |
| 265 | nstime_subtract(&after_sleep, &before_sleep); |
| 266 | nstime_add(&info->tot_sleep_time, &after_sleep); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static bool |
| 272 | background_thread_pause_check(tsdn_t *tsdn, background_thread_info_t *info) { |
no test coverage detected