source: libcfa/src/concurrency/kernel/cluster.hfa @ 741e22c

ADTast-experimentalpthread-emulation
Last change on this file since 741e22c was 741e22c, checked in by Thierry Delisle <tdelisle@…>, 20 months ago

Fixed potential false sharing on ready-schedule mutate lock

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