Merge "Initial interface for MemoryTrackerHAL" into klp-dev
diff --git a/include/hardware/memtrack.h b/include/hardware/memtrack.h
new file mode 100644
index 0000000..57ba4ad
--- /dev/null
+++ b/include/hardware/memtrack.h
@@ -0,0 +1,160 @@
+/*
+ * 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 ANDROID_INCLUDE_HARDWARE_MEMTRACK_H
+#define ANDROID_INCLUDE_HARDWARE_MEMTRACK_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+#define MEMTRACK_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
+
+/**
+ * The id of this module
+ */
+#define MEMTRACK_HARDWARE_MODULE_ID "memtrack"
+
+/*
+ * The Memory Tracker HAL is designed to return information about device-specific
+ * memory usage. The primary goal is to be able to track memory that is not
+ * trackable in any other way, for example texture memory that is allocated by
+ * a process, but not mapped in to that process' address space.
+ * A secondary goal is to be able to categorize memory used by a process into
+ * GL, graphics, etc. All memory sizes should be in real memory usage,
+ * accounting for stride, bit depth, rounding up to page size, etc.
+ *
+ * A process collecting memory statistics will call getMemory for each
+ * combination of pid and memory type. For each memory type that it recognizes
+ * the HAL should fill out an array of memtrack_record structures breaking
+ * down the statistics of that memory type as much as possible. For example,
+ * getMemory(<pid>, MEMTRACK_TYPE_GL) might return:
+ * { { 4096, ACCOUNTED | PRIVATE | SYSTEM },
+ * { 40960, UNACCOUNTED | PRIVATE | SYSTEM },
+ * { 8192, ACCOUNTED | PRIVATE | DEDICATED },
+ * { 8192, UNACCOUNTED | PRIVATE | DEDICATED } }
+ * If the HAL could not differentiate between SYSTEM and DEDICATED memory, it
+ * could return:
+ * { { 12288, ACCOUNTED | PRIVATE },
+ * { 49152, UNACCOUNTED | PRIVATE } }
+ *
+ * Memory should not overlap between types. For example, a graphics buffer
+ * that has been mapped into the GPU as a surface should show up when
+ * MEMTRACK_TYPE_GRAPHICS is requested, and not when MEMTRACK_TYPE_GL
+ * is requested.
+ */
+
+enum memtrack_type {
+ MEMTRACK_TYPE_OTHER = 0,
+ MEMTRACK_TYPE_GL = 1,
+ MEMTRACK_TYPE_GRAPHICS = 2,
+ MEMTRACK_TYPE_MULTIMEDIA = 3,
+ MEMTRACK_TYPE_CAMERA = 4,
+ MEMTRACK_NUM_TYPES,
+};
+
+struct memtrack_record {
+ size_t size_in_bytes;
+ unsigned int flags;
+};
+
+/**
+ * Flags to differentiate memory that can already be accounted for in
+ * /proc/<pid>/smaps,
+ * (Shared_Clean + Shared_Dirty + Private_Clean + Private_Dirty = Size).
+ * In general, memory mapped in to a userspace process is accounted unless
+ * it was mapped with remap_pfn_range.
+ * Exactly one of these should be set.
+ */
+#define MEMTRACK_FLAG_SMAPS_ACCOUNTED (1 << 1)
+#define MEMTRACK_FLAG_SMAPS_UNACCOUNTED (1 << 2)
+
+/**
+ * Flags to differentiate memory shared across multiple processes vs. memory
+ * used by a single process. Only zero or one of these may be set in a record.
+ * If none are set, record is assumed to count shared + private memory.
+ */
+#define MEMTRACK_FLAG_SHARED (1 << 3)
+#define MEMTRACK_FLAG_SHARED_PSS (1 << 4) /* shared / num_procesess */
+#define MEMTRACK_FLAG_PRIVATE (1 << 5)
+
+/**
+ * Flags to differentiate memory taken from the kernel's allocation pool vs.
+ * memory that is dedicated to non-kernel allocations, for example a carveout
+ * or separate video memory. Only zero or one of these may be set in a record.
+ * If none are set, record is assumed to count system + dedicated memory.
+ */
+#define MEMTRACK_FLAG_SYSTEM (1 << 6)
+#define MEMTRACK_FLAG_DEDICATED (1 << 7)
+
+/**
+ * Flags to differentiate memory accessible by the CPU in non-secure mode vs.
+ * memory that is protected. Only zero or one of these may be set in a record.
+ * If none are set, record is assumed to count secure + nonsecure memory.
+ */
+#define MEMTRACK_FLAG_NONSECURE (1 << 8)
+#define MEMTRACK_FLAG_SECURE (1 << 9)
+
+/**
+ * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
+ * and the fields of this data structure must begin with hw_module_t
+ * followed by module specific information.
+ */
+typedef struct memtrack_module {
+ struct hw_module_t common;
+
+ /**
+ * (*init)() performs memtrack management setup actions and is called
+ * once before any calls to getMemory().
+ * Returns 0 on success, -errno on error.
+ */
+ int (*init)(const struct memtrack_module *module);
+
+ /**
+ * (*getMemory)() expects an array of record objects and populates up to
+ * *num_record structures with the sizes of memory plus associated flags for
+ * that memory. It also updates *num_records with the total number of
+ * records it could return if *num_records was large enough when passed in.
+ * Returning records with size 0 is expected, the number of records should
+ * not vary between calls to getMemory for the same memory type, even
+ * for different pids.
+ *
+ * The caller will often call getMemory for a type and pid with
+ * *num_records == 0 to determine how many records to allocate room for,
+ * this case should be a fast-path in the HAL, returning a constant and
+ * not querying any kernel files. If *num_records passed in is 0,
+ * then records may be NULL.
+ *
+ * This function must be thread-safe, it may get called from multiple
+ * threads at the same time.
+ *
+ * Returns 0 on success, -ENODEV if the type is not supported, -errno
+ * on other errors.
+ */
+ int (*getMemory)(const struct memtrack_module *module,
+ pid_t pid,
+ int type,
+ struct memtrack_record *records,
+ size_t *num_records);
+} memtrack_module_t;
+
+__END_DECLS
+
+#endif // ANDROID_INCLUDE_HARDWARE_MEMTRACK_H