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

Commit f1a5bfe0 authored by Sandeep Patil's avatar Sandeep Patil Committed by android-build-merger
Browse files

Merge "libmeminfo: Add libmeminfo to gather global and per-process memory stats"

am: c68ac11c

Change-Id: Ia1e9e77b8de7770fa9839e3640d156bd1b5d766f
parents 48453018 c68ac11c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
../.clang-format-4
 No newline at end of file

libmeminfo/Android.bp

0 → 100644
+70 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

cc_defaults {
    name: "libmeminfo_defaults",
    cflags: [
        "-Wall",
        "-Werror",
    ],

    shared_libs: [
        "libbase",
        "liblog",
        "libprocinfo",
    ],
}

cc_library {
    name: "libmeminfo",
    defaults: ["libmeminfo_defaults"],
    export_include_dirs: ["include"],
    export_shared_lib_headers: ["libbase"],
    srcs: [
        "pageacct.cpp",
        "procmeminfo.cpp",
        "sysmeminfo.cpp",
    ],
}

cc_test {
    name: "libmeminfo_test",
    defaults: ["libmeminfo_defaults"],

    static_libs: [
        "libmeminfo",
        "libpagemap",
        "libbase",
        "liblog",
    ],

    srcs: [
        "libmeminfo_test.cpp"
    ],
}

cc_benchmark {
    name: "libmeminfo_benchmark",
    srcs: [
        "libmeminfo_benchmark.cpp",
    ],
    static_libs : [
        "libbase",
        "liblog",
        "libmeminfo",
        "libprocinfo",
    ],
}
+75 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sys/types.h>
#include <unistd.h>

#include <string>
#include <vector>

namespace android {
namespace meminfo {

struct MemUsage {
    uint64_t vss;
    uint64_t rss;
    uint64_t pss;
    uint64_t uss;

    uint64_t private_clean;
    uint64_t private_dirty;
    uint64_t shared_clean;
    uint64_t shared_dirty;

    MemUsage()
        : vss(0),
          rss(0),
          pss(0),
          uss(0),
          private_clean(0),
          private_dirty(0),
          shared_clean(0),
          shared_dirty(0) {}

    ~MemUsage() = default;

    void clear() {
        vss = rss = pss = uss = 0;
        private_clean = private_dirty = shared_clean = shared_dirty = 0;
    }
};

struct Vma {
    uint64_t start;
    uint64_t end;
    uint64_t offset;
    uint16_t flags;
    std::string name;

    Vma(uint64_t s, uint64_t e, uint64_t off, uint16_t f, const char* n)
        : start(s), end(e), offset(off), flags(f), name(n) {}
    ~Vma() = default;

    // Memory usage of this mapping.
    MemUsage usage;
    // Working set within this mapping.
    MemUsage wss;
};

}  // namespace meminfo
}  // namespace android
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sys/types.h>
#include <unistd.h>

#include <string>
#include <vector>

#include <android-base/unique_fd.h>

namespace android {
namespace meminfo {

class PageAcct final {
    // Class for per-page accounting by using kernel provided interfaces like
    // kpagecount, kpageflags etc.
  public:
    static bool KernelHasPageIdle() {
        return (access("/sys/kernel/mm/page_idle/bitmap", R_OK | W_OK) == 0);
    }

    bool InitPageAcct(bool pageidle_enable = false);
    bool PageFlags(uint64_t pfn, uint64_t* flags);
    bool PageMapCount(uint64_t pfn, uint64_t* mapcount);

    int IsPageIdle(uint64_t pfn);

    // The only way to create PageAcct object
    static PageAcct& Instance() {
        static PageAcct instance;
        return instance;
    }

    ~PageAcct() = default;

  private:
    PageAcct() : kpagecount_fd_(-1), kpageflags_fd_(-1), pageidle_fd_(-1) {}
    int MarkPageIdle(uint64_t pfn) const;
    int GetPageIdle(uint64_t pfn) const;

    // Non-copyable & Non-movable
    PageAcct(const PageAcct&) = delete;
    PageAcct& operator=(const PageAcct&) = delete;
    PageAcct& operator=(PageAcct&&) = delete;
    PageAcct(PageAcct&&) = delete;

    ::android::base::unique_fd kpagecount_fd_;
    ::android::base::unique_fd kpageflags_fd_;
    ::android::base::unique_fd pageidle_fd_;
};

}  // namespace meminfo
}  // namespace android
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sys/types.h>

#include <string>
#include <vector>

#include "meminfo.h"

namespace android {
namespace meminfo {

class ProcMemInfo final {
    // Per-process memory accounting
  public:
    ProcMemInfo(pid_t pid, bool get_wss = false);

    const std::vector<Vma>& Maps();
    const MemUsage& Usage();
    const MemUsage& Wss();

    bool WssReset();
    ~ProcMemInfo() = default;

  private:
    bool ReadMaps(bool get_wss);
    bool ReadVmaStats(int pagemap_fd, Vma& vma, bool get_wss);

    pid_t pid_;
    bool get_wss_;

    std::vector<Vma> maps_;

    MemUsage usage_;
    MemUsage wss_;
};

}  // namespace meminfo
}  // namespace android
Loading