[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"
|
---|
| 20 |
|
---|
[b035046] | 21 | #include <limits.h>
|
---|
[78a580d] | 22 |
|
---|
[da77728] | 23 | #include "clock.hfa"
|
---|
| 24 |
|
---|
[708ae38] | 25 | //-----------------------------------------------------------------------
|
---|
| 26 | // Calc moving average based on existing average, before and current time.
|
---|
[5f9c42b] | 27 | static inline unsigned long long moving_average(unsigned long long currtsc, unsigned long long instsc, unsigned long long old_avg, bool strict) {
|
---|
| 28 | (void)strict; // disable the warning around the fact this is unused in release.
|
---|
[33e4968e] | 29 | /* paranoid */ warnf( !strict || old_avg < 33_000_000_000, "Suspiciously large previous average: %'llu (%llx), %'ldms \n", old_avg, old_avg, program()`ms );
|
---|
[708ae38] | 30 |
|
---|
| 31 | const unsigned long long new_val = currtsc > instsc ? currtsc - instsc : 0;
|
---|
| 32 | const unsigned long long total_weight = 16;
|
---|
| 33 | const unsigned long long new_weight = 4;
|
---|
| 34 | const unsigned long long old_weight = total_weight - new_weight;
|
---|
| 35 | const unsigned long long ret = ((new_weight * new_val) + (old_weight * old_avg)) / total_weight;
|
---|
[da77728] | 36 |
|
---|
[33e4968e] | 37 | /* paranoid */ 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, instsc, new_val, new_val / 1000000, old_avg );
|
---|
[708ae38] | 38 | return ret;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[5f9c42b] | 41 | static inline void touch_tsc(__timestamp_t * tscs, size_t idx, unsigned long long ts_prev, unsigned long long ts_next, bool strict) {
|
---|
[b035046] | 42 | if (ts_next == ULLONG_MAX) return;
|
---|
[78a580d] | 43 | unsigned long long now = rdtscl();
|
---|
[2af1943] | 44 | unsigned long long pma = __atomic_load_n(&tscs[ idx ].t.ma, __ATOMIC_RELAXED);
|
---|
| 45 | __atomic_store_n(&tscs[ idx ].t.tv, ts_next, __ATOMIC_RELAXED);
|
---|
[5f9c42b] | 46 | __atomic_store_n(&tscs[ idx ].t.ma, moving_average(now, ts_prev, pma, strict), __ATOMIC_RELAXED);
|
---|
[78a580d] | 47 | }
|
---|
| 48 |
|
---|
[708ae38] | 49 | //-----------------------------------------------------------------------
|
---|
| 50 | // Calc age a timestamp should be before needing help.
|
---|
| 51 | forall(Data_t * | { unsigned long long ts(Data_t & this); })
|
---|
| 52 | static inline unsigned long long calc_cutoff(
|
---|
| 53 | const unsigned long long ctsc,
|
---|
[4479890] | 54 | unsigned procid,
|
---|
[708ae38] | 55 | size_t count,
|
---|
| 56 | Data_t * data,
|
---|
| 57 | __timestamp_t * tscs,
|
---|
[5f9c42b] | 58 | const unsigned shard_factor,
|
---|
| 59 | bool strict
|
---|
[708ae38] | 60 | ) {
|
---|
[4479890] | 61 | unsigned start = procid;
|
---|
[708ae38] | 62 | unsigned long long max = 0;
|
---|
| 63 | for(i; shard_factor) {
|
---|
| 64 | unsigned long long ptsc = ts(data[start + i]);
|
---|
[b035046] | 65 | if(ptsc != ULLONG_MAX) {
|
---|
[708ae38] | 66 | /* paranoid */ verify( start + i < count );
|
---|
[5f9c42b] | 67 | unsigned long long tsc = moving_average(ctsc, ptsc, tscs[start + i].t.ma, strict);
|
---|
[708ae38] | 68 | if(tsc > max) max = tsc;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
[aa144c5a] | 71 | return 8 * max;
|
---|
[708ae38] | 72 | }
|
---|
| 73 |
|
---|
| 74 | static inline unsigned cache_id(struct cluster * cltr, unsigned idx) with (cltr->sched) {
|
---|
| 75 | // Figure out the current cpu and make sure it is valid
|
---|
| 76 | const int cpu = __kernel_getcpu();
|
---|
| 77 | /* paranoid */ verify(cpu >= 0);
|
---|
| 78 | /* paranoid */ verify(cpu < cpu_info.hthrd_count);
|
---|
| 79 | unsigned this_cache = cpu_info.llc_map[cpu].cache;
|
---|
| 80 |
|
---|
| 81 | // Super important: don't write the same value over and over again
|
---|
| 82 | // We want to maximise our chances that his particular values stays in cache
|
---|
| 83 | if(caches[idx].id != this_cache)
|
---|
| 84 | __atomic_store_n(&caches[idx].id, this_cache, __ATOMIC_RELAXED);
|
---|
| 85 |
|
---|
| 86 | return this_cache;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | static struct {
|
---|
| 90 | const unsigned readyq;
|
---|
[adb3ea1] | 91 | const unsigned io;
|
---|
| 92 | } __shard_factor = { 2, 1 };
|
---|
[708ae38] | 93 |
|
---|
| 94 | // Local Variables: //
|
---|
| 95 | // mode: c //
|
---|
| 96 | // tab-width: 4 //
|
---|
| 97 | // End: //
|
---|