옵투나 공유메모리로로
This commit is contained in:
@@ -33,6 +33,7 @@ from kis_trader.backtest.tail_param_search import _results_dir_for_write
|
||||
from kis_trader.strategies.breakout import breakout_backtest_wants_tick_replay, breakout_entry_mode
|
||||
from kis_trader.engine.indicator_cache import attach_indicator_caches_to_params
|
||||
from kis_trader.backtest.breakout_tick_loader import load_breakout_ticks_by_code
|
||||
from kis_trader.utils.env import get_env_bool
|
||||
|
||||
logger = logging.getLogger("param_search_optuna")
|
||||
|
||||
@@ -63,6 +64,7 @@ class BreakoutSearchContext:
|
||||
start_key: str
|
||||
end_key: str
|
||||
cache_holder: Dict[str, Any] = field(default_factory=dict)
|
||||
shared_tick_store: Any = None # ws_ticks 공유메모리 핸들 (종료 시 unlink)
|
||||
|
||||
|
||||
def prepare_breakout_search_context(
|
||||
@@ -159,6 +161,25 @@ def prepare_breakout_search_context(
|
||||
finally:
|
||||
_tick_db.close()
|
||||
|
||||
# ── 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
|
||||
|
||||
grid = grids[mode]
|
||||
_ob_axes = ("max_spread_pct", "min_bid_ask_ratio", "ask_wall_max_qty")
|
||||
_ob_sweeping = any(len(set(grid.get(k) or [])) > 1 for k in _ob_axes)
|
||||
@@ -238,6 +259,7 @@ def prepare_breakout_search_context(
|
||||
start_key=start_key,
|
||||
end_key=end_key,
|
||||
cache_holder=cache_holder,
|
||||
shared_tick_store=shared_tick_store,
|
||||
)
|
||||
|
||||
|
||||
@@ -309,7 +331,17 @@ def run_breakout_optuna(
|
||||
|
||||
logger.info("🔬 Optuna BREAKOUT | study=%s | trials=%d", study_name, n_trials)
|
||||
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
|
||||
|
||||
passing: List[Dict[str, Any]] = []
|
||||
|
||||
Reference in New Issue
Block a user