Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/device/cpu.cfa

    r33608cb rab4a595  
    3030        #include <fcntl.h>
    3131}
    32 
    33 #include "algorithms/range_iterator.hfa"
    3432
    3533// search a string for character 'character' but looking atmost at len
     
    137135                count++;
    138136        }
    139         int ret = iterate_dir(path, lambda);
    140         if(ret == ENOTDIR) return 0;
     137        iterate_dir(path, lambda);
    141138
    142139        /* paranoid */ verifyf(count == max + 1, "Inconsistent %s count, counted %d, but max %s was %d", prefix, count, prefix, (int)max);
     
    146143
    147144// Count number of cpus in the system
    148 static [int, const char *] count_cpus(void) {
     145static int count_cpus(void) {
    149146        const char * fpath = "/sys/devices/system/cpu/online";
    150147        int fd = open(fpath, 0, O_RDONLY);
     
    162159
    163160        const char * _;
    164         return [read_width(buff, r - 1, &_), strndup(buff, r - 1)];
     161        int cnt = read_width(buff, r - 1, &_);
     162        /* paranoid */ verify(cnt == count_prefix_dirs("/sys/devices/system/cpu", "cpu"));
     163        return cnt;
    165164}
    166165
     
    227226
    228227struct raw_cache_instance {
    229         idx_range_t range;      // A text description of the cpus covered
    230         unsigned width;         // The number of cpus covered
    231         unsigned char level;    // the cache level
     228        idx_range_t range;
     229        unsigned width;
     230        unsigned char level;
    232231        // FIXME add at least size and type
    233232};
     
    236235static void ^?{}(raw_cache_instance & this) { free(this.range);}
    237236
    238 // Returns a 2D array of instances of size [cpu count][cache levels]
    239 // where cache level doesn't include instruction caches
    240 raw_cache_instance ** build_raw_cache_table(unsigned cpus_c, idx_range_t cpus, unsigned idxs, unsigned cache_levels)
     237raw_cache_instance ** build_raw_cache_table(unsigned cpus, unsigned idxs, unsigned cache_levels)
    241238{
    242         raw_cache_instance ** raw = alloc(cpus_c, '\0'`fill);
    243 
    244         RangeIter rc = { cpus };
    245         while(moveNext(rc)) {
    246                 unsigned i = rc.com;
     239        raw_cache_instance ** raw = alloc(cpus);
     240        for(i; cpus) {
    247241                raw[i] = alloc(cache_levels);
    248242                void addcache(unsigned fidx, unsigned char level, idx_range_t range, size_t len) {
     
    269263
    270264// returns an allocate list of all the different distinct last level caches
    271 static [*llc_map_t, size_t cnt] distinct_llcs(idx_range_t cpus, unsigned llc_idx, raw_cache_instance ** raw) {
     265static [*llc_map_t, size_t cnt] distinct_llcs(unsigned cpus, unsigned llc_idx, raw_cache_instance ** raw) {
    272266        // Allocate at least one element
    273267        llc_map_t* ranges = alloc();
    274268        size_t range_cnt = 1;
    275269
    276         RangeIter rc = { cpus };
    277         __attribute__((unused)) bool ret =
    278         moveNext(rc);
    279         /* paranoid */ verify( ret );
    280         /* paranoid */ verify( rc.com >= 0 );
    281 
    282270        // Initialize with element 0
    283         ranges->raw = &raw[rc.com][llc_idx];
     271        ranges->raw = &raw[0][llc_idx];
    284272        ranges->count = 0;
    285273        ranges->start = -1u;
    286274
    287275        // Go over all other cpus
    288         CPU_LOOP: while(moveNext(rc)) {
    289                 unsigned i = rc.com;
     276        CPU_LOOP: for(i; 1~cpus) {
    290277                // Check if the range is already there
    291278                raw_cache_instance * candidate = &raw[i][llc_idx];
     
    317304}
    318305
    319 static [[]cpu_pairing_t] get_cpu_pairings(unsigned cpus_c, idx_range_t cpus, raw_cache_instance ** raw, llc_map_t * maps, size_t map_cnt) {
    320         cpu_pairing_t * pairings = alloc(cpus_c);
    321 
    322         RangeIter rc = { cpus };
    323         CPU_LOOP: while(moveNext(rc)) {
    324                 unsigned i = rc.com;
     306static [[]cpu_pairing_t] get_cpu_pairings(unsigned cpus, raw_cache_instance ** raw, llc_map_t * maps, size_t map_cnt) {
     307        cpu_pairing_t * pairings = alloc(cpus);
     308
     309        CPU_LOOP: for(i; cpus) {
    325310                pairings[i].cpu = i;
    326311                idx_range_t want = raw[i][0].range;
     
    342327extern "C" {
    343328        void __cfaabi_device_startup( void ) {
    344                 int cpus_c;
    345                 const char * cpus;
    346                 [cpus_c, cpus] = count_cpus();
    347                 #if defined(__CFA_WITH_VERIFY__)
    348                 // Verify that the mapping is self consistant.
    349                 {
    350                         RangeIter rc = { cpus };
    351                         while(moveNext(rc)) {
    352                                 unsigned i = rc.com;
    353                                 verify(cpus_c > i);
    354                         }
    355                 }
    356                 #endif
    357 
     329                int cpus = count_cpus();
    358330                int idxs = count_cache_indexes();
    359331
     
    361333                unsigned cache_levels = 0;
    362334                unsigned llc = 0;
    363                 if (idxs != 0) {
     335                {
    364336                        unsigned char prev = -1u;
    365337                        void first(unsigned idx, unsigned char level, const char * map, size_t len) {
     
    373345
    374346                // Read in raw data
    375                 raw_cache_instance ** raw = build_raw_cache_table(cpus_c, cpus, idxs, cache_levels);
     347                raw_cache_instance ** raw = build_raw_cache_table(cpus, idxs, cache_levels);
    376348
    377349                // Find number of distinct cache instances
     
    390362                                width2 += maps[i].raw->width;
    391363                        }
    392                         verify(width1 == cpus_c);
    393                         verify(width2 == cpus_c);
     364                        verify(width1 == cpus);
     365                        verify(width2 == cpus);
    394366                }
    395367                #endif
    396368
    397369                // Get mappings from cpu to cache instance
    398                 cpu_pairing_t * pairings = get_cpu_pairings(cpus_c, cpus, raw, maps, map_cnt);
     370                cpu_pairing_t * pairings = get_cpu_pairings(cpus, raw, maps, map_cnt);
    399371
    400372                // Sort by cache instance
    401                 qsort(pairings, cpus_c);
     373                qsort(pairings, cpus);
    402374
    403375                {
    404376                        unsigned it = 0;
    405                         RangeIter rc = { cpus };
    406                         while(moveNext(rc)) {
    407                                 unsigned i = rc.com;
     377                        for(i; cpus) {
    408378                                unsigned llc_id = pairings[i].id;
    409379                                if(maps[llc_id].start == -1u) {
     
    414384                                }
    415385                        }
    416                         /* paranoid */ verify(it == cpus_c);
     386                        /* paranoid */ verify(it == cpus);
    417387                }
    418388
    419389                // From the mappings build the actual cpu map we want
    420                 struct cpu_map_entry_t * entries = alloc(cpus_c);
    421                 for(i; cpus_c) { entries[i].count = 0; }
    422 
    423                 RangeIter rc = { cpus };
    424                 while(moveNext(rc)) {
    425                         unsigned i = rc.com;
     390                struct cpu_map_entry_t * entries = alloc(cpus);
     391                for(i; cpus) { entries[i].count = 0; }
     392                for(i; cpus) {
    426393                        /* paranoid */ verify(pairings[i].id < map_cnt);
    427394                        unsigned c = pairings[i].cpu;
     
    439406                free(pairings);
    440407
    441                 for(i; cpus_c) {
    442                         if( raw[i] ) for(j; cache_levels) {
     408                for(i; cpus) {
     409                        for(j; cache_levels) {
    443410                                ^(raw[i][j]){};
    444411                        }
     
    448415
    449416                cpu_info.llc_map = entries;
    450                 cpu_info.hthrd_count = cpus_c;
    451                 cpu_info.llc_count = map_cnt;
     417                cpu_info.hthrd_count = cpus;
    452418        }
    453419
Note: See TracChangeset for help on using the changeset viewer.