옵투나 공유메모리로로
This commit is contained in:
@@ -81,7 +81,7 @@ from kis_trader.backtest.tail_param_search import (
|
||||
)
|
||||
from kis_trader.engine import tail_engine as te
|
||||
from kis_trader.engine.indicator_cache import attach_indicator_caches_to_params
|
||||
from kis_trader.utils.env import get_env_from_db, get_env_int
|
||||
from kis_trader.utils.env import get_env_bool, get_env_from_db, get_env_int
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||
logger = logging.getLogger("param_search_optuna")
|
||||
@@ -130,6 +130,7 @@ class TailSearchContext:
|
||||
grid_keys: List[str]
|
||||
ob_filter_on: bool
|
||||
cache_holder: Dict[str, Any] = field(default_factory=dict)
|
||||
shared_tick_store: Any = None # ws_ticks 공유메모리 핸들 (종료 시 unlink)
|
||||
|
||||
|
||||
def prepare_tail_search_context(
|
||||
@@ -267,6 +268,25 @@ def prepare_tail_search_context(
|
||||
elif tail_backtest_wants_tick_replay(_tick_probe):
|
||||
logger.warning("⚠️ ws_ticks 없음 — OHLC 폴백 (WS_TICK_SAVE_ENABLED 후 재탐색)")
|
||||
|
||||
# ── ws_ticks 공유메모리 (Optuna, opt-in) — dict→numpy 컬럼 shared_memory 로 RAM 절감 ──
|
||||
# 끄려면 OPTUNA_PARAM_SEARCH_SHARED_TICKS=0. numpy/shm 미지원·빌드 실패 시 자동 폴백.
|
||||
shared_tick_store = None
|
||||
if get_env_bool("OPTUNA_PARAM_SEARCH_SHARED_TICKS", True) and ticks_by_code:
|
||||
from kis_trader.backtest.shared_ticks import build_shared_ticks_view
|
||||
_view, shared_tick_store = build_shared_ticks_view(ticks_by_code, enabled=True)
|
||||
if shared_tick_store is not None:
|
||||
import atexit as _atexit
|
||||
_atexit.register(shared_tick_store.unlink) # 크래시 시 /dev/shm 누수 방지
|
||||
logger.info("📦 ws_ticks 공유메모리 ON (Optuna) — dict 사본 제거, RAM 절감")
|
||||
ticks_by_code = _view
|
||||
import gc as _gc
|
||||
_gc.collect()
|
||||
try:
|
||||
import ctypes as _ctypes
|
||||
_ctypes.CDLL("libc.so.6").malloc_trim(0)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
from kis_trader.backtest.tail_param_search import _tail_grids
|
||||
pre_grid = _tail_grids(mode)
|
||||
_ob_axes = ("max_spread_pct", "min_bid_ask_ratio")
|
||||
@@ -343,6 +363,7 @@ def prepare_tail_search_context(
|
||||
grid_keys=tail_grid_axis_keys(mode),
|
||||
ob_filter_on=ob_filter_on,
|
||||
cache_holder=cache_holder,
|
||||
shared_tick_store=shared_tick_store,
|
||||
)
|
||||
finally:
|
||||
db.close()
|
||||
@@ -424,12 +445,22 @@ def run_tail_optuna(
|
||||
study_name, n_trials, sampler_name, storage_url, n_jobs,
|
||||
)
|
||||
t0 = time.time()
|
||||
study.optimize(
|
||||
objective,
|
||||
n_trials=n_trials,
|
||||
n_jobs=n_jobs,
|
||||
show_progress_bar=show_progress,
|
||||
)
|
||||
try:
|
||||
study.optimize(
|
||||
objective,
|
||||
n_trials=n_trials,
|
||||
n_jobs=n_jobs,
|
||||
show_progress_bar=show_progress,
|
||||
)
|
||||
finally:
|
||||
# 탐색 종료(또는 예외) 시 공유메모리 즉시 해제 (atexit 는 크래시 대비 이중 안전장치).
|
||||
_store = getattr(ctx, "shared_tick_store", None)
|
||||
if _store is not None:
|
||||
try:
|
||||
_store.unlink()
|
||||
except Exception:
|
||||
pass
|
||||
ctx.shared_tick_store = None
|
||||
elapsed = time.time() - t0
|
||||
logger.info("✅ Optuna 완료 | %.1f초 | 완료 trial %d", elapsed, len(study.trials))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user