Files
hakmem/core/box/tiny_class_policy_box.c

95 lines
3.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// tiny_class_policy_box.c - Initialization of per-class Tiny policy table
#include "tiny_class_policy_box.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
TinyClassPolicy g_tiny_class_policy[TINY_NUM_CLASSES];
static _Atomic int g_tiny_class_policy_init_done = 0;
static _Atomic int g_tiny_class_policy_logged = 0;
static inline TinyClassPolicy tiny_class_policy_default_entry(void) {
TinyClassPolicy p = {0};
p.page_box_enabled = 0;
p.warm_enabled = 0;
p.warm_cap = 0;
return p;
}
static void tiny_class_policy_set_legacy(void) {
TinyClassPolicy def = tiny_class_policy_default_entry();
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
g_tiny_class_policy[i] = def;
}
// legacy: Page Box は C5C7、Warm は全クラス ONC0C4 は控えめ cap
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
g_tiny_class_policy[i].warm_enabled = 1;
g_tiny_class_policy[i].warm_cap = (i < 5) ? 4 : 8;
}
for (int i = 5; i < TINY_NUM_CLASSES; i++) {
g_tiny_class_policy[i].page_box_enabled = 1;
}
}
static void tiny_class_policy_set_c5_7_only(void) {
TinyClassPolicy def = tiny_class_policy_default_entry();
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
g_tiny_class_policy[i] = def;
}
for (int i = 5; i < TINY_NUM_CLASSES; i++) {
g_tiny_class_policy[i].page_box_enabled = 1;
g_tiny_class_policy[i].warm_enabled = 1;
g_tiny_class_policy[i].warm_cap = 8;
}
}
static void tiny_class_policy_set_tinyplus_all(void) {
// いまは legacy と同じ挙動でエントリを用意しておく。
tiny_class_policy_set_legacy();
}
static const char* tiny_class_policy_set_profile(const char* profile) {
if (profile == NULL || *profile == '\0' || strcasecmp(profile, "legacy") == 0) {
tiny_class_policy_set_legacy();
return "legacy";
} else if (strcasecmp(profile, "c5_7_only") == 0) {
tiny_class_policy_set_c5_7_only();
return "c5_7_only";
} else if (strcasecmp(profile, "tinyplus_all") == 0) {
tiny_class_policy_set_tinyplus_all();
return "tinyplus_all";
} else {
// 不明な値は安全側で legacy にフォールバック。
tiny_class_policy_set_legacy();
return "legacy";
}
}
void tiny_class_policy_init_once(void) {
if (atomic_load_explicit(&g_tiny_class_policy_init_done, memory_order_acquire)) {
return;
}
const char* profile = getenv("HAKMEM_TINY_POLICY_PROFILE");
const char* active_profile = tiny_class_policy_set_profile(profile);
// 1-shot ダンプでポリシーの内容を可視化(デバッグ用)
if (atomic_exchange_explicit(&g_tiny_class_policy_logged, 1, memory_order_acq_rel) == 0) {
fprintf(stderr, "[POLICY_INIT] profile=%s\n", active_profile);
for (int cls = 0; cls < TINY_NUM_CLASSES; cls++) {
TinyClassPolicy* p = &g_tiny_class_policy[cls];
fprintf(stderr,
" C%d: page=%u warm=%u cap=%u\n",
cls,
p->page_box_enabled,
p->warm_enabled,
p->warm_cap);
}
}
atomic_store_explicit(&g_tiny_class_policy_init_done, 1, memory_order_release);
}