libmemtrack: Add HIDL support for Memtrack

Bug: 31180823
Change-Id: I9be61e17028baa218e7b4aea9383109360687a30
Signed-off-by: Ruchi Kandoi <kandoiruchi@google.com>
This commit is contained in:
Ruchi Kandoi 2016-10-03 16:56:02 -07:00
parent 03a7950fda
commit 6e5739d671
5 changed files with 176 additions and 220 deletions

View file

@ -2,13 +2,18 @@
cc_library_shared {
name: "libmemtrack",
srcs: ["memtrack.c"],
srcs: ["memtrack.cpp"],
export_include_dirs: ["include"],
local_include_dirs: ["include"],
include_dirs: ["hardware/libhardware/include"],
shared_libs: [
"libhardware",
"liblog",
"libbase",
"libhidl",
"libhwbinder",
"libutils",
"android.hardware.memtrack@1.0",
],
cflags: [
"-Wall",

View file

@ -34,16 +34,6 @@ extern "C" {
*/
struct memtrack_proc;
/**
* memtrack_init
*
* Must be called once before calling any other functions. After this function
* is called, everything else is thread-safe.
*
* Returns 0 on success, -errno on error.
*/
int memtrack_init(void);
/**
* memtrack_proc_new
*

View file

@ -1,203 +0,0 @@
/*
* Copyright (C) 2013 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.
*/
#define LOG_TAG "memtrack"
#include <memtrack/memtrack.h>
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <android/log.h>
#include <hardware/memtrack.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
static const memtrack_module_t *module;
struct memtrack_proc {
pid_t pid;
struct memtrack_proc_type {
enum memtrack_type type;
size_t num_records;
size_t allocated_records;
struct memtrack_record *records;
} types[MEMTRACK_NUM_TYPES];
};
int memtrack_init(void)
{
int err;
if (module) {
return 0;
}
err = hw_get_module(MEMTRACK_HARDWARE_MODULE_ID,
(hw_module_t const**)&module);
if (err) {
ALOGE("Couldn't load %s module (%s)", MEMTRACK_HARDWARE_MODULE_ID,
strerror(-err));
return err;
}
return module->init(module);
}
struct memtrack_proc *memtrack_proc_new(void)
{
if (!module) {
return NULL;
}
return calloc(sizeof(struct memtrack_proc), 1);
}
void memtrack_proc_destroy(struct memtrack_proc *p)
{
enum memtrack_type i;
if (p) {
for (i = 0; i < MEMTRACK_NUM_TYPES; i++) {
free(p->types[i].records);
}
}
free(p);
}
static int memtrack_proc_get_type(struct memtrack_proc_type *t,
pid_t pid, enum memtrack_type type)
{
size_t num_records = t->num_records;
int ret;
retry:
ret = module->getMemory(module, pid, type, t->records, &num_records);
if (ret) {
t->num_records = 0;
return ret;
}
if (num_records > t->allocated_records) {
/* Need more records than allocated */
free(t->records);
t->records = calloc(sizeof(*t->records), num_records);
if (!t->records) {
return -ENOMEM;
}
t->allocated_records = num_records;
goto retry;
}
t->num_records = num_records;
return 0;
}
/* TODO: sanity checks on return values from HALs:
* make sure no records have invalid flags set
* - unknown flags
* - too many flags of a single category
* - missing ACCOUNTED/UNACCOUNTED
* make sure there are not overlapping SHARED and SHARED_PSS records
*/
static int memtrack_proc_sanity_check(struct memtrack_proc *p)
{
(void)p;
return 0;
}
int memtrack_proc_get(struct memtrack_proc *p, pid_t pid)
{
enum memtrack_type i;
if (!module) {
return -EINVAL;
}
if (!p) {
return -EINVAL;
}
p->pid = pid;
for (i = 0; i < MEMTRACK_NUM_TYPES; i++) {
memtrack_proc_get_type(&p->types[i], pid, i);
}
return memtrack_proc_sanity_check(p);
}
static ssize_t memtrack_proc_sum(struct memtrack_proc *p,
enum memtrack_type types[], size_t num_types,
unsigned int flags)
{
ssize_t sum = 0;
size_t i;
size_t j;
for (i = 0; i < num_types; i++) {
enum memtrack_type type = types[i];
for (j = 0; j < p->types[type].num_records; j++) {
if ((p->types[type].records[j].flags & flags) == flags) {
sum += p->types[type].records[j].size_in_bytes;
}
}
}
return sum;
}
ssize_t memtrack_proc_graphics_total(struct memtrack_proc *p)
{
enum memtrack_type types[] = { MEMTRACK_TYPE_GRAPHICS };
return memtrack_proc_sum(p, types, ARRAY_SIZE(types), 0);
}
ssize_t memtrack_proc_graphics_pss(struct memtrack_proc *p)
{
enum memtrack_type types[] = { MEMTRACK_TYPE_GRAPHICS };
return memtrack_proc_sum(p, types, ARRAY_SIZE(types),
MEMTRACK_FLAG_SMAPS_UNACCOUNTED);
}
ssize_t memtrack_proc_gl_total(struct memtrack_proc *p)
{
enum memtrack_type types[] = { MEMTRACK_TYPE_GL };
return memtrack_proc_sum(p, types, ARRAY_SIZE(types), 0);
}
ssize_t memtrack_proc_gl_pss(struct memtrack_proc *p)
{
enum memtrack_type types[] = { MEMTRACK_TYPE_GL };
return memtrack_proc_sum(p, types, ARRAY_SIZE(types),
MEMTRACK_FLAG_SMAPS_UNACCOUNTED);
}
ssize_t memtrack_proc_other_total(struct memtrack_proc *p)
{
enum memtrack_type types[] = { MEMTRACK_TYPE_MULTIMEDIA,
MEMTRACK_TYPE_CAMERA,
MEMTRACK_TYPE_OTHER };
return memtrack_proc_sum(p, types, ARRAY_SIZE(types), 0);
}
ssize_t memtrack_proc_other_pss(struct memtrack_proc *p)
{
enum memtrack_type types[] = { MEMTRACK_TYPE_MULTIMEDIA,
MEMTRACK_TYPE_CAMERA,
MEMTRACK_TYPE_OTHER };
return memtrack_proc_sum(p, types, ARRAY_SIZE(types),
MEMTRACK_FLAG_SMAPS_UNACCOUNTED);
}

170
libmemtrack/memtrack.cpp Normal file
View file

@ -0,0 +1,170 @@
/*
* Copyright (C) 2013 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.
*/
#define LOG_TAG "memtrack"
#include <android/hardware/memtrack/1.0/IMemtrack.h>
#include <memtrack/memtrack.h>
#include <errno.h>
#include <malloc.h>
#include <vector>
#include <string.h>
#include <mutex>
using android::hardware::memtrack::V1_0::IMemtrack;
using android::hardware::memtrack::V1_0::MemtrackType;
using android::hardware::memtrack::V1_0::MemtrackRecord;
using android::hardware::memtrack::V1_0::MemtrackFlag;
using android::hardware::memtrack::V1_0::MemtrackStatus;
using android::hardware::hidl_vec;
struct memtrack_proc_type {
MemtrackType type;
std::vector<MemtrackRecord> records;
};
struct memtrack_proc {
pid_t pid;
memtrack_proc_type types[static_cast<int>(MemtrackType::NUM_TYPES)];
};
//TODO(b/31632518)
static android::sp<IMemtrack> get_instance() {
static android::sp<IMemtrack> module = IMemtrack::getService("memtrack");
if (module == nullptr) {
ALOGE("Couldn't load memtrack module");
}
return module;
}
memtrack_proc *memtrack_proc_new(void)
{
return new memtrack_proc();
}
void memtrack_proc_destroy(memtrack_proc *p)
{
delete(p);
}
static int memtrack_proc_get_type(memtrack_proc_type *t,
pid_t pid, MemtrackType type)
{
int err = 0;
android::sp<IMemtrack> memtrack = get_instance();
if (memtrack == nullptr)
return -1;
memtrack->getMemory(pid, type,
[&t, &err](MemtrackStatus status, hidl_vec<MemtrackRecord> records) {
if (status != MemtrackStatus::SUCCESS) {
err = -1;
t->records.resize(0);
}
t->records.resize(records.size());
for (size_t i = 0; i < records.size(); i++) {
t->records[i].sizeInBytes = records[i].sizeInBytes;
t->records[i].flags = records[i].flags;
}
});
return err;
}
/* TODO: sanity checks on return values from HALs:
* make sure no records have invalid flags set
* - unknown flags
* - too many flags of a single category
* - missing ACCOUNTED/UNACCOUNTED
* make sure there are not overlapping SHARED and SHARED_PSS records
*/
static int memtrack_proc_sanity_check(memtrack_proc* /*p*/)
{
return 0;
}
int memtrack_proc_get(memtrack_proc *p, pid_t pid)
{
if (!p) {
return -EINVAL;
}
p->pid = pid;
for (uint32_t i = 0; i < (uint32_t)MemtrackType::NUM_TYPES; i++) {
int ret = memtrack_proc_get_type(&p->types[i], pid, (MemtrackType)i);
if (ret != 0)
return ret;
}
return memtrack_proc_sanity_check(p);
}
static ssize_t memtrack_proc_sum(memtrack_proc *p,
const std::vector<MemtrackType>& types, uint32_t flags)
{
ssize_t sum = 0;
for (size_t i = 0; i < types.size(); i++) {
memtrack_proc_type type = p->types[static_cast<int>(types[i])];
std::vector<MemtrackRecord> records = type.records;
for (size_t j = 0; j < records.size(); j++) {
if ((records[j].flags & flags) == flags) {
sum += records[j].sizeInBytes;
}
}
}
return sum;
}
ssize_t memtrack_proc_graphics_total(memtrack_proc *p)
{
std::vector<MemtrackType> types = {MemtrackType::GRAPHICS};
return memtrack_proc_sum(p, types, 0);
}
ssize_t memtrack_proc_graphics_pss(memtrack_proc *p)
{
std::vector<MemtrackType> types = { MemtrackType::GRAPHICS };
return memtrack_proc_sum(p, types,
(uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
}
ssize_t memtrack_proc_gl_total(memtrack_proc *p)
{
std::vector<MemtrackType> types = { MemtrackType::GL };
return memtrack_proc_sum(p, types, 0);
}
ssize_t memtrack_proc_gl_pss(memtrack_proc *p)
{
std::vector<MemtrackType> types = { MemtrackType::GL };
return memtrack_proc_sum(p, types,
(uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
}
ssize_t memtrack_proc_other_total(memtrack_proc *p)
{
std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
MemtrackType::CAMERA, MemtrackType::OTHER };
return memtrack_proc_sum(p, types, 0);
}
ssize_t memtrack_proc_other_pss(memtrack_proc *p)
{
std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
MemtrackType::CAMERA, MemtrackType::OTHER };
return memtrack_proc_sum(p, types,
(uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
}

View file

@ -82,12 +82,6 @@ int main(int argc, char *argv[])
(void)argc;
(void)argv;
ret = memtrack_init();
if (ret < 0) {
fprintf(stderr, "failed to initialize HAL: %s (%d)\n", strerror(-ret), ret);
exit(EXIT_FAILURE);
}
ret = pm_kernel_create(&ker);
if (ret) {
fprintf(stderr, "Error creating kernel interface -- "