Skip to content

Commit 68bfab7

Browse files
committed
Enhanced the output of cache miss counts and branch miss counts for performance event groups; added snapshot index printing functionality; updated userspace code generation tests to verify variable reuse logic.
1 parent 4becf2e commit 68bfab7

5 files changed

Lines changed: 105 additions & 4 deletions

File tree

examples/perf_cache_miss.ks

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,35 @@ fn main() -> i32 {
2222
print("Cache-miss count: %lld", cache_count)
2323
var branch_count = read(branch)
2424
print("Branch-miss count: %lld", branch_count)
25+
26+
var prev = read_details(cache)
27+
// Simulate workload with cache misses and branch misses.
28+
var x = 0
29+
var i = 0
30+
for (i in 0..10000000) {
31+
if (i % 100 == 0) {
32+
x = x + 1
33+
} else {
34+
x = x * 2
35+
}
36+
}
37+
var cur = read_details(cache)
38+
var delta = cur.scaled - prev.scaled
39+
var dt_ns = cur.time_enabled - prev.time_enabled
40+
if (dt_ns > 0) {
41+
var per_sec = (delta * 1000000000) / dt_ns
42+
print("Cache misses/sec: %lld", per_sec)
43+
}
44+
2545
var snapshot = read_group(cache)
2646
print("Grouped snapshot entries: %u", snapshot.count)
2747

48+
var snapshot_index = 0
49+
while (snapshot_index < snapshot.count) {
50+
print("id=%llu value=%lld", snapshot.ids[snapshot_index], snapshot.values[snapshot_index])
51+
snapshot_index = snapshot_index + 1
52+
}
53+
2854
detach(branch)
2955
detach(cache)
3056
detach(prog)

src/ir_generator.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ let rec lower_expression ctx (expr : Ast.expr) =
877877
emit_variable_decl_val ctx ptr_val ptr_val.val_type (Some ptr_expr) expr.expr_pos;
878878

879879
(* result = *ptr *)
880-
let load_expr = make_ir_expr (IRValue ptr_val) element_type expr.expr_pos in
880+
let load_expr = make_ir_expr (IRUnOp (IRDeref, ptr_val)) element_type expr.expr_pos in
881881
emit_variable_decl_val ctx result_val element_type (Some load_expr) expr.expr_pos);
882882

883883
result_val)
@@ -3572,4 +3572,4 @@ let generate_ir ?(use_type_annotations=false) ast symbol_table source_name =
35723572
with
35733573
| exn ->
35743574
Printf.eprintf "IR generation failed: %s\n" (Printexc.to_string exn);
3575-
raise exn
3575+
raise exn

src/userspace_codegen.ml

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,11 @@ let rec generate_c_instruction_from_ir ctx instruction =
18951895
(match init_expr_opt with
18961896
| Some init_expr ->
18971897
let init_str = generate_c_expression_from_ir ctx init_expr in
1898-
sprintf "%s = %s;" array_decl init_str
1898+
(match init_expr.expr_desc with
1899+
| IRValue { value_desc = IRLiteral (ArrayLit _); _ } ->
1900+
sprintf "%s = %s;" array_decl init_str
1901+
| _ ->
1902+
sprintf "%s;\n memcpy(%s, %s, sizeof(%s));" array_decl c_var_name init_str c_var_name)
18991903
| None ->
19001904
sprintf "%s;" array_decl)
19011905
| _ ->
@@ -2394,8 +2398,33 @@ let collect_undeclared_variables_in_function ir_func =
23942398
| _ -> ()
23952399
in
23962400

2401+
let rec collect_declared_from_instr ir_instr =
2402+
collect_declared_vars ir_instr;
2403+
match ir_instr.instr_desc with
2404+
| IRIf (_, then_instrs, else_instrs_opt) ->
2405+
List.iter collect_declared_from_instr then_instrs;
2406+
(match else_instrs_opt with
2407+
| Some else_instrs -> List.iter collect_declared_from_instr else_instrs
2408+
| None -> ())
2409+
| IRIfElseChain (conditions_and_bodies, final_else) ->
2410+
List.iter (fun (_, instrs) ->
2411+
List.iter collect_declared_from_instr instrs
2412+
) conditions_and_bodies;
2413+
(match final_else with
2414+
| Some instrs -> List.iter collect_declared_from_instr instrs
2415+
| None -> ())
2416+
| IRBpfLoop (_, _, _, _, body_instructions) ->
2417+
List.iter collect_declared_from_instr body_instructions
2418+
| IRTry (try_instrs, catch_clauses) ->
2419+
List.iter collect_declared_from_instr try_instrs;
2420+
List.iter (fun clause ->
2421+
List.iter collect_declared_from_instr clause.catch_body
2422+
) catch_clauses
2423+
| _ -> ()
2424+
in
2425+
23972426
let collect_declared_from_instrs instrs =
2398-
List.iter collect_declared_vars instrs
2427+
List.iter collect_declared_from_instr instrs
23992428
in
24002429

24012430
List.iter (fun block ->
@@ -2614,6 +2643,10 @@ let generate_c_function_from_ir ?(global_variables = []) ?(base_name = "") ?(con
26142643
let rec collect_declared_vars ir_instr =
26152644
match ir_instr.instr_desc with
26162645
| IRVariableDecl (dest_val, _, _) ->
2646+
(match dest_val.value_desc with
2647+
| IRVariable var_name | IRTempVariable var_name ->
2648+
Hashtbl.replace ctx.declared_via_ir var_name ()
2649+
| _ -> ());
26172650
(* Only user variables (IRVariable) need var_ prefix, not compiler temps (IRTempVariable) *)
26182651
(match dest_val.value_desc with
26192652
| IRVariable var_name ->

tests/test_perf_event_attach.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@ fn main() -> i32 {
580580
var details = read_details(cache)
581581
var snapshot = read_group(cache)
582582
print("raw=%lld scaled=%lld group=%u", raw, details.scaled, snapshot.count)
583+
var i = 0
584+
while (i < snapshot.count) {
585+
print("id=%llu value=%lld", snapshot.ids[i], snapshot.values[i])
586+
i = i + 1
587+
}
583588
detach(branch)
584589
detach(cache)
585590
detach(prog)
@@ -599,6 +604,11 @@ fn main() -> i32 {
599604
(contains_substr code "PERF_FORMAT_ID" && contains_substr code "PERF_FORMAT_GROUP");
600605
check bool "group values are multiplex scaled" true
601606
(contains_substr code "ks_scale_perf_count(group.values[i].value")
607+
;
608+
check bool "array field snapshots are copied before indexing" true
609+
(contains_substr code "memcpy(__field_access_");
610+
check bool "array snapshot indexing dereferences element pointer" true
611+
(contains_substr code "*__array_ptr_")
602612

603613
let test_perf_group_too_large_static_group_rejected () =
604614
Unix.putenv "KERNELSCRIPT_PERF_GROUP_MAX_EVENTS" "4";

tests/test_userspace_for_codegen.ml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,37 @@ fn main() -> i32 {
357357
with
358358
| exn -> fail ("Test failed with exception: " ^ Printexc.to_string exn)
359359

360+
(** Test 9: A later variable declaration with the same name as a for counter
361+
should own the declaration instead of producing a duplicate function-scope
362+
predeclaration. *)
363+
let test_for_counter_reused_by_later_var_decl () =
364+
let program_text = {|
365+
@xdp fn test(ctx: *xdp_md) -> xdp_action {
366+
return 2
367+
}
368+
369+
fn main() -> i32 {
370+
var total = 0
371+
for (i in 0..3) {
372+
total = total + i
373+
}
374+
var i = 0
375+
while (i < 2) {
376+
i = i + 1
377+
}
378+
return 0
379+
}
380+
|} in
381+
382+
try
383+
let result = generate_userspace_code_from_program program_text "test_for_reuse" in
384+
check bool "for loop still generated" true (contains_pattern result "for.*var_i");
385+
check bool "later declaration generated once" true (contains_pattern result "uint32_t var_i = 0");
386+
check bool "no duplicate predeclaration for reused counter" false
387+
(contains_pattern result "uint32_t var_i;[\\s\\S]*uint32_t var_i = 0")
388+
with
389+
| exn -> fail ("Test failed with exception: " ^ Printexc.to_string exn)
390+
360391
(** All global function for statement codegen tests *)
361392
let global_function_for_codegen_tests = [
362393
"basic_for_loop_constant_bounds", `Quick, test_basic_for_loop_constant_bounds;
@@ -367,6 +398,7 @@ let global_function_for_codegen_tests = [
367398
"for_loop_zero_iterations", `Quick, test_for_loop_zero_iterations;
368399
"for_loop_in_helper_function", `Quick, test_for_loop_in_helper_function;
369400
"global_functions_vs_ebpf_differences", `Quick, test_global_functions_vs_ebpf_for_loop_differences;
401+
"for_counter_reused_by_later_var_decl", `Quick, test_for_counter_reused_by_later_var_decl;
370402
]
371403

372404
let () =

0 commit comments

Comments
 (0)