Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e04680d1 authored by Sandeep Patil's avatar Sandeep Patil
Browse files

meminfo: Pull ReadVmallocInfo() out of the SysMemInfo class.



There is no need for the vmalloc reader to be in the sysmeminfo object.
The caller has to create the SysMemInfo object for no reason right now.

Bug: 111694435
Test: libmeminfo_test 1

Change-Id: I263c08c169ea6caf2221bfb915b26ef3c0b1c20c
Signed-off-by: default avatarSandeep Patil <sspatil@google.com>
parent c8903eeb
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ class SysMemInfo final {
    // in vmalloc area by the kernel.
    // Note that this deliberately ignores binder buffers. They are _always_
    // mapped in a process and are counted for in each process.
    uint64_t ReadVmallocInfo(const std::string& path = "/proc/vmallocinfo");
    uint64_t ReadVmallocInfo();

    // getters
    uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; }
@@ -84,5 +84,10 @@ class SysMemInfo final {
                     std::function<void(const std::string&, uint64_t)> store_val);
};

// Parse /proc/vmallocinfo and return total physical memory mapped
// in vmalloc area by the kernel. Note that this deliberately ignores binder buffers. They are
// _always_ mapped in a process and are counted for in each process.
uint64_t ReadVmallocInfo(const std::string& path = "/proc/vmallocinfo");

}  // namespace meminfo
}  // namespace android
+1 −2
Original line number Diff line number Diff line
@@ -455,8 +455,7 @@ static void BM_VmallocInfo_new(benchmark::State& state) {
    std::string vmallocinfo =
            ::android::base::StringPrintf("%s/testdata1/vmallocinfo", exec_dir.c_str());
    for (auto _ : state) {
        SysMemInfo smi;
        CHECK_EQ(smi.ReadVmallocInfo(vmallocinfo), 29884416);
        CHECK_EQ(::android::meminfo::ReadVmallocInfo(vmallocinfo), 29884416);
    }
}
BENCHMARK(BM_VmallocInfo_new);
+4 −8
Original line number Diff line number Diff line
@@ -844,8 +844,7 @@ TEST(SysMemInfoParser, TestVmallocInfoNoMemory) {
    ASSERT_TRUE(::android::base::WriteStringToFd(vmallocinfo, tf.fd));
    std::string file = std::string(tf.path);

    SysMemInfo smi;
    EXPECT_EQ(smi.ReadVmallocInfo(file), 0);
    EXPECT_EQ(ReadVmallocInfo(file), 0);
}

TEST(SysMemInfoParser, TestVmallocInfoKernel) {
@@ -857,8 +856,7 @@ TEST(SysMemInfoParser, TestVmallocInfoKernel) {
    ASSERT_TRUE(::android::base::WriteStringToFd(vmallocinfo, tf.fd));
    std::string file = std::string(tf.path);

    SysMemInfo smi;
    EXPECT_EQ(smi.ReadVmallocInfo(file), getpagesize());
    EXPECT_EQ(ReadVmallocInfo(file), getpagesize());
}

TEST(SysMemInfoParser, TestVmallocInfoModule) {
@@ -870,8 +868,7 @@ TEST(SysMemInfoParser, TestVmallocInfoModule) {
    ASSERT_TRUE(::android::base::WriteStringToFd(vmallocinfo, tf.fd));
    std::string file = std::string(tf.path);

    SysMemInfo smi;
    EXPECT_EQ(smi.ReadVmallocInfo(file), 6 * getpagesize());
    EXPECT_EQ(ReadVmallocInfo(file), 6 * getpagesize());
}

TEST(SysMemInfoParser, TestVmallocInfoAll) {
@@ -888,8 +885,7 @@ TEST(SysMemInfoParser, TestVmallocInfoAll) {
    ASSERT_TRUE(::android::base::WriteStringToFd(vmallocinfo, tf.fd));
    std::string file = std::string(tf.path);

    SysMemInfo smi;
    EXPECT_EQ(smi.ReadVmallocInfo(file), 7 * getpagesize());
    EXPECT_EQ(ReadVmallocInfo(file), 7 * getpagesize());
}

int main(int argc, char** argv) {
+38 −30
Original line number Diff line number Diff line
@@ -79,36 +79,8 @@ bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, std::vector<u
    });
}

uint64_t SysMemInfo::ReadVmallocInfo(const std::string& path) {
    uint64_t vmalloc_total = 0;
    auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
    if (fp == nullptr) {
        return vmalloc_total;
    }

    char line[1024];
    while (fgets(line, 1024, fp.get()) != nullptr) {
        // We are looking for lines like
        // 0x0000000000000000-0x0000000000000000   12288 drm_property_create_blob+0x44/0xec pages=2
        // vmalloc 0x0000000000000000-0x0000000000000000    8192
        // wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc Notice that if the caller is
        // coming from a module, the kernel prints and extra "[module_name]" after the address and
        // the symbol of the call site. This means we can't use the old sscanf() method of getting
        // the # of pages.
        char* p_start = strstr(line, "pages=");
        if (p_start == nullptr) {
            // we didn't find anything
            continue;
        }

        p_start = strtok(p_start, " ");
        long nr_pages;
        if (sscanf(p_start, "pages=%ld", &nr_pages) == 1) {
            vmalloc_total += (nr_pages * getpagesize());
        }
    }

    return vmalloc_total;
uint64_t SysMemInfo::ReadVmallocInfo() {
    return ::android::meminfo::ReadVmallocInfo();
}

// TODO: Delete this function if it can't match up with the c-like implementation below.
@@ -263,5 +235,41 @@ bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_d
    return false;
}

// Public methods
uint64_t ReadVmallocInfo(const std::string& path) {
    uint64_t vmalloc_total = 0;
    auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path.c_str(), "re"), fclose};
    if (fp == nullptr) {
        return vmalloc_total;
    }

    char* line = nullptr;
    size_t line_alloc = 0;
    while (getline(&line, &line_alloc, fp.get()) > 0) {
        // We are looking for lines like
        //
        // 0x0000000000000000-0x0000000000000000   12288 drm_property_create_blob+0x44/0xec pages=2 vmalloc
        // 0x0000000000000000-0x0000000000000000    8192 wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc
        //
        // Notice that if the caller is coming from a module, the kernel prints and extra
        // "[module_name]" after the address and the symbol of the call site. This means we can't
        // use the old sscanf() method of getting the # of pages.
        char* p_start = strstr(line, "pages=");
        if (p_start == nullptr) {
            // we didn't find anything
            continue;
        }

        uint64_t nr_pages;
        if (sscanf(p_start, "pages=%" SCNu64 "", &nr_pages) == 1) {
            vmalloc_total += (nr_pages * getpagesize());
        }
    }

    free(line);

    return vmalloc_total;
}

}  // namespace meminfo
}  // namespace android