Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/io/http/filecache.cfa

    r0aec496 rd9c2284  
    5656}
    5757
     58static inline [unsigned size, char unit] human_size( size_t size ) {
     59        int idx = 0;
     60        static char units [] = { ' ', 'K', 'M', 'G', 'T' };
     61        while( size >= 1024 ) {
     62                idx++;
     63                size /= 1024;
     64                if(idx >= 5) {
     65                        abort("File too large to print\n");
     66                }
     67        }
     68
     69        return [size, units[idx]];
     70}
    5871
    5972struct {
     
    6982
    7083[int fd, size_t size] get_file( * const char file, size_t len ) {
    71         uint32_t idx = murmur3_32( (const uint8_t *)file, len, options.hash_seed ) % file_cache.size;
     84        uint32_t idx = murmur3_32( (const uint8_t *)file, len, options.file_cache.hash_seed ) % file_cache.size;
    7285
    7386        for(int i = 0;; i++) {
     
    8699
    87100int put_file( cache_line & entry ) {
    88         uint32_t idx = murmur3_32( (const uint8_t *)entry.file, strlen(entry.file), options.hash_seed ) % file_cache.size;
     101        uint32_t idx = murmur3_32( (const uint8_t *)entry.file, strlen(entry.file), options.file_cache.hash_seed ) % file_cache.size;
    89102
    90103        int i = 0;
     
    101114void fill_cache( const char * path ) {
    102115        int ret;
     116        ret = chdir(path);
     117        if(ret < 0) {
     118                abort( "chdir error: (%d) %s\n", (int)errno, strerror(errno) );
     119        }
     120
    103121        size_t fcount = 0;
    104122        size_t fsize = 16;
     
    118136                raw[idx].file = strdup(fpath+2);
    119137                raw[idx].size = sb->st_size;
    120                 raw[idx].fd = open( fpath, options.open_flags );
    121                 if(raw[idx].fd < 0) {
    122                         abort( "open file error: (%d) %s\n", (int)errno, strerror(errno) );
     138                if( !options.file_cache.list ) {
     139                        raw[idx].fd = open( fpath, options.file_cache.open_flags );
     140                        if(raw[idx].fd < 0) {
     141                                abort( "open file error: (%d) %s\n", (int)errno, strerror(errno) );
     142                        }
    123143                }
    124144                return 0;
    125145        }
    126146
    127         ret = ftw(path, walk, 10);
     147        ret = ftw(".", walk, 10);
    128148        if(ret < 0) {
    129149                abort( "ftw error: (%d) %s\n", (int)errno, strerror(errno) );
     
    135155
    136156        // Step 2 create the cache
    137         file_cache.size = options.file_cache_size > 0 ? options.file_cache_size : fsize;
     157        file_cache.size = options.file_cache.size > 0 ? options.file_cache.size : fsize;
    138158        if( file_cache.size < fcount ) {
    139159                abort("File Cache too small\n");
    140160        }
    141161
    142         file_cache.entries = anew(fsize);
     162        file_cache.entries = anew(file_cache.size);
    143163
    144164        // Step 3 fill the cache
    145165        int conflicts = 0;
    146166        for(i; fcount) {
    147                 printf("Added file %s\n", raw[i].file);
    148167                conflicts += put_file( raw[i] );
    149168        }
    150169        printf("Filled cache from path \"%s\" with %zu files\n", path, fcount);
    151170        if( conflicts > 0 ) {
    152                 printf("Found %d conflicts (seed: %u)\n", conflicts, options.hash_seed);
     171                printf("Found %d conflicts (seed: %u)\n", conflicts, options.file_cache.hash_seed);
    153172                #if defined(REJECT_CONFLICTS)
    154173                        abort("Conflicts found in the cache");
     
    156175        }
    157176
     177        if(options.file_cache.list) {
     178                printf("Listing files and exiting\n");
     179                for(i; fcount) {
     180                        int s; char u;
     181                        [s, u] = human_size(raw[i].size);
     182                        printf("%4d%c - %s\n", s, u, raw[i].file);
     183                        free(raw[i].file);
     184                }
     185                free(raw);
     186                adelete(file_cache.size, file_cache.entries);
     187                exit(0);
     188        }
     189
    158190        // Step 4 clean up
    159191        free( raw );
    160192}
     193
     194[int *, int] filefds(int extra) {
     195        if(!file_cache.entries) {
     196                abort("File cache not filled!\n");
     197        }
     198
     199        return [aalloc(extra), 0];
     200}
     201
    161202
    162203void close_cache() {
Note: See TracChangeset for help on using the changeset viewer.