@@ -480,14 +480,15 @@ Value *IRBuilderBPF::CreateMapLookupElem(Value *ctx,
480
480
return ret;
481
481
}
482
482
483
- Value *IRBuilderBPF::CreatePerCpuMapSumElems (Value *ctx,
483
+ Value *IRBuilderBPF::CreatePerCpuMapAggElems (Value *ctx,
484
484
Map &map,
485
485
Value *key,
486
+ const SizedType &type,
486
487
const location &loc,
487
488
bool is_aot)
488
489
{
489
490
/*
490
- * int sum = 0;
491
+ * int ret = 0;
491
492
* int i = 0;
492
493
* while (i < nr_cpus) {
493
494
* int * cpu_value = map_lookup_percpu_elem(map, key, i);
@@ -498,25 +499,25 @@ Value *IRBuilderBPF::CreatePerCpuMapSumElems(Value *ctx,
498
499
* debug("No cpu found for cpu id: %lu", i) // Mostly for AOT
499
500
* break;
500
501
* }
501
- * sum += *cpu_value;
502
+ * // Get the sum, min, or max value
502
503
* i++;
503
504
* }
504
- * return sum ;
505
+ * return ret ;
505
506
*/
506
507
507
508
assert (ctx && ctx->getType () == GET_PTR_TY ());
508
509
509
510
const std::string &map_name = map.ident ;
510
511
511
- AllocaInst *sum = CreateAllocaBPF (getInt64Ty (), " sum " );
512
+ AllocaInst *ret = CreateAllocaBPF (getInt64Ty (), " ret " );
512
513
AllocaInst *i = CreateAllocaBPF (getInt32Ty (), " i" );
513
514
514
515
// Set a large upper bound if we don't know the number of cpus
515
516
// when generating the instructions
516
517
int nr_cpus = is_aot ? 1024 : bpftrace_.get_num_possible_cpus ();
517
518
518
519
CreateStore (getInt32 (0 ), i);
519
- CreateStore (getInt64 (0 ), sum );
520
+ CreateStore (getInt64 (0 ), ret );
520
521
521
522
Function *parent = GetInsertBlock ()->getParent ();
522
523
BasicBlock *while_cond = BasicBlock::Create (module_.getContext (),
@@ -560,10 +561,16 @@ Value *IRBuilderBPF::CreatePerCpuMapSumElems(Value *ctx,
560
561
SetInsertPoint (lookup_success_block);
561
562
// createMapLookup returns an u8*
562
563
auto *cast = CreatePointerCast (call, getInt64Ty ()->getPointerTo (), " cast" );
563
- // sum += cpu_value;
564
- CreateStore (CreateAdd (CreateLoad (getInt64Ty (), cast),
565
- CreateLoad (getInt64Ty (), sum)),
566
- sum);
564
+
565
+ if (type.IsSumTy () || type.IsCountTy ()) {
566
+ createPerCpuSum (ret, cast);
567
+ } else if (type.IsMaxTy ()) {
568
+ createPerCpuMinMax (ret, cast, true );
569
+ } else if (type.IsMinTy ()) {
570
+ createPerCpuMinMax (ret, cast, false );
571
+ } else {
572
+ LOG (BUG) << " Unsupported map aggregation type: " << type;
573
+ }
567
574
568
575
// ++i;
569
576
CreateStore (CreateAdd (CreateLoad (getInt32Ty (), i), getInt32 (1 )), i);
@@ -603,9 +610,50 @@ Value *IRBuilderBPF::CreatePerCpuMapSumElems(Value *ctx,
603
610
SetInsertPoint (while_end);
604
611
605
612
CreateLifetimeEnd (i);
606
- Value *ret = CreateLoad (getInt64Ty (), sum);
607
- CreateLifetimeEnd (sum);
608
- return ret;
613
+ Value *ret_reg = CreateLoad (getInt64Ty (), ret);
614
+ CreateLifetimeEnd (ret);
615
+ return ret_reg;
616
+ }
617
+
618
+ void IRBuilderBPF::createPerCpuSum (AllocaInst *ret, Value *cpu_value)
619
+ {
620
+ CreateStore (CreateAdd (CreateLoad (getInt64Ty (), cpu_value),
621
+ CreateLoad (getInt64Ty (), ret)),
622
+ ret);
623
+ }
624
+
625
+ void IRBuilderBPF::createPerCpuMinMax (AllocaInst *ret,
626
+ Value *cpu_value,
627
+ bool is_max)
628
+ {
629
+ Function *parent = GetInsertBlock ()->getParent ();
630
+ BasicBlock *success_block = BasicBlock::Create (module_.getContext (),
631
+ " min_max_success" ,
632
+ parent);
633
+ BasicBlock *merge_block = BasicBlock::Create (module_.getContext (),
634
+ " min_max_merge" ,
635
+ parent);
636
+ Value *condition;
637
+
638
+ if (is_max) {
639
+ condition = CreateICmpSGT (CreateLoad (getInt64Ty (), cpu_value),
640
+ CreateLoad (getInt64Ty (), ret),
641
+ " max_cond" );
642
+ } else {
643
+ condition = CreateICmpSLT (CreateLoad (getInt64Ty (), cpu_value),
644
+ CreateLoad (getInt64Ty (), ret),
645
+ " min_cond" );
646
+ }
647
+ CreateCondBr (condition, success_block, merge_block);
648
+
649
+ SetInsertPoint (success_block);
650
+
651
+ // ret = cpu_value;
652
+ CreateStore (CreateLoad (getInt64Ty (), cpu_value), ret);
653
+
654
+ CreateBr (merge_block);
655
+
656
+ SetInsertPoint (merge_block);
609
657
}
610
658
611
659
void IRBuilderBPF::CreateMapUpdateElem (Value *ctx,
0 commit comments