[708ae38] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // cluster.hfa -- file that includes helpers for subsystem that need cluster wide support |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Tue Mar 15 16:40:12 2022 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | |
---|
[8f01ad71] | 16 | #pragma once |
---|
| 17 | |
---|
[78a580d] | 18 | #include "device/cpu.hfa" |
---|
[708ae38] | 19 | #include "kernel/private.hfa" |
---|
[31c967b] | 20 | #include "math.hfa" |
---|
[708ae38] | 21 | |
---|
[b035046] | 22 | #include <limits.h> |
---|
[78a580d] | 23 | |
---|
[da77728] | 24 | #include "clock.hfa" |
---|
| 25 | |
---|
[31c967b] | 26 | #if defined(READYQ_USE_LINEAR_AVG) |
---|
| 27 | |
---|
| 28 | // no conversion needed in this case |
---|
| 29 | static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) { return intsc; } |
---|
| 30 | |
---|
| 31 | // warn normally all ints |
---|
| 32 | #define warn_large_before warnf( !strict || old_avg < 33_000_000_000, "Suspiciously large previous average: %'llu (%llx), %'ldms \n", old_avg, old_avg, program()`ms ) |
---|
| 33 | #define warn_large_after warnf( !strict || ret < 33_000_000_000, "Suspiciously large new average after %'ldms cputime: %'llu (%llx) from %'llu-%'llu (%'llu, %'llu) and %'llu\n", program()`ms, ret, ret, currtsc, intsc, new_val, new_val / 1000000, old_avg ) |
---|
| 34 | |
---|
| 35 | // 8X linear factor is just 8 * x |
---|
| 36 | #define AVG_FACTOR( x ) (8 * (x)) |
---|
| 37 | |
---|
| 38 | #elif defined(READYQ_USE_LOGDBL_AVG) |
---|
| 39 | |
---|
| 40 | // convert to log2 scale but using double |
---|
[5f6b2c2] | 41 | static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) { if(unlikely(0 == intsc)) return 0.0; else return log2(intsc); } |
---|
[31c967b] | 42 | |
---|
| 43 | #define warn_large_before warnf( !strict || old_avg < 35.0, "Suspiciously large previous average: %'lf, %'ldms \n", old_avg, program()`ms ) |
---|
| 44 | #define warn_large_after warnf( !strict || ret < 35.3, "Suspiciously large new average after %'ldms cputime: %'lf from %'llu-%'llu (%'llu, %'llu) and %'lf\n", program()`ms, ret, currtsc, intsc, new_val, new_val / 1000000, old_avg ); \ |
---|
| 45 | verify(ret >= 0) |
---|
| 46 | |
---|
| 47 | // 8X factor in logscale is log2(8X) = log2(8) + log2(X) = 3 + log2(X) |
---|
| 48 | #define AVG_FACTOR( x ) (3.0 + (x)) |
---|
| 49 | |
---|
| 50 | // we need to overload the __atomic_load_n because they don't support double |
---|
| 51 | static inline double __atomic_load_n(volatile double * ptr, int mem) { |
---|
| 52 | volatile uint64_t * uptr = (volatile uint64_t *)ptr; |
---|
| 53 | _Static_assert(sizeof(*uptr) == sizeof(*ptr)); |
---|
| 54 | uint64_t ret = 0; |
---|
| 55 | ret = __atomic_load_n(uptr, mem); |
---|
| 56 | uint64_t *rp = &ret; |
---|
| 57 | double ret = *(volatile double *)rp; |
---|
| 58 | /* paranoid */ verify( ret == 0 || ret > 3e-100 ); |
---|
| 59 | return ret; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | // we need to overload the __atomic_store_n because they don't support double |
---|
| 63 | static inline void __atomic_store_n(volatile double * ptr, double val, int mem) { |
---|
| 64 | /* paranoid */ verify( val == 0 || val > 3e-100 ); |
---|
| 65 | volatile uint64_t * uptr = (volatile uint64_t *)ptr; |
---|
| 66 | _Static_assert(sizeof(*uptr) == sizeof(*ptr)); |
---|
| 67 | uint64_t * valp = (uint64_t *)&val; |
---|
| 68 | __atomic_store_n(uptr, *valp, mem); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | #elif defined(READYQ_USE_LOGDBL_AVG) |
---|
| 72 | |
---|
| 73 | //convert to log2 scale but with fix point u32.32 values |
---|
| 74 | static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) { return ulog2_32_32(tsc); } |
---|
| 75 | |
---|
| 76 | // 8X factor, +3 in logscale (see above) is + 0x3.00000000 |
---|
| 77 | #define AVG_FACTOR( x ) (0x3_00000000ull + (x)) |
---|
| 78 | |
---|
| 79 | #else |
---|
| 80 | #error must pick a scheme for averaging |
---|
| 81 | #endif |
---|
| 82 | |
---|
[708ae38] | 83 | //----------------------------------------------------------------------- |
---|
| 84 | // Calc moving average based on existing average, before and current time. |
---|
[31c967b] | 85 | static inline __readyQ_avg_t moving_average(unsigned long long currtsc, unsigned long long intsc, __readyQ_avg_t old_avg, bool strict) { |
---|
[5f9c42b] | 86 | (void)strict; // disable the warning around the fact this is unused in release. |
---|
[31c967b] | 87 | /* paranoid */ warn_large_before; |
---|
[708ae38] | 88 | |
---|
[31c967b] | 89 | const unsigned long long new_val = currtsc > intsc ? currtsc - intsc : 0; |
---|
| 90 | const __readyQ_avg_t total_weight = 16; |
---|
[d1cd4c4] | 91 | const __readyQ_avg_t new_weight = 12; |
---|
[31c967b] | 92 | const __readyQ_avg_t old_weight = total_weight - new_weight; |
---|
| 93 | const __readyQ_avg_t ret = ((new_weight * __to_readyQ_avg(new_val)) + (old_weight * old_avg)) / total_weight; |
---|
[da77728] | 94 | |
---|
[31c967b] | 95 | /* paranoid */ warn_large_after; |
---|
[708ae38] | 96 | return ret; |
---|
| 97 | } |
---|
| 98 | |
---|
[5f9c42b] | 99 | static inline void touch_tsc(__timestamp_t * tscs, size_t idx, unsigned long long ts_prev, unsigned long long ts_next, bool strict) { |
---|
[b035046] | 100 | if (ts_next == ULLONG_MAX) return; |
---|
[78a580d] | 101 | unsigned long long now = rdtscl(); |
---|
[31c967b] | 102 | __readyQ_avg_t pma = __atomic_load_n(&tscs[ idx ].t.ma, __ATOMIC_RELAXED); |
---|
[2af1943] | 103 | __atomic_store_n(&tscs[ idx ].t.tv, ts_next, __ATOMIC_RELAXED); |
---|
[5f9c42b] | 104 | __atomic_store_n(&tscs[ idx ].t.ma, moving_average(now, ts_prev, pma, strict), __ATOMIC_RELAXED); |
---|
[78a580d] | 105 | } |
---|
| 106 | |
---|
[708ae38] | 107 | //----------------------------------------------------------------------- |
---|
| 108 | // Calc age a timestamp should be before needing help. |
---|
| 109 | forall(Data_t * | { unsigned long long ts(Data_t & this); }) |
---|
[31c967b] | 110 | static inline __readyQ_avg_t calc_cutoff( |
---|
[708ae38] | 111 | const unsigned long long ctsc, |
---|
[4479890] | 112 | unsigned procid, |
---|
[708ae38] | 113 | size_t count, |
---|
| 114 | Data_t * data, |
---|
| 115 | __timestamp_t * tscs, |
---|
[5f9c42b] | 116 | const unsigned shard_factor, |
---|
| 117 | bool strict |
---|
[708ae38] | 118 | ) { |
---|
[4479890] | 119 | unsigned start = procid; |
---|
[31c967b] | 120 | __readyQ_avg_t max = 0; |
---|
[708ae38] | 121 | for(i; shard_factor) { |
---|
| 122 | unsigned long long ptsc = ts(data[start + i]); |
---|
[b035046] | 123 | if(ptsc != ULLONG_MAX) { |
---|
[708ae38] | 124 | /* paranoid */ verify( start + i < count ); |
---|
[31c967b] | 125 | __readyQ_avg_t avg = moving_average(ctsc, ptsc, tscs[start + i].t.ma, strict); |
---|
| 126 | if(avg > max) max = avg; |
---|
[708ae38] | 127 | } |
---|
| 128 | } |
---|
[31c967b] | 129 | return AVG_FACTOR( max ); |
---|
[708ae38] | 130 | } |
---|
| 131 | |
---|
| 132 | static inline unsigned cache_id(struct cluster * cltr, unsigned idx) with (cltr->sched) { |
---|
| 133 | // Figure out the current cpu and make sure it is valid |
---|
| 134 | const int cpu = __kernel_getcpu(); |
---|
| 135 | /* paranoid */ verify(cpu >= 0); |
---|
| 136 | /* paranoid */ verify(cpu < cpu_info.hthrd_count); |
---|
| 137 | unsigned this_cache = cpu_info.llc_map[cpu].cache; |
---|
| 138 | |
---|
| 139 | // Super important: don't write the same value over and over again |
---|
| 140 | // We want to maximise our chances that his particular values stays in cache |
---|
| 141 | if(caches[idx].id != this_cache) |
---|
| 142 | __atomic_store_n(&caches[idx].id, this_cache, __ATOMIC_RELAXED); |
---|
| 143 | |
---|
| 144 | return this_cache; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | static struct { |
---|
| 148 | const unsigned readyq; |
---|
[adb3ea1] | 149 | const unsigned io; |
---|
| 150 | } __shard_factor = { 2, 1 }; |
---|
[708ae38] | 151 | |
---|
| 152 | // Local Variables: // |
---|
| 153 | // mode: c // |
---|
| 154 | // tab-width: 4 // |
---|
| 155 | // End: // |
---|