Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bpf): cleanup initialising structs and nested ifs #1444

Merged
merged 1 commit into from
May 18, 2024

Conversation

sthaha
Copy link
Collaborator

@sthaha sthaha commented May 17, 2024

This commit changes all declarae and then initialise code of form below

struct foobar x; x.a = a; x.b = b;

to

struct foobar x = { .a = a, .b = b, };

The difference in the code produced is as follows

;  new_pid_key.pid = cur_pid;                             | pid_time_t new_pid_key = {.pid = cur_pid };
  0: 63 6a fc ff 00 00 00 00	*(u32 *)(r10 - 0x4) = r6  |  0:	79 a1 68 ff 00 00 00 00	r1 = *(u64 *)(r10 - 0x98)
  1: bf a2 00 00 00 00 00 00	r2 = r10                  |  1:	63 1a fc ff 00 00 00 00	*(u32 *)(r10 - 0x4) = r1
  2: 07 02 00 00 fc ff ff ff	r2 += -0x4                |  2:	bf a2 00 00 00 00 00 00	r2 = r10
  3: bf a3 00 00 00 00 00 00	r3 = r10                  |  3:	07 02 00 00 f0 ff ff ff	r2 += -0x10
  4: 07 03 00 00 70 ff ff ff	r3 += -0x90

Modifies calc_delta to remove unnecessary pointer deferencing of second arg - val

@sthaha sthaha requested review from dave-tucker and rootfs May 17, 2024 01:59
This commit changes all declarae and then initialise code of form below

```
struct foobar x;
x.a = a;
x.b = b;
```

to
```
struct foobar x = { .a = a, .b = b, };
```
The difference in the code produced is as follows

```
;  new_pid_key.pid = cur_pid;                             | pid_time_t new_pid_key = {.pid = cur_pid };
  0: 63 6a fc ff 00 00 00 00	*(u32 *)(r10 - 0x4) = r6  |  0:	79 a1 68 ff 00 00 00 00	r1 = *(u64 *)(r10 - 0x98)
  1: bf a2 00 00 00 00 00 00	r2 = r10                  |  1:	63 1a fc ff 00 00 00 00	*(u32 *)(r10 - 0x4) = r1
  2: 07 02 00 00 fc ff ff ff	r2 += -0x4                |  2:	bf a2 00 00 00 00 00 00	r2 = r10
  3: bf a3 00 00 00 00 00 00	r3 = r10                  |  3:	07 02 00 00 f0 ff ff ff	r2 += -0x10
  4: 07 03 00 00 70 ff ff ff	r3 += -0x90

```

Modifies calc_delta to remove unnecessary pointer deferencing of second
arg - `val`

Signed-off-by: Sunil Thaha <sthaha@redhat.com>
(cherry picked from commit ac43e26)
bpf_map_update_elem(&pid_time, &new_pid_key, &cur_ts, BPF_NOEXIST);
return cpu_time;
}

static inline u64 calc_delta(u64 *prev_val, u64 *val)
static inline u64 calc_delta(u64 *prev_val, u64 val)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that the current implementation already assumes u64 *val is not nil.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you're here wouldn't it be better to make this calc_delta(u64 prev_val, u64 val)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the original implementation handles prev_val (pointer) being nil/null. The caller may

  1. calc_delta(nil, 20) -> 0
  2. calc_delta(0xABCD (80) , 20) -> 0
  3. calc_delta(0xABCD (8) , 20) -> 12

So the caller does not worry about previous record being present. Also note, this is inlined.

@sthaha sthaha requested a review from vimalk78 May 17, 2024 04:05
Copy link
Collaborator

@dave-tucker dave-tucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small nit but otherwise LGTM

bpf_map_update_elem(&pid_time, &new_pid_key, &cur_ts, BPF_NOEXIST);
return cpu_time;
}

static inline u64 calc_delta(u64 *prev_val, u64 *val)
static inline u64 calc_delta(u64 *prev_val, u64 val)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you're here wouldn't it be better to make this calc_delta(u64 prev_val, u64 val)?

@rootfs rootfs merged commit 5f59172 into sustainable-computing-io:main May 18, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants