Example #1
0
def test():
    L2 = Cache(size=0x100000, associativity=16)
    L2.accessTime = 8
    L2.tagTime = 3
    L2.accessEnergy = 0.137789
    L2.tagEnergy = 0.00538836
    L1 = Cache(size=0x8000, child=L2)
    nLines = -1
    if len(sys.argv) > 1:
        nLines = int(sys.argv[1])
    skip = 0
    if len(sys.argv) > 2:
        skip = int(sys.argv[2])
    warmup = 0
    if len(sys.argv) > 3:
        warmup = int(sys.argv[3])
    for i, line in enumerate(sys.stdin):
        if i >= skip:
            addr = int(line.split(' ')[-3])
            L1.access(addr, 'Write' in line, i >= skip + warmup)
        if i + 1 == skip + warmup + nLines and nLines != -1:
            break
    print("N:", L1.counter - warmup)
    print("L1 hit:  %d (%0.3f)" % (L1.hit, L1.hit /
                                   (L1.counter - warmup) * 100))
    print("L1 miss: %d (%0.3f)" % (L1.miss, L1.miss /
                                   (L1.counter - warmup) * 100))
    print("L2 hit:  %d (%0.3f)" % (L2.hit, L2.hit /
                                   (L1.counter - warmup) * 100))
    print("L2 miss: %d (%0.3f)" % (L2.miss, L2.miss /
                                   (L1.counter - warmup) * 100))
    print("Time L1: %d, L2: %d, total: %d" %
          (L1.cycles, L2.cycles, L1.cycles + L2.cycles))
    print("Energy L1: %0.3f, L2: %0.3f, total: %0.3f" %
          (L1.energy, L2.energy, L1.energy + L2.energy))
Example #2
0
import sys
from cache import Cache

traceFile = open(sys.argv[1], "r")
c = Cache(16, 1048576, 64, "LRU")
hit = 0
miss = 0
total = 0

for line in traceFile:
    total += 1
    fields = line.split(" ")
    mode = fields[1]
    address = fields[2]
    result = c.access(mode, address)
    if result is True:
        hit += 1
    else:
        miss += 1

miss_rate = float(miss) / float(total)
miss_rate_percentage = miss_rate * 100
print ('Cache miss rate: %.2f%%'%miss_rate_percentage )







Example #3
0
def run(commands):
  input_file = constants.INPUT_FOLDER_PATH + commands.input_file_label
  # Parse trace file to programmable.
  traces = []
  with open(input_file, 'r') as trace_file:
    traces = trace_parser.parse(trace_file, constants.BIT_SIZE)

  # Config for L1 I/D, L2 (Fixed)
  config_L1_inst = CacheConfig(
    C=L1_CACHE_SIZE, L=BLOCK_SIZE, K=1, N=512,
    BIT_SIZE=constants.BIT_SIZE,
    input_label=commands.input_file_label,
    HIT_TIME=4,
    MISS_PENALTY=16,
  )
  config_L1_data = CacheConfig(
    C=L1_CACHE_SIZE, L=BLOCK_SIZE, K=1, N=512,
    BIT_SIZE=constants.BIT_SIZE,
    input_label=commands.input_file_label,
    HIT_TIME=4,
    MISS_PENALTY=16,
  )
  config_L2 = CacheConfig(
    C=L2_CACHE_SIZE, L=BLOCK_SIZE, K=8, N=512,
    BIT_SIZE=constants.BIT_SIZE,
    input_label=commands.input_file_label,
    HIT_TIME=16,
    MISS_PENALTY=32,
  )

  raw_configs_dicts_L3 = {}
  with open('configs/project.json', 'r') as raw_config_file:
    raw_configs_dicts_L3 = json.load(raw_config_file)
  raw_configs_L3 = [
    {
      'C': L3_CACHE_SIZE,
      'L': BLOCK_SIZE,
      'K': raw_config['K'],
      'N': raw_config['N'],
      'INST_PREFETCHER': raw_config['INST_PREFETCHER'],
      'DATA_PREFETCHER': raw_config['DATA_PREFETCHER'],
      'REPLACEMENT': raw_config['REPLACEMENT'],
    }
    for raw_config in cartesian_dict_product(raw_configs_dicts_L3)
    if check_raw_config({
      'C': L3_CACHE_SIZE,
      'L': BLOCK_SIZE,
      'K': raw_config['K'],
      'N': raw_config['N'],
    })
  ]
  validate_raw_configs(raw_configs_L3)
  del raw_configs_dicts_L3

  for raw_config_L3 in raw_configs_L3:
    # Config for L3 (Dynamic)
    config_L3 = CacheConfig(
      C=raw_config_L3['C'],
      L=raw_config_L3['L'],
      K=raw_config_L3['K'],
      N=raw_config_L3['N'],
      BIT_SIZE=constants.BIT_SIZE,
      input_label=commands.input_file_label,
      HIT_TIME=32,
      MISS_PENALTY=120,
      # inst_prefetcher=constants.PREFETCHER_TYPE['STREAM_BUFFER'],
      inst_prefetcher=constants.PREFETCHER_TYPE[raw_config_L3['INST_PREFETCHER']],
      # data_prefetcher=constants.PREFETCHER_TYPE['WRITE_BUFFER'],
      data_prefetcher=constants.PREFETCHER_TYPE[raw_config_L3['DATA_PREFETCHER']],
      replacement_policy=constants.REPLACEMENT_POLICY_TYPE[raw_config_L3['REPLACEMENT']],
    )

    # TODO(totorody): Implements to run caches
    cache_L1_inst = Cache(config_L1_inst)
    cache_L1_data = Cache(config_L1_data)
    cache_L2 = Cache(config_L2)
    cache_L3 = Cache(config_L3)

    cache_L1_inst.set_low_cache(cache_L2)
    cache_L1_data.set_low_cache(cache_L2)
    cache_L2.set_low_cache(cache_L3)

    print('Start to run caching...')
    index = 0
    for trace in traces:
      if index % 10000 == 0:
        print('trace #:', index)
      index += 1
      if trace['type'] not in constants.ACCESS_TYPE.values():
        continue
      if trace['type'] == constants.ACCESS_TYPE['INST_READ']:
        cache_L1_inst.access(trace)
      else:
        cache_L1_data.access(trace)

    print('Prints cache simulation results...')
    inst_result = cache_L1_inst.get_result('L1-Inst')
    data_result = cache_L1_data.get_result('L1-Data')
    L2_result = cache_L2.get_result('L2')
    L3_result = cache_L3.get_result('L3')

    output_file = constants.OUTPUT_FOLDER_PATH \
        + populate_output_file_label(config_L3)
    with open(output_file, 'w+') as csv_file:
      csv_manager = CsvManager(csv_file, inst_result.keys())
      csv_manager.write_row(inst_result)
      csv_manager.write_row(data_result)
      csv_manager.write_row(L2_result)
      csv_manager.write_row(L3_result)