Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cortex/kernel_features/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Cortex Kernel Features

User-space implementations of kernel-level AI concepts:
- Model Lifecycle Manager (systemd-based LLM services)
- KV-Cache Manager (shared memory cache pools)
- Accelerator Limits (cgroups v2 wrapper)
- LLM Device (/dev/llm FUSE interface)
"""

from .model_lifecycle import ModelLifecycleManager, ModelConfig
from .kv_cache_manager import KVCacheManager, CacheConfig
from .accelerator_limits import AcceleratorLimitsManager, ResourceLimits

__all__ = [
'ModelLifecycleManager', 'ModelConfig',
'KVCacheManager', 'CacheConfig',
'AcceleratorLimitsManager', 'ResourceLimits',
]
124 changes: 124 additions & 0 deletions cortex/kernel_features/accelerator_limits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env python3
"""
Cortex Accelerator-Aware Resource Limits

cgroups v2 wrapper for AI workloads.
"""

import os
import json
import sqlite3
import subprocess
from pathlib import Path
from dataclasses import dataclass, asdict
from typing import Optional, List, Dict
from enum import Enum

CORTEX_DB = Path.home() / ".cortex/limits.db"
CGROUP_ROOT = Path("/sys/fs/cgroup")

class WorkloadPreset(Enum):
INFERENCE = "inference"
TRAINING = "training"
BATCH = "batch"
INTERACTIVE = "interactive"

PRESETS = {
"inference": {"cpu": 400, "memory_gb": 32, "oom_adj": -500, "gpu_pct": 100},
"training": {"cpu": 1600, "memory_gb": 128, "oom_adj": -800, "gpu_pct": 100},
"batch": {"cpu": 800, "memory_gb": 64, "oom_adj": 0, "gpu_pct": 80},
"interactive": {"cpu": 200, "memory_gb": 16, "oom_adj": -200, "gpu_pct": 50},
}

@dataclass
class ResourceLimits:
name: str
preset: str = "inference"
cpu_quota: float = 400.0
memory_max: int = 32 * 1024**3
gpu_ids: List[int] = None

Check warning on line 39 in cortex/kernel_features/accelerator_limits.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace the type hint "list[int]" with "Optional[list[int]]" or don't assign "None" to "gpu_ids"

See more on https://sonarcloud.io/project/issues?id=cortexlinux_cortex&issues=AZretQQai-4iu7DOY4jp&open=AZretQQai-4iu7DOY4jp&pullRequest=224
oom_score_adj: int = 0

def __post_init__(self):
self.gpu_ids = self.gpu_ids or []

@classmethod
def from_preset(cls, name: str, preset: str, gpus: int = 0):
p = PRESETS.get(preset, PRESETS["inference"])
return cls(name, preset, p["cpu"], int(p["memory_gb"] * 1e9),
list(range(gpus)), p["oom_adj"])


class LimitsDatabase:
def __init__(self):
CORTEX_DB.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(CORTEX_DB) as conn:
conn.execute("CREATE TABLE IF NOT EXISTS profiles (name TEXT PRIMARY KEY, config TEXT)")

def save(self, limits: ResourceLimits):
with sqlite3.connect(CORTEX_DB) as conn:
conn.execute("INSERT OR REPLACE INTO profiles VALUES (?,?)",
(limits.name, json.dumps(asdict(limits))))

def get(self, name: str) -> Optional[ResourceLimits]:
with sqlite3.connect(CORTEX_DB) as conn:
row = conn.execute("SELECT config FROM profiles WHERE name=?", (name,)).fetchone()
return ResourceLimits(**json.loads(row[0])) if row else None

def list_all(self):
with sqlite3.connect(CORTEX_DB) as conn:
return [ResourceLimits(**json.loads(r[0])) for r in conn.execute("SELECT config FROM profiles")]


class AcceleratorLimitsManager:
def __init__(self):
self.db = LimitsDatabase()

def create(self, limits: ResourceLimits) -> bool:
self.db.save(limits)
print(f"✅ Created profile '{limits.name}' (preset: {limits.preset})")
return True

def get_env(self, name: str) -> Dict[str, str]:
limits = self.db.get(name)
if not limits:
return {}
return {"CUDA_VISIBLE_DEVICES": ",".join(map(str, limits.gpu_ids))}

def status(self):
profiles = self.db.list_all()
print(f"\n{'NAME':<20} {'PRESET':<12} {'CPU':<8} {'MEMORY':<10} {'GPUS':<10}")
print("-" * 65)
for p in profiles:
gpus = ",".join(map(str, p.gpu_ids)) or "-"
print(f"{p.name:<20} {p.preset:<12} {p.cpu_quota/100:.0f}{'':<5} {p.memory_max/1e9:.0f}G{'':<5} {gpus:<10}")


def main():
import argparse
parser = argparse.ArgumentParser(description="Cortex Accelerator Limits")
sub = parser.add_subparsers(dest="cmd")

c = sub.add_parser("create")
c.add_argument("name")
c.add_argument("--preset", default="inference")
c.add_argument("--gpus", type=int, default=0)

sub.add_parser("env").add_argument("name")
sub.add_parser("status")
sub.add_parser("list")

args = parser.parse_args()
mgr = AcceleratorLimitsManager()

if args.cmd == "create":
mgr.create(ResourceLimits.from_preset(args.name, args.preset, args.gpus))
elif args.cmd == "env":
for k, v in mgr.get_env(args.name).items():
print(f"export {k}={v}")
elif args.cmd in ("status", "list"):
mgr.status()


if __name__ == "__main__":
main()
157 changes: 157 additions & 0 deletions cortex/kernel_features/kv_cache_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/usr/bin/env python3
"""
Cortex KV-Cache Manager

User-space KV-cache management for LLM inference optimization.
"""

import os
import json
import sqlite3
import time
import hashlib
from pathlib import Path
from dataclasses import dataclass, asdict
from typing import Optional, List, Dict
from multiprocessing import shared_memory
from enum import Enum

CORTEX_DB = Path.home() / ".cortex/kv_cache.db"
SHM_PREFIX = "cortex_kv_"

class CachePolicy(Enum):
LRU = "lru"
LFU = "lfu"
FIFO = "fifo"

@dataclass
class CacheConfig:
name: str
size_bytes: int
policy: str = "lru"
max_sequences: int = 1000

@dataclass
class CacheEntry:
sequence_id: int
created_at: float
last_accessed: float
access_count: int
token_count: int
size_bytes: int
offset: int


class CacheDatabase:
def __init__(self):
CORTEX_DB.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(CORTEX_DB) as conn:
conn.executescript("""
CREATE TABLE IF NOT EXISTS pools (name TEXT PRIMARY KEY, config TEXT, shm_name TEXT);
CREATE TABLE IF NOT EXISTS entries (seq_id INTEGER, pool TEXT, created REAL, accessed REAL,
count INTEGER, tokens INTEGER, size INTEGER, offset INTEGER, PRIMARY KEY(seq_id, pool));
CREATE TABLE IF NOT EXISTS stats (pool TEXT PRIMARY KEY, hits INTEGER DEFAULT 0, misses INTEGER DEFAULT 0);
""")

def save_pool(self, cfg: CacheConfig, shm: str):
with sqlite3.connect(CORTEX_DB) as conn:
conn.execute("INSERT OR REPLACE INTO pools VALUES (?,?,?)", (cfg.name, json.dumps(asdict(cfg)), shm))
conn.execute("INSERT OR IGNORE INTO stats (pool) VALUES (?)", (cfg.name,))

def get_pool(self, name: str):
with sqlite3.connect(CORTEX_DB) as conn:
row = conn.execute("SELECT config, shm_name FROM pools WHERE name=?", (name,)).fetchone()
return (CacheConfig(**json.loads(row[0])), row[1]) if row else None

def list_pools(self):
with sqlite3.connect(CORTEX_DB) as conn:
return [CacheConfig(**json.loads(r[0])) for r in conn.execute("SELECT config FROM pools").fetchall()]


class SharedMemoryPool:
def __init__(self, name: str, size: int, create: bool = True):
self.name = f"{SHM_PREFIX}{name}"
self.size = size
if create:
try:
old = shared_memory.SharedMemory(name=self.name)
old.close()
old.unlink()
except:

Check failure on line 80 in cortex/kernel_features/kv_cache_manager.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Specify an exception class to catch or reraise the exception

See more on https://sonarcloud.io/project/issues?id=cortexlinux_cortex&issues=AZretQP9i-4iu7DOY4jm&open=AZretQP9i-4iu7DOY4jm&pullRequest=224
pass
self.shm = shared_memory.SharedMemory(name=self.name, create=True, size=size + 8192)
else:
self.shm = shared_memory.SharedMemory(name=self.name)

def get_usage(self):
return self.size, 0, 0 # Simplified

def destroy(self):
self.shm.close()
try:
self.shm.unlink()
except:

Check failure on line 93 in cortex/kernel_features/kv_cache_manager.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Specify an exception class to catch or reraise the exception

See more on https://sonarcloud.io/project/issues?id=cortexlinux_cortex&issues=AZretQP9i-4iu7DOY4jn&open=AZretQP9i-4iu7DOY4jn&pullRequest=224
pass
Comment on lines +71 to +94
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Replace bare except clauses with specific exception types.

Bare except: catches all exceptions including KeyboardInterrupt and SystemExit, which can mask critical errors. The static analysis tools flag this as a failure.

 class SharedMemoryPool:
     def __init__(self, name: str, size: int, create: bool = True):
         self.name = f"{SHM_PREFIX}{name}"
         self.size = size
         if create:
             try:
                 old = shared_memory.SharedMemory(name=self.name)
                 old.close()
                 old.unlink()
-            except:
+            except FileNotFoundError:
                 pass
             self.shm = shared_memory.SharedMemory(name=self.name, create=True, size=size + 8192)
         else:
             self.shm = shared_memory.SharedMemory(name=self.name)
     
     def get_usage(self):
         return self.size, 0, 0  # Simplified
     
     def destroy(self):
         self.shm.close()
         try:
             self.shm.unlink()
-        except:
+        except FileNotFoundError:
             pass
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class SharedMemoryPool:
def __init__(self, name: str, size: int, create: bool = True):
self.name = f"{SHM_PREFIX}{name}"
self.size = size
if create:
try:
old = shared_memory.SharedMemory(name=self.name)
old.close()
old.unlink()
except:
pass
self.shm = shared_memory.SharedMemory(name=self.name, create=True, size=size + 8192)
else:
self.shm = shared_memory.SharedMemory(name=self.name)
def get_usage(self):
return self.size, 0, 0 # Simplified
def destroy(self):
self.shm.close()
try:
self.shm.unlink()
except:
pass
class SharedMemoryPool:
def __init__(self, name: str, size: int, create: bool = True):
self.name = f"{SHM_PREFIX}{name}"
self.size = size
if create:
try:
old = shared_memory.SharedMemory(name=self.name)
old.close()
old.unlink()
except FileNotFoundError:
pass
self.shm = shared_memory.SharedMemory(name=self.name, create=True, size=size + 8192)
else:
self.shm = shared_memory.SharedMemory(name=self.name)
def get_usage(self):
return self.size, 0, 0 # Simplified
def destroy(self):
self.shm.close()
try:
self.shm.unlink()
except FileNotFoundError:
pass
🧰 Tools
🪛 GitHub Check: SonarCloud Code Analysis

[failure] 93-93: Specify an exception class to catch or reraise the exception

See more on https://sonarcloud.io/project/issues?id=cortexlinux_cortex&issues=AZretQP9i-4iu7DOY4jn&open=AZretQP9i-4iu7DOY4jn&pullRequest=224


[failure] 80-80: Specify an exception class to catch or reraise the exception

See more on https://sonarcloud.io/project/issues?id=cortexlinux_cortex&issues=AZretQP9i-4iu7DOY4jm&open=AZretQP9i-4iu7DOY4jm&pullRequest=224

🪛 Ruff (0.14.7)

80-80: Do not use bare except

(E722)


80-81: try-except-pass detected, consider logging the exception

(S110)


93-93: Do not use bare except

(E722)


93-94: try-except-pass detected, consider logging the exception

(S110)

🤖 Prompt for AI Agents
In cortex/kernel_features/kv_cache_manager.py around lines 71 to 94, replace the
bare except blocks with specific exception types: when attempting to open/unlink
an existing shared memory segment (the first try), catch FileNotFoundError and
PermissionError (or OSError) only, and let other exceptions propagate; when
unlinking in destroy(), catch FileNotFoundError (and optionally
PermissionError/OSError) instead of a bare except. If you want to be explicit,
catch (FileNotFoundError, PermissionError, OSError) and avoid swallowing
KeyboardInterrupt/SystemExit by not using a bare except.



class KVCacheManager:
def __init__(self):
self.db = CacheDatabase()
self.pools: Dict[str, SharedMemoryPool] = {}

def create_pool(self, cfg: CacheConfig) -> bool:
pool = SharedMemoryPool(cfg.name, cfg.size_bytes)
self.pools[cfg.name] = pool
self.db.save_pool(cfg, pool.name)
print(f"✅ Created cache pool '{cfg.name}' ({cfg.size_bytes / 1e9:.1f} GB)")
return True

def destroy_pool(self, name: str) -> bool:
if name in self.pools:
self.pools[name].destroy()
del self.pools[name]
with sqlite3.connect(CORTEX_DB) as conn:
conn.execute("DELETE FROM pools WHERE name=?", (name,))
print(f"✅ Destroyed pool '{name}'")
return True
Comment on lines +109 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Resource leak: shared memory not cleaned up if pool not in current instance.

If a pool was created by a different process instance, it won't be in self.pools. The method deletes the DB record but leaves the shared memory segment orphaned.

     def destroy_pool(self, name: str) -> bool:
         if name in self.pools:
             self.pools[name].destroy()
             del self.pools[name]
+        else:
+            # Try to clean up SHM created by another process
+            try:
+                pool = SharedMemoryPool(name, 0, create=False)
+                pool.destroy()
+            except FileNotFoundError:
+                pass
         with sqlite3.connect(CORTEX_DB) as conn:
             conn.execute("DELETE FROM pools WHERE name=?", (name,))
         print(f"✅ Destroyed pool '{name}'")
         return True

Committable suggestion skipped: line range outside the PR's diff.


def status(self, name: str = None):
pools = [self.db.get_pool(name)] if name else [(p, "") for p in self.db.list_pools()]
print(f"\n{'POOL':<20} {'SIZE':<12} {'POLICY':<10}")
print("-" * 50)
for item in pools:
if item:
cfg = item[0] if isinstance(item, tuple) else item
print(f"{cfg.name:<20} {cfg.size_bytes/1e9:.1f}G{'':<6} {cfg.policy:<10}")


def main():
import argparse
parser = argparse.ArgumentParser(description="Cortex KV-Cache Manager")
sub = parser.add_subparsers(dest="cmd")

c = sub.add_parser("create")
c.add_argument("name")
c.add_argument("--size", required=True)
c.add_argument("--policy", default="lru")

sub.add_parser("destroy").add_argument("name")
sub.add_parser("status").add_argument("name", nargs="?")
sub.add_parser("list")

args = parser.parse_args()
mgr = KVCacheManager()

if args.cmd == "create":
size_str = args.size.upper()
mult = {"K": 1e3, "M": 1e6, "G": 1e9}.get(size_str[-1], 1)
size = int(float(size_str.rstrip("KMG")) * mult)
mgr.create_pool(CacheConfig(args.name, size, args.policy))
elif args.cmd == "destroy":
mgr.destroy_pool(args.name)
elif args.cmd in ("status", "list"):
mgr.status(getattr(args, 'name', None))


if __name__ == "__main__":
main()
Loading
Loading