Skip to content

Commit

Permalink
Add frequency profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe661234 committed Nov 18, 2023
1 parent 2f08fe6 commit 2ed630c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
52 changes: 49 additions & 3 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef MIR
Expand All @@ -18,7 +20,7 @@
/* THRESHOLD is set to identify hot spots. Once the frequency of use for a block
* exceeds the THRESHOLD, the JIT compiler flow is triggered.
*/
#define THRESHOLD 32768
#define THRESHOLD 4096
#if RV32_HAS(JIT)
#ifndef MIR
#define CODE_CACHE_SIZE (64 * 1024 * 1024)
Expand Down Expand Up @@ -544,8 +546,10 @@ void *cache_put(cache_t *cache, uint32_t key, void *value)
return delete_value;
}

uint64_t freq[4097] = {0};
void cache_free(cache_t *cache, void (*callback)(void *))
{
uint64_t total_times = 0;
#if RV32_HAS(ARC)
for (int i = 0; i < N_CACHE_LIST_TYPES; i++) {
arc_entry_t *entry, *safe;
Expand All @@ -567,12 +571,54 @@ void cache_free(cache_t *cache, void (*callback)(void *))
lfu_entry_t)
#endif
#endif
callback(entry->value);
{
freq[entry->frequency]++;
total_times += entry->frequency;
}
}
mpool_destroy(cache_mp);
free(cache->map->ht_list_head);
free(cache->map);
free(cache);
for (int i = 0; i <= 4096; i++) {
if (freq[i]) {
printf("%4d | %10ld\n", i, freq[i]);
}
}
}

uint32_t cache_freq(struct cache *cache, uint32_t key)
{
if (!cache->capacity ||
hlist_empty(&cache->map->ht_list_head[cache_hash(key)]))
return 0;
#if RV32_HAS(ARC)
arc_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &cache->map->ht_list_head[cache_hash(key)],
ht_list)
#else
hlist_for_each_entry (entry, &cache->map->ht_list_head[cache_hash(key)],
ht_list, arc_entry_t)
#endif
{
if (entry->key == key)
return entry->frequency;
}
#else
lfu_entry_t *entry = NULL;
#ifdef __HAVE_TYPEOF
hlist_for_each_entry (entry, &cache->map->ht_list_head[cache_hash(key)],
ht_list)
#else
hlist_for_each_entry (entry, &cache->map->ht_list_head[cache_hash(key)],
ht_list, lfu_entry_t)
#endif
{
if (entry->key == key)
return entry->frequency;
}
#endif
return 0;
}

#if RV32_HAS(JIT)
Expand Down
2 changes: 2 additions & 0 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ uint8_t *code_cache_add(struct cache *cache,
uint64_t align);
#endif
#endif

uint32_t cache_freq(struct cache *cache, uint32_t key);
3 changes: 2 additions & 1 deletion src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,13 @@ static void gen_fuse7(riscv_t *rv UNUSED, rv_insn_t *ir, char *gencode)
}
#undef RVOP

uint64_t count = 0;
static void trace_ebb(riscv_t *rv, char *gencode, rv_insn_t *ir, set_t *set)
{
while (1) {
if (set_add(set, ir->pc))
dispatch_table[ir->opcode](rv, ir, gencode);

count++;
if (!ir->next)
break;
ir = ir->next;
Expand Down
8 changes: 4 additions & 4 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ void rv_delete(riscv_t *rv)
#if !RV32_HAS(JIT)
block_map_destroy(rv);
#else
// cache_free(rv->block_cache, NULL);
// #ifdef MIR
// cache_free(rv->code_cache, NULL);
// #endif
cache_free(rv->block_cache, NULL);
#ifdef MIR
cache_free(rv->code_cache, NULL);
#endif
#endif
free(rv);
}
Expand Down
1 change: 1 addition & 0 deletions src/riscv_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ typedef struct block {
struct block *predict; /**< block prediction */

rv_insn_t *ir_head, *ir_tail; /**< the first and last ir for this block */
bool backward;
#if RV32_HAS(JIT)
bool hot; /**< Determine the block is hotspot or not */
#endif
Expand Down

0 comments on commit 2ed630c

Please sign in to comment.