stl_accel.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #ifndef STL_ACCEL_H
  2. #define STL_ACCEL_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <sys/types.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* =========================================================================
  10. * 1. 6-Tier ISA Runtime CPUID / XGETBV Dispatch
  11. * Tier 0: SSE2 baseline (2003+ x86-64)
  12. * Tier 1: SSSE3 (2012 Atom: PSHUFB Base64 / Hex)
  13. * Tier 2: SSE4.2 + POPCNT + PCLMULQDQ (Celeron J1800/J1900/N2830 / Pentium N3540)
  14. * Tier 3: Goldmont+ SHA-NI + AES-NI + PCLMULQDQ (Celeron J4125/J3455 / Pentium J5005)
  15. * Tier 4: AVX2 + BMI1/2 + PCLMULQDQ [+ SHA-NI/VPCLMULQDQ/GFNI] (Intel N100/N95/N200 & Core)
  16. * Tier 5: AVX-512F + BW + VL (Xeon / Core AVX-512)
  17. * ========================================================================= */
  18. #define STL_ISA_SSE2 0
  19. #define STL_ISA_SSSE3 1
  20. #define STL_ISA_SSE42_PCLMUL 2
  21. #define STL_ISA_GOLDMONT_SHA 3
  22. #define STL_ISA_AVX2_BMI 4
  23. #define STL_ISA_AVX512 5
  24. int stl_cpu_tier_detect(void);
  25. int stl_active_isa_tier(void);
  26. void stl_force_isa_tier(int tier); /* -1 restores auto-detected tier */
  27. const char *stl_isa_tier_name(int tier);
  28. /* Assembly entry points (scan_asm.S) */
  29. uint64_t asm_xgetbv_eax0(void);
  30. size_t asm_base64_encode_sse2(const uint8_t *src, size_t len, uint8_t *dst);
  31. /* Multi-tier SIMD kernels (simd_kernels.c) */
  32. void *stl_aligned_buf_alloc(size_t size, size_t *out_aligned_cap);
  33. void stl_aligned_buf_free(void *ptr);
  34. uint64_t simd_mem_diff_64b_sse2(const uint8_t *a, const uint8_t *b, size_t len);
  35. uint64_t simd_mem_diff_64b_sse42(const uint8_t *a, const uint8_t *b, size_t len);
  36. uint64_t simd_mem_diff_64b_avx2(const uint8_t *a, const uint8_t *b, size_t len);
  37. uint64_t simd_mem_diff_64b_avx512(const uint8_t *a, const uint8_t *b, size_t len);
  38. size_t simd_hex_encode_sse2(const uint8_t *src, size_t len, uint8_t *dst);
  39. size_t simd_hex_encode_ssse3(const uint8_t *src, size_t len, uint8_t *dst);
  40. size_t simd_hex_encode_avx2(const uint8_t *src, size_t len, uint8_t *dst);
  41. size_t simd_base64_encode_ssse3(const uint8_t *src, size_t len, uint8_t *dst);
  42. size_t simd_base64_encode_avx2(const uint8_t *src, size_t len, uint8_t *dst);
  43. uint32_t simd_crc32_ieee_slicing8(uint32_t init_crc, const uint8_t *buf, size_t len);
  44. uint32_t simd_crc32_ieee_pclmul(uint32_t init_crc, const uint8_t *buf, size_t len);
  45. uint32_t simd_crc32_ieee_vpclmul(uint32_t init_crc, const uint8_t *buf, size_t len);
  46. void simd_sha1_sw(const uint8_t *data, size_t len, uint8_t out20[20]);
  47. void simd_sha1_shani(const uint8_t *data, size_t len, uint8_t out20[20]);
  48. /* Dispatched multi-tier CPU entry points */
  49. size_t stl_base64_encode(const uint8_t *src, size_t len, uint8_t *dst);
  50. size_t stl_hex_encode(const uint8_t *src, size_t len, uint8_t *dst);
  51. uint32_t stl_crc32_ieee(uint32_t init_crc, const uint8_t *buf, size_t len);
  52. void stl_sha1_bytes(const uint8_t *data, size_t len, uint8_t out20[20]);
  53. size_t stl_ws_accept_key(const uint8_t *key_ptr, size_t key_len, uint8_t out28[28]);
  54. /* =========================================================================
  55. * 2. Single-Pass Zero-Allocation Serial Clean + UEFI Fold + Split + Classify
  56. * ========================================================================= */
  57. #define STL_TAG_BRACKET (1u << 0) /* '[' banner / stage / tag */
  58. #define STL_TAG_PROMPT (1u << 1) /* '>' or '#' prompt candidate */
  59. #define STL_TAG_ERR_HINT (1u << 2) /* 'E','F','W','U' error/warning hint */
  60. #define STL_TAG_DIGIT (1u << 3) /* '0'..'9' numeric / coord / progress */
  61. #define STL_TAG_COLON_EQ (1u << 4) /* ':' or '=' key-value / table row */
  62. #define STL_HALF_LINE_CAP 1024
  63. typedef struct {
  64. uint32_t offset;
  65. uint32_t len;
  66. uint32_t tag_mask;
  67. uint8_t first_nonws;
  68. uint8_t term_kind; /* 1=LF, 2=CR, 4=ROW_JUMP, 5=WRAP */
  69. uint16_t reserved;
  70. } StlCleanLineSpan;
  71. typedef struct {
  72. uint32_t esc_state;
  73. uint32_t csi_p1;
  74. uint32_t csi_p2;
  75. uint32_t csi_param_idx;
  76. uint32_t cur_row;
  77. uint32_t cur_col;
  78. uint32_t prev_was_cr;
  79. uint32_t half_len;
  80. uint32_t tag_mask;
  81. uint8_t first_nonws;
  82. uint8_t csi_priv;
  83. uint16_t pad0;
  84. uint8_t half_buf[STL_HALF_LINE_CAP];
  85. } StlCleanStreamState;
  86. typedef struct {
  87. uint8_t *clean_buf;
  88. size_t clean_cap;
  89. size_t clean_written;
  90. StlCleanLineSpan *spans;
  91. size_t spans_cap;
  92. size_t spans_count;
  93. size_t src_consumed;
  94. } StlCleanOutput;
  95. _Static_assert(sizeof(StlCleanLineSpan) == 16, "StlCleanLineSpan must be 16 bytes");
  96. _Static_assert(offsetof(StlCleanStreamState, half_buf) == 40, "half_buf must be at offset 40");
  97. _Static_assert(sizeof(StlCleanOutput) == 56, "StlCleanOutput must be 56 bytes");
  98. void asm_clean_split_classify_sse2(
  99. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  100. void asm_clean_split_classify_sse42(
  101. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  102. void asm_clean_split_classify_avx2(
  103. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  104. void stl_clean_stream_init(StlCleanStreamState *state);
  105. void stl_clean_stream_clear_halfline(StlCleanStreamState *state);
  106. void stl_clean_split_classify(
  107. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  108. /* =========================================================================
  109. * 3. Monotonic 64-Bit mmap Ring Buffer (Hugepage + 64B Tail Pad + Guard Page)
  110. * ========================================================================= */
  111. typedef struct {
  112. uint8_t *buf; /* mmap base: [0..cap) ring, [cap..cap+64) zero pad, then PROT_NONE */
  113. size_t cap; /* power-of-two capacity (default 8 MiB) */
  114. size_t mask; /* cap - 1 */
  115. size_t mmap_total; /* total mmap length including guard page */
  116. uint64_t base_offset; /* oldest retained absolute byte offset (monotonic, never decreases) */
  117. uint64_t write_offset; /* next write absolute byte offset (monotonic, never decreases) */
  118. } StlRingBuf;
  119. int stl_ring_init(StlRingBuf *rb, size_t requested_cap);
  120. void stl_ring_destroy(StlRingBuf *rb);
  121. uint64_t stl_ring_append(StlRingBuf *rb, const uint8_t *data, size_t len);
  122. size_t stl_ring_read(
  123. const StlRingBuf *rb,
  124. uint64_t req_offset,
  125. uint8_t *dst,
  126. size_t max_len,
  127. uint64_t *out_actual_start,
  128. uint64_t *out_next_offset);
  129. void stl_ring_clear_monotonic(StlRingBuf *rb);
  130. /* =========================================================================
  131. * 4. Deterministic re2c Line Lexer (parser_re2c.re -> parser_re2c.c)
  132. * ========================================================================= */
  133. typedef enum {
  134. STL_LK_NONE = 0,
  135. STL_LK_ICT_BANNER_HOVSEE = 1,
  136. STL_LK_ICT_BANNER_CXMT = 2,
  137. STL_LK_ICT_START_STAGE1 = 3,
  138. STL_LK_ICT_START_STAGE2 = 4,
  139. STL_LK_ICT_ENTER_HINT = 5,
  140. STL_LK_ICT_PROMPT = 6,
  141. STL_LK_ICT_EXIT = 7,
  142. STL_LK_MRC_START = 8,
  143. STL_LK_MRC_DONE = 9,
  144. STL_LK_DIMM_INVENTORY = 10,
  145. STL_LK_RMT_START = 11,
  146. STL_LK_RMT_ROW = 12,
  147. STL_LK_RMT_STOP = 13,
  148. STL_LK_TRAINING_ERROR = 14,
  149. STL_LK_AMT_BANNER = 15,
  150. STL_LK_AMT_PROMPT = 16,
  151. STL_LK_AMT_PROGRESS = 17,
  152. STL_LK_AMT_SUMMARY = 18,
  153. STL_LK_AMT_ERROR = 19,
  154. STL_LK_UEFI_BANNER = 20,
  155. STL_LK_UEFI_PROMPT_ROOT = 21,
  156. STL_LK_UEFI_PROMPT_EFI = 22,
  157. STL_LK_PXE_BOOT = 23,
  158. STL_LK_MT_BANNER = 24,
  159. STL_LK_MT_PROGRESS = 25,
  160. STL_LK_MT_ERROR = 26,
  161. STL_LK_MT_PASS_COMPLETE = 27,
  162. STL_LK_MT_FAIL_COMPLETE = 28,
  163. STL_LK_INLINE_TEMP = 29,
  164. STL_LK_BIOS_BOOT = 30
  165. } StlLineKind;
  166. typedef struct {
  167. uint32_t kind; /* StlLineKind */
  168. uint8_t fw_family; /* 0=unknown, 1=Hovsee/GNR-4.3.3(24-algo), 2=CxmtDdrTest/GNR-4.3.0(19-algo) */
  169. int8_t cpu; /* -1 if unspecified, 0 or 1 */
  170. int8_t channel; /* -1 if unspecified, 0..11 */
  171. int8_t dimm; /* -1 if unspecified, 0..1 */
  172. int8_t rank; /* -1 if unspecified, 0..3 */
  173. int8_t bank; /* -1 if unspecified, validated 0..31 (never >= 32!) */
  174. int8_t subkind; /* 0=normal, 1=pass, 2=fail, 3=disabled, 4=present, 5=coord_cont */
  175. uint8_t fs_index; /* 0..9 for FSx:\ prompt */
  176. int32_t val0; /* algo_id / progress_pct / margin_rx_lo / error_cnt */
  177. int32_t val1; /* loop_or_total / margin_rx_hi */
  178. int32_t val2; /* step / margin_tx_lo */
  179. int32_t val3; /* margin_tx_hi / temp_c_x10 */
  180. uint64_t addr; /* parsed hex address if present */
  181. } StlRe2cEvent;
  182. _Static_assert(sizeof(StlRe2cEvent) == 40, "StlRe2cEvent must be 40 bytes");
  183. void stl_re2c_parse_line(const uint8_t *line, size_t len, uint32_t tag_mask, StlRe2cEvent *ev);
  184. int stl_re2c_detect_halfline_prompt(const uint8_t *buf, size_t len, StlRe2cEvent *ev);
  185. /* =========================================================================
  186. * 5. Linux epoll / timerfd / eventfd / Non-Blocking Serial & Socket Helpers
  187. * ========================================================================= */
  188. int stl_epoll_create(void);
  189. int stl_epoll_ctl_fd(int epfd, int op, int fd, uint32_t events, uint64_t token);
  190. int stl_epoll_wait_tokens(
  191. int epfd, uint64_t *out_tokens, uint32_t *out_events, int max_ev, int timeout_ms);
  192. int stl_timerfd_create_ms(uint64_t initial_ms, uint64_t interval_ms);
  193. int stl_timerfd_arm_ms(int tfd, uint64_t initial_ms, uint64_t interval_ms);
  194. void stl_timerfd_drain(int tfd);
  195. int stl_eventfd_create(void);
  196. void stl_eventfd_notify(int efd);
  197. void stl_eventfd_drain(int efd);
  198. int stl_set_nonblocking_nodelay(int fd, int is_tcp);
  199. int stl_open_tty_115200_raw(const char *path);
  200. int64_t stl_ws_writev2(int fd, const uint8_t *hdr, size_t hdr_len, const uint8_t *body, size_t body_len);
  201. void stl_pin_thread_to_core(int core_index);
  202. int c_pty_open_serial_115200(const char *path);
  203. int c_open_tty_baud_raw(const char *path, int baud);
  204. int c_open_mcu_tty_baud_raw(const char *path, int baud);
  205. int c_pty_open_tcp_serial(const char *host, int port, int timeout_ms);
  206. int c_pty_open_tcp_listen(int port);
  207. int c_pty_accept_tcp_nonblock(int listen_fd);
  208. int c_pty_spawn_ssh_console(
  209. const char *host,
  210. int port,
  211. const char *user,
  212. const char *password,
  213. const char *known_hosts_path,
  214. int *out_master_fd,
  215. int *out_child_pid);
  216. int c_pty_spawn_ipmi_sol(
  217. const char *host,
  218. const char *user,
  219. const char *password,
  220. int *out_master_fd,
  221. int *out_child_pid);
  222. ssize_t c_pty_read_nonblock(int master_fd, uint8_t *buf, size_t cap);
  223. ssize_t c_mcu_tty_read_nonblock(int fd, uint8_t *buf, size_t cap, int is_serial);
  224. ssize_t c_pty_write_bytes(int master_fd, const uint8_t *buf, size_t len);
  225. int c_pty_is_alive(int child_pid);
  226. void c_pty_close(int master_fd, int child_pid);
  227. /* In-process libssh exec + SFTP atomic file transfer */
  228. int c_ssh_exec_capture(
  229. const char *host,
  230. int port,
  231. const char *user,
  232. const char *password,
  233. const char *cmd,
  234. char *out_buf,
  235. size_t out_cap,
  236. size_t *out_len,
  237. int timeout_ms);
  238. int c_ssh_sftp_put_file(
  239. const char *host,
  240. int port,
  241. const char *user,
  242. const char *password,
  243. const char *local_path,
  244. const char *remote_path,
  245. int timeout_ms);
  246. int c_ssh_sftp_get_file(
  247. const char *host,
  248. int port,
  249. const char *user,
  250. const char *password,
  251. const char *remote_path,
  252. const char *local_path,
  253. int timeout_ms);
  254. /* In-process libfreeipmi persistent session & telemetry */
  255. int c_ipmi_open(
  256. const char *host,
  257. const char *user,
  258. const char *password,
  259. int cipher_suite,
  260. int timeout_ms);
  261. void c_ipmi_close(int handle);
  262. int c_ipmi_is_alive(int handle);
  263. int c_ipmi_chassis_status(int handle, int *out_power_on, char *out_buf, size_t out_cap);
  264. int c_ipmi_chassis_control(int handle, int op);
  265. int c_ipmi_warm_reset(int handle);
  266. int c_ipmi_raw(
  267. int handle,
  268. uint8_t net_fn,
  269. uint8_t cmd,
  270. const uint8_t *payload,
  271. size_t payload_len,
  272. uint8_t *out_buf,
  273. size_t out_cap,
  274. size_t *out_len);
  275. int c_ipmi_sdr_temp_list(int handle, char *out_buf, size_t out_cap, size_t *out_len);
  276. void c_ipmi_invalidate_sdr_cache(int handle);
  277. int c_ipmi_device_info(
  278. int handle,
  279. const char *kind,
  280. char *out_buf,
  281. size_t out_cap,
  282. size_t *out_len);
  283. /* =========================================================================
  284. * 6. OpenCL Gen7.5/Gen9.5/Gen12 Zero-Copy Batch GPU Offload (Auto CPU Fallback)
  285. * ========================================================================= */
  286. typedef struct {
  287. int available; /* 1 if Intel/OpenCL iGPU initialized */
  288. int zero_copy_llc; /* 1 if unified memory CL_MEM_USE_HOST_PTR supported */
  289. uint32_t compute_units;
  290. uint64_t break_even_bytes; /* Calibrated S* threshold (default 524288 = 512 KiB) */
  291. uint64_t gpu_offload_count;
  292. uint64_t cpu_fallback_count;
  293. uint32_t calibrated_gpu_us;
  294. uint32_t calibrated_cpu_us;
  295. char device_name[96];
  296. } StlOpenClInfo;
  297. void stl_opencl_query_info(StlOpenClInfo *out_info);
  298. int stl_opencl_has_tdp_headroom(void);
  299. /* Batch historical log newline/error-marker pre-index; auto-falls back to CPU SIMD below S* or low TDP */
  300. size_t stl_batch_rescan_logs(
  301. const uint8_t *buf_4k_aligned,
  302. size_t len,
  303. uint32_t *out_line_offsets,
  304. uint8_t *out_line_tags,
  305. size_t max_lines,
  306. int *used_gpu);
  307. /* Batch large-buffer Hex encode (GPU >= S* on 4KB-aligned buffer, CPU multi-tier SIMD fallback) */
  308. size_t stl_batch_hex_encode(
  309. const uint8_t *src_4k_aligned,
  310. size_t len,
  311. uint8_t *dst_4k_aligned,
  312. int *used_gpu);
  313. /* Batch parallel 64 KiB block IEEE CRC-32 (for 50 MiB rotated trace volumes; CPU PCLMULQDQ fallback) */
  314. size_t stl_batch_chunk_crc32(
  315. const uint8_t *buf_4k_aligned,
  316. size_t len,
  317. size_t chunk_size,
  318. uint32_t *out_crcs,
  319. size_t max_chunks,
  320. int *used_gpu);
  321. /* Multi-station RMT margin matrix min/max/mean & 16x16 eye-diagram bilinear interpolation */
  322. int stl_batch_rmt_eye_reduce(
  323. const float *margins_in, /* [station_count * 24 * 4] (rx_lo, rx_hi, tx_lo, tx_hi) */
  324. size_t station_count,
  325. float *eye_grid_out, /* [station_count * 24 * 256] (16x16 normalized eye surface) */
  326. float *summary_out, /* [station_count * 4] (min_timing, min_vref, mean_timing, mean_vref) */
  327. int *used_gpu);
  328. /* Filesystem free-space guard, idle priority, monotonic clock, SIMD diff, permission & UDP/NIC helpers */
  329. int stl_free_disk_bytes(const char *path, uint64_t *out_avail_bytes);
  330. void stl_set_thread_idle_priority(void);
  331. int stl_chmod_0600(const char *path);
  332. uint64_t stl_mono_us(void);
  333. uint64_t stl_mem_diff_64b_mask(const uint8_t *a, const uint8_t *b, size_t len);
  334. void stl_batch_mem_diff_64b_masks(
  335. const uint8_t *prev_base,
  336. const uint8_t *cur_base,
  337. size_t stride,
  338. size_t item_len,
  339. size_t count,
  340. uint64_t *out_masks);
  341. typedef struct {
  342. char name[32];
  343. uint32_t ip_be;
  344. uint32_t mask_be;
  345. uint32_t bcast_be;
  346. uint32_t ifindex;
  347. uint8_t mac[6];
  348. uint8_t is_up;
  349. uint8_t is_loopback;
  350. } StlNetIface;
  351. int c_list_ipv4_ifaces(StlNetIface *out_list, int max_count);
  352. int c_udp_bind_dhcp_socket(const char *iface, int port, int reuse_port);
  353. ssize_t c_udp_recv_pktinfo(
  354. int fd,
  355. uint8_t *buf,
  356. size_t cap,
  357. uint32_t *out_peer_ip_be,
  358. uint16_t *out_peer_port,
  359. uint32_t *out_local_ip_be,
  360. uint32_t *out_ifindex);
  361. ssize_t c_udp_send_pktinfo(
  362. int fd,
  363. const uint8_t *buf,
  364. size_t len,
  365. uint32_t dst_ip_be,
  366. uint16_t dst_port,
  367. uint32_t src_ip_be,
  368. uint32_t ifindex);
  369. #ifdef __cplusplus
  370. }
  371. #endif
  372. #endif /* STL_ACCEL_H */