Reapply "Update bpf test for type safety"
am: 44ad5cbf69

Change-Id: I376688601ad3460cc68a3ad10246ece5a65e4138
diff --git a/test/bpf_load_tp_prog.c b/test/bpf_load_tp_prog.c
index a8b4860..84438bd 100644
--- a/test/bpf_load_tp_prog.c
+++ b/test/bpf_load_tp_prog.c
@@ -19,13 +19,8 @@
 #include <stdint.h>
 #include <bpf_helpers.h>
 
-struct bpf_map_def SEC("maps") cpu_pid = {
-        .type = BPF_MAP_TYPE_ARRAY,
-        .key_size = sizeof(int),
-        .value_size = sizeof(uint32_t),
-        /* Assume max of 1024 CPUs */
-        .max_entries = 1024,
-};
+/* Assume max of 1024 CPUs */
+DEFINE_BPF_MAP(cpu_pid_map, ARRAY, int, uint32_t, 1024)
 
 struct switch_args {
     unsigned long long ignore;
@@ -46,7 +41,7 @@
     key = bpf_get_smp_processor_id();
     val = args->next_pid;
 
-    bpf_map_update_elem(&cpu_pid, &key, &val, BPF_ANY);
+    bpf_cpu_pid_map_update_elem(&key, &val, BPF_ANY);
     return 0;
 }
 
diff --git a/time_in_state.c b/time_in_state.c
index ddf8900..618ebd4 100644
--- a/time_in_state.c
+++ b/time_in_state.c
@@ -16,32 +16,16 @@
 
 #include <bpf_helpers.h>
 
-struct time_key {
+typedef struct {
     uint32_t uid;
     uint32_t freq;
-};
+} time_key;
 
-struct bpf_map_def SEC("maps") uid_times = {
-        .type = BPF_MAP_TYPE_PERCPU_HASH,
-        .key_size = sizeof(struct time_key),
-        .value_size = sizeof(uint64_t),
-        .max_entries = 10240,
-};
+DEFINE_BPF_MAP(uid_times_map, PERCPU_HASH, time_key, uint64_t, 10240)
+DEFINE_BPF_MAP(cpu_last_update_map, PERCPU_ARRAY, uint32_t, uint64_t, 1)
 
-struct bpf_map_def SEC("maps") cpu_last_update = {
-        .type = BPF_MAP_TYPE_PERCPU_ARRAY,
-        .key_size = sizeof(int),
-        .value_size = sizeof(uint64_t),
-        .max_entries = 1,
-};
-
-struct bpf_map_def SEC("maps") cpu_freq = {
-        .type = BPF_MAP_TYPE_ARRAY,
-        .key_size = sizeof(int),
-        .value_size = sizeof(uint32_t),
-        /* Assume max of 1024 CPUs */
-        .max_entries = 1024,
-};
+/* Assume max of 1024 CPUs */
+DEFINE_BPF_MAP(cpu_freq_map, ARRAY, uint32_t, uint32_t, 1024)
 
 struct switch_args {
     unsigned long long ignore;
@@ -57,20 +41,20 @@
 SEC("tracepoint/sched/sched_switch")
 int tp_sched_switch(struct switch_args* args) {
     uint32_t zero = 0;
-    uint64_t* last = bpf_map_lookup_elem(&cpu_last_update, &zero);
+    uint64_t* last = bpf_cpu_last_update_map_lookup_elem(&zero);
     if (!last) return 0;
     uint64_t old_last = *last;
     uint64_t time = bpf_ktime_get_ns();
     *last = time;
     uint32_t cpu = bpf_get_smp_processor_id();
-    uint32_t* freq = bpf_map_lookup_elem(&cpu_freq, &cpu);
+    uint32_t* freq = bpf_cpu_freq_map_lookup_elem(&cpu);
     if (args->prev_pid && old_last && freq && *freq) {
         uint32_t uid = bpf_get_current_uid_gid();
-        struct time_key key = {.uid = uid, .freq = *freq};
-        uint64_t* tot_time = bpf_map_lookup_elem(&uid_times, &key);
+        time_key key = {.uid = uid, .freq = *freq};
+        uint64_t* tot_time = bpf_uid_times_map_lookup_elem(&key);
         uint64_t delta = time - old_last;
         if (!tot_time)
-            bpf_map_update_elem(&uid_times, &key, &delta, BPF_ANY);
+            bpf_uid_times_map_update_elem(&key, &delta, BPF_ANY);
         else
             *tot_time += delta;
     }
@@ -85,9 +69,9 @@
 
 SEC("tracepoint/power/cpu_frequency")
 int tp_cpufreq(struct cpufreq_args* args) {
-    unsigned int cpu = args->cpu_id;
+    uint32_t cpu = args->cpu_id;
     unsigned int new = args->state;
-    bpf_map_update_elem(&cpu_freq, &cpu, &new, BPF_ANY);
+    bpf_cpu_freq_map_update_elem(&cpu, &new, BPF_ANY);
     return 0;
 }