upload android base code part6
This commit is contained in:
parent
421e214c7d
commit
4e516ec6ed
35396 changed files with 9188716 additions and 0 deletions
40
android/system/core/libmemtrack/Android.bp
Normal file
40
android/system/core/libmemtrack/Android.bp
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2013 The Android Open Source Project
|
||||
|
||||
cc_library_shared {
|
||||
name: "libmemtrack",
|
||||
vendor_available: true,
|
||||
vndk: {
|
||||
enabled: true,
|
||||
},
|
||||
srcs: ["memtrack.cpp"],
|
||||
export_include_dirs: ["include"],
|
||||
local_include_dirs: ["include"],
|
||||
include_dirs: ["hardware/libhardware/include"],
|
||||
shared_libs: [
|
||||
"libhardware",
|
||||
"liblog",
|
||||
"libbase",
|
||||
"libhidlbase",
|
||||
"libhidltransport",
|
||||
"libhwbinder",
|
||||
"libutils",
|
||||
"android.hardware.memtrack@1.0",
|
||||
],
|
||||
cflags: [
|
||||
"-Wall",
|
||||
"-Werror",
|
||||
],
|
||||
}
|
||||
|
||||
cc_binary {
|
||||
name: "memtrack_test",
|
||||
srcs: ["memtrack_test.c"],
|
||||
shared_libs: [
|
||||
"libmemtrack",
|
||||
"libpagemap",
|
||||
],
|
||||
cflags: [
|
||||
"-Wall",
|
||||
"-Werror",
|
||||
],
|
||||
}
|
136
android/system/core/libmemtrack/include/memtrack/memtrack.h
Normal file
136
android/system/core/libmemtrack/include/memtrack/memtrack.h
Normal file
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef _LIBMEMTRACK_MEMTRACK_H_
|
||||
#define _LIBMEMTRACK_MEMTRACK_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* struct memtrack_proc
|
||||
*
|
||||
* an opaque handle to the memory stats on a process.
|
||||
* Created with memtrack_proc_new, destroyed by
|
||||
* memtrack_proc_destroy. Can be reused multiple times with
|
||||
* memtrack_proc_get.
|
||||
*/
|
||||
struct memtrack_proc;
|
||||
|
||||
/**
|
||||
* memtrack_proc_new
|
||||
*
|
||||
* Return a new handle to hold process memory stats.
|
||||
*
|
||||
* Returns NULL on error.
|
||||
*/
|
||||
struct memtrack_proc *memtrack_proc_new(void);
|
||||
|
||||
/**
|
||||
* memtrack_proc_destroy
|
||||
*
|
||||
* Free all memory associated with a process memory stats handle.
|
||||
*/
|
||||
void memtrack_proc_destroy(struct memtrack_proc *p);
|
||||
|
||||
/**
|
||||
* memtrack_proc_get
|
||||
*
|
||||
* Fill a process memory stats handle with data about the given pid. Can be
|
||||
* called on a handle that was just allocated with memtrack_proc_new,
|
||||
* or on a handle that has been previously passed to memtrack_proc_get
|
||||
* to replace the data with new data on the same or another process. It is
|
||||
* expected that the second call on the same handle should not require
|
||||
* allocating any new memory.
|
||||
*
|
||||
* Returns 0 on success, -errno on error.
|
||||
*/
|
||||
int memtrack_proc_get(struct memtrack_proc *p, pid_t pid);
|
||||
|
||||
/**
|
||||
* memtrack_proc_graphics_total
|
||||
*
|
||||
* Return total amount of memory that has been allocated for use as window
|
||||
* buffers. Does not differentiate between memory that has already been
|
||||
* accounted for by reading /proc/pid/smaps and memory that has not been
|
||||
* accounted for.
|
||||
*
|
||||
* Returns non-negative size in bytes on success, -errno on error.
|
||||
*/
|
||||
ssize_t memtrack_proc_graphics_total(struct memtrack_proc *p);
|
||||
|
||||
/**
|
||||
* memtrack_proc_graphics_pss
|
||||
*
|
||||
* Return total amount of memory that has been allocated for use as window
|
||||
* buffers, but has not already been accounted for by reading /proc/pid/smaps.
|
||||
* Memory that is shared across processes may already be divided by the
|
||||
* number of processes that share it (preferred), or may be charged in full to
|
||||
* every process that shares it, depending on the capabilities of the driver.
|
||||
*
|
||||
* Returns non-negative size in bytes on success, -errno on error.
|
||||
*/
|
||||
ssize_t memtrack_proc_graphics_pss(struct memtrack_proc *p);
|
||||
|
||||
/**
|
||||
* memtrack_proc_gl_total
|
||||
*
|
||||
* Same as memtrack_proc_graphics_total, but counts GL memory (which
|
||||
* should not overlap with graphics memory) instead of graphics memory.
|
||||
*
|
||||
* Returns non-negative size in bytes on success, -errno on error.
|
||||
*/
|
||||
ssize_t memtrack_proc_gl_total(struct memtrack_proc *p);
|
||||
|
||||
/**
|
||||
* memtrack_proc_gl_pss
|
||||
*
|
||||
* Same as memtrack_proc_graphics_total, but counts GL memory (which
|
||||
* should not overlap with graphics memory) instead of graphics memory.
|
||||
*
|
||||
* Returns non-negative size in bytes on success, -errno on error.
|
||||
*/
|
||||
ssize_t memtrack_proc_gl_pss(struct memtrack_proc *p);
|
||||
|
||||
/**
|
||||
* memtrack_proc_other_total
|
||||
*
|
||||
* Same as memtrack_proc_graphics_total, but counts miscellaneous memory
|
||||
* not tracked by gl or graphics calls above.
|
||||
*
|
||||
* Returns non-negative size in bytes on success, -errno on error.
|
||||
*/
|
||||
ssize_t memtrack_proc_other_total(struct memtrack_proc *p);
|
||||
|
||||
/**
|
||||
* memtrack_proc_other_pss
|
||||
*
|
||||
* Same as memtrack_proc_graphics_total, but counts miscellaneous memory
|
||||
* not tracked by gl or graphics calls above.
|
||||
*
|
||||
* Returns non-negative size in bytes on success, -errno on error.
|
||||
*/
|
||||
ssize_t memtrack_proc_other_pss(struct memtrack_proc *p);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
173
android/system/core/libmemtrack/memtrack.cpp
Normal file
173
android/system/core/libmemtrack/memtrack.cpp
Normal file
|
@ -0,0 +1,173 @@
|
|||
/*
|
||||
* 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>
|
||||
|
||||
#include <log/log.h>
|
||||
|
||||
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;
|
||||
using android::hardware::Return;
|
||||
|
||||
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();
|
||||
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;
|
||||
|
||||
Return<void> ret = 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 ret.isOk() ? err : -1;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
139
android/system/core/libmemtrack/memtrack_test.c
Normal file
139
android/system/core/libmemtrack/memtrack_test.c
Normal file
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <memtrack/memtrack.h>
|
||||
|
||||
#include <pagemap/pagemap.h>
|
||||
|
||||
#define DIV_ROUND_UP(x,y) (((x) + (y) - 1) / (y))
|
||||
|
||||
static int getprocname(pid_t pid, char *buf, int len) {
|
||||
char *filename;
|
||||
FILE *f;
|
||||
int rc = 0;
|
||||
static const char* unknown_cmdline = "<unknown>";
|
||||
|
||||
if (len <= 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
|
||||
rc = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
f = fopen(filename, "r");
|
||||
if (f == NULL) {
|
||||
rc = 2;
|
||||
goto releasefilename;
|
||||
}
|
||||
|
||||
if (fgets(buf, len, f) == NULL) {
|
||||
rc = 3;
|
||||
goto closefile;
|
||||
}
|
||||
|
||||
closefile:
|
||||
(void) fclose(f);
|
||||
releasefilename:
|
||||
free(filename);
|
||||
exit:
|
||||
if (rc != 0) {
|
||||
/*
|
||||
* The process went away before we could read its process name. Try
|
||||
* to give the user "<unknown>" here, but otherwise they get to look
|
||||
* at a blank.
|
||||
*/
|
||||
if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
|
||||
rc = 4;
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
pm_kernel_t *ker;
|
||||
size_t num_procs;
|
||||
pid_t *pids;
|
||||
struct memtrack_proc *p;
|
||||
size_t i;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
ret = pm_kernel_create(&ker);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Error creating kernel interface -- "
|
||||
"does this kernel have pagemap?\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ret = pm_kernel_pids(ker, &pids, &num_procs);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Error listing processes.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
p = memtrack_proc_new();
|
||||
if (ret) {
|
||||
fprintf(stderr, "failed to create memtrack process handle\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (i = 0; i < num_procs; i++) {
|
||||
pid_t pid = pids[i];
|
||||
char cmdline[256];
|
||||
size_t v1;
|
||||
size_t v2;
|
||||
size_t v3;
|
||||
size_t v4;
|
||||
size_t v5;
|
||||
size_t v6;
|
||||
|
||||
getprocname(pid, cmdline, (int)sizeof(cmdline));
|
||||
|
||||
ret = memtrack_proc_get(p, pid);
|
||||
if (ret) {
|
||||
fprintf(stderr, "failed to get memory info for pid %d: %s (%d)\n",
|
||||
pid, strerror(-ret), ret);
|
||||
continue;
|
||||
}
|
||||
|
||||
v1 = DIV_ROUND_UP(memtrack_proc_graphics_total(p), 1024);
|
||||
v2 = DIV_ROUND_UP(memtrack_proc_graphics_pss(p), 1024);
|
||||
v3 = DIV_ROUND_UP(memtrack_proc_gl_total(p), 1024);
|
||||
v4 = DIV_ROUND_UP(memtrack_proc_gl_pss(p), 1024);
|
||||
v5 = DIV_ROUND_UP(memtrack_proc_other_total(p), 1024);
|
||||
v6 = DIV_ROUND_UP(memtrack_proc_other_pss(p), 1024);
|
||||
|
||||
if (v1 | v2 | v3 | v4 | v5 | v6) {
|
||||
printf("%5d %6zu %6zu %6zu %6zu %6zu %6zu %s\n", pid,
|
||||
v1, v2, v3, v4, v5, v6, cmdline);
|
||||
}
|
||||
}
|
||||
|
||||
memtrack_proc_destroy(p);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue