All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matt Pallissard <matt@pallissard.net>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Yonghong Song <yhs@fb.com>, bpf <bpf@vger.kernel.org>
Subject: Re: Accessing mm_rss_stat fields with btf/BPF_CORE_READ_INTO
Date: Mon, 22 Jun 2020 08:01:28 -0700	[thread overview]
Message-ID: <20200622150128.hjwe3uak2sy7po22@matt-gen-desktop-p01.matt.pallissard.net> (raw)
In-Reply-To: <20200621154428.pf6foowywrq3wxt2@matt-gen-laptop-p01>



On 2020-06-21T08:44:28 -0700, Matt Pallissard wrote:
>
>
> On 2020-06-20T20:29:43 -0700, Andrii Nakryiko wrote:
> > On Sat, Jun 20, 2020 at 1:07 PM Matt Pallissard <matt@pallissard.net> wrote:
> > >
> > >
> > >
> > >
> > > On 2020-06-20T11:11:55 -0700, Yonghong Song wrote:
> > > >
> > > >
> > > > On 6/20/20 9:22 AM, Matt Pallissard wrote:
> > > > > New to bpf here.
> > > > >
> > > > > I'm trying to read values out of of mm_struct.  I have code like this;
> > > > >
> > > > > unsigned long i[10] = {};
> > > > > struct task_struct *t;
> > > > > struct mm_rss_stat *rss;
> > > > >
> > > > > t = (struct task_struct *)bpf_get_current_task();
> > > > > BPF_CORE_READ_INTO(&rss, t, mm, rss_stat);
> > > > > BPF_CORE_READ_INTO(i, rss, count);
> > > > >
> > > > > However, all values in `i` appear to be 0 (i[MM_FILEPAGES], etc), as if no data gets copied.  I'm about 100% confident that this is caused by a glaring oversight on my part.
> > > >
> > > > Maybe you want to check the return value of BPF_CORE_READ_INTO.
> > > > Underlying it is using bpf_probe_read and bpf_probe_read may fail e.g., due
> > > > to major fault.
> > >
> > > Doh, I should have known to check the return codes!  Yes, it was failing.  I knew I was overlooking something trivial.
> > >
> >
> > I wrote exactly such piece of code a while ago. Here's part of it for
> > reference, I think it will be helpful:
> >
> >   struct task_struct *task = (struct task_struct *)bpf_get_current_task();
> >   const struct mm_struct *mm = BPF_CORE_READ(task, mm);
> >
> >   if (mm) {
> >       u64 hiwater_rss = BPF_CORE_READ(mm, hiwater_rss);
> >       u64 file_pages = BPF_CORE_READ(mm, rss_stat.count[MM_FILEPAGES].counter);
> >       u64 anon_pages = BPF_CORE_READ(mm, rss_stat.count[MM_ANONPAGES].counter);
> >       u64 shmem_pages = BPF_CORE_READ(mm,
> > rss_stat.count[MM_SHMEMPAGES].counter);
> >       u64 active_rss = file_pages + anon_pages + shmem_pages;
> >       /* ... */
>
> Thank you,
>
> After realizing that I was referencing the struct incorrectly, I wound up with a similar block of code.  However, as I started testing it against /proc/pid/smaps[,_rollup] I noticed that my numbers didn't match up.  Always smaller.
>
> I took a quick glance at fs/proc/task_mmu.c.  I think I'll have to walk some sort of accounting structure.


I started to take a hard look at fs/proc/task_mmu.c.  With all the locking, globals, and compile-time constants, I'm not sure that it's even possible to correctly walk `vm_area_struct` in bpf.

If anyone has suggestions for getting memory numbers from an entire process, not just a task/thread, I'd love to hear them.  If not, I'll pursue this on my own.


Matt Pallissard

  reply	other threads:[~2020-06-22 15:01 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-20 16:22 Accessing mm_rss_stat fields with btf/BPF_CORE_READ_INTO Matt Pallissard
2020-06-20 18:11 ` Yonghong Song
2020-06-20 20:06   ` Matt Pallissard
2020-06-21  3:29     ` Andrii Nakryiko
2020-06-21 15:44       ` Matt Pallissard
2020-06-22 15:01         ` Matt Pallissard [this message]
2020-06-22 16:20           ` Andrii Nakryiko
2020-06-22 17:19             ` Matt Pallissard
2020-06-22 22:09               ` Andrii Nakryiko
2020-06-23 14:54                 ` Matt Pallissard
2020-06-23 16:35                   ` Yonghong Song
2020-06-23 17:58                     ` Andrii Nakryiko
2020-06-23 18:11                       ` Matt Pallissard
2020-06-23 18:36                         ` Andrii Nakryiko
2020-06-23 22:05                           ` Matt Pallissard
2020-06-23 22:13                             ` Andrii Nakryiko
2020-06-24 15:51                               ` Matt Pallissard
2020-06-23 22:16                             ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200622150128.hjwe3uak2sy7po22@matt-gen-desktop-p01.matt.pallissard.net \
    --to=matt@pallissard.net \
    --cc=andrii.nakryiko@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.