Process address space, thread scheduling và OS debugging
Process là resource/protection container; thread là schedulable execution context chia sẻ phần lớn process resources. JVM threads cuối cùng vẫn chịu scheduler, memory và kernel limits.
Process resource model
Mỗi process có virtual address space, page tables/mappings, credentials, file-descriptor table, signal dispositions, namespaces/cgroups và một hay nhiều threads. Virtual memory tách địa chỉ khỏi physical pages; RSS, PSS, anonymous/file-backed, shared/private và swap đo các góc khác nhau. fork() tạo child với copy-on-write mappings; execve() thay program image nhưng giữ một số process attributes/FD không close-on-exec.
Thread resource model
Threads cùng process chia sẻ address space, heap/code, open files và nhiều signal dispositions; mỗi thread có TID, registers/program counter, stack, scheduling state, signal mask và thread-local storage. Shared memory làm communication rẻ nhưng tạo races/visibility/locks; stack per thread làm số thread ảnh hưởng virtual/native memory.
Scheduling và context switch
Linux scheduler chọn runnable tasks theo scheduling class, priority/nice, CPU affinity và cgroup quota. Context switch lưu/khôi phục execution state; cache/TLB locality và migration có cost. Load average gồm runnable và uninterruptible tasks, không đồng nghĩa CPU utilization. Nhiều threads hơn cores có thể tăng throughput I/O-bound nhưng làm queue/context switching/lock contention tệ hơn.
Lifecycle và signals
clone/fork tạo task/process semantics; thread exit giải phóng stack/TLS, process exit đóng FDs và để zombie cho parent reap. Signals có process-directed hoặc thread-directed delivery; SIGTERM cho graceful shutdown, SIGKILL không cleanup. PID 1 có signal/reaping semantics đặc biệt trong container; application cần forward signal và reap children nếu làm init.
IPC và file descriptors
Pipes, Unix/domain sockets, TCP sockets, shared memory, mmap, signals và queues có latency/copy/backpressure/failure khác nhau. FD là index vào per-process table trỏ open file description; dup/fork có thể share offset/status. Socket cũng là FD; limits đến từ per-process RLIMIT_NOFILE, system limits, ephemeral ports, backlog và memory—not chỉ application pool.
Namespaces, cgroups và containers
PID namespace đổi process view nhưng không tạo VM; mount/network/user namespaces tách resources. cgroups giới hạn/account CPU, memory, PIDs và I/O. JVM/container phải đọc đúng limits; OOM kill khác Java OutOfMemoryError. Host PID/TID và namespace IDs có thể khác khi debug từ host/container.
JVM mapping
Java platform threads thường map 1:1 native OS threads; virtual threads được JVM schedule lên carrier platform threads nhưng blocking native/pinning/downstream capacity vẫn quan trọng. Heap/metaspace/code cache/direct buffers/thread stacks/JIT/native libraries góp vào RSS. Một Java process có GC/compiler/service threads ngoài application pool.
Evidence-first debugging
| Triệu chứng | Evidence ưu tiên | Giả thuyết |
|---|---|---|
| CPU cao | process/thread CPU, run queue, throttling, JFR/perf stacks | hot loop, GC, lock spin, excessive runnable, quota |
| CPU thấp, latency cao | thread states, pool/queue wait, iowait, socket/DB spans | blocked I/O, lock, downstream, saturation |
| RSS tăng | heap-after-GC, NMT, smaps/PSS, thread count, direct buffers | heap retain, native leak, mmap/cache, stacks |
| Cannot open/connect | FD count/limits, sockets, ephemeral ports, SYN/backlog | FD leak, port exhaustion, accept queue, DNS/network |
Workflow: xác định impact/time/scope → container/host limits → process → threads → stacks/resources → correlate deploy/load → mitigation một thay đổi → verify. Không kết luận từ một snapshot; lấy nhiều thread dumps/profiles theo thời gian.