1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_RUNTIME_STATS_H_
18 #define ART_RUNTIME_RUNTIME_STATS_H_
19 
20 #include <stdint.h>
21 
22 namespace art {
23 
24 // These must match the values in dalvik.system.VMDebug.
25 enum StatKinds {
26   KIND_ALLOCATED_OBJECTS      = 1<<0,
27   KIND_ALLOCATED_BYTES        = 1<<1,
28   KIND_FREED_OBJECTS          = 1<<2,
29   KIND_FREED_BYTES            = 1<<3,
30   KIND_GC_INVOCATIONS         = 1<<4,
31   KIND_CLASS_INIT_COUNT       = 1<<5,
32   KIND_CLASS_INIT_TIME        = 1<<6,
33 
34   // These values exist for backward compatibility.
35   KIND_EXT_ALLOCATED_OBJECTS = 1<<12,
36   KIND_EXT_ALLOCATED_BYTES   = 1<<13,
37   KIND_EXT_FREED_OBJECTS     = 1<<14,
38   KIND_EXT_FREED_BYTES       = 1<<15,
39 
40   KIND_GLOBAL_ALLOCATED_OBJECTS   = KIND_ALLOCATED_OBJECTS,
41   KIND_GLOBAL_ALLOCATED_BYTES     = KIND_ALLOCATED_BYTES,
42   KIND_GLOBAL_FREED_OBJECTS       = KIND_FREED_OBJECTS,
43   KIND_GLOBAL_FREED_BYTES         = KIND_FREED_BYTES,
44   KIND_GLOBAL_GC_INVOCATIONS      = KIND_GC_INVOCATIONS,
45   KIND_GLOBAL_CLASS_INIT_COUNT    = KIND_CLASS_INIT_COUNT,
46   KIND_GLOBAL_CLASS_INIT_TIME     = KIND_CLASS_INIT_TIME,
47 
48   KIND_THREAD_ALLOCATED_OBJECTS   = KIND_ALLOCATED_OBJECTS << 16,
49   KIND_THREAD_ALLOCATED_BYTES     = KIND_ALLOCATED_BYTES << 16,
50   KIND_THREAD_FREED_OBJECTS       = KIND_FREED_OBJECTS << 16,
51   KIND_THREAD_FREED_BYTES         = KIND_FREED_BYTES << 16,
52 
53   KIND_THREAD_GC_INVOCATIONS      = KIND_GC_INVOCATIONS << 16,
54 
55   // TODO: failedAllocCount, failedAllocSize
56 };
57 
58 /*
59  * Memory allocation profiler state.  This is used both globally and
60  * per-thread.
61  */
62 struct PACKED(4) RuntimeStats {
RuntimeStatsRuntimeStats63   RuntimeStats() {
64     Clear(~0);
65   }
66 
ClearRuntimeStats67   void Clear(int flags) {
68     if ((flags & KIND_ALLOCATED_OBJECTS) != 0) {
69       allocated_objects = 0;
70     }
71     if ((flags & KIND_ALLOCATED_BYTES) != 0) {
72       allocated_bytes = 0;
73     }
74     if ((flags & KIND_FREED_OBJECTS) != 0) {
75       freed_objects = 0;
76     }
77     if ((flags & KIND_FREED_BYTES) != 0) {
78       freed_bytes = 0;
79     }
80     if ((flags & KIND_GC_INVOCATIONS) != 0) {
81       gc_for_alloc_count = 0;
82     }
83     if ((flags & KIND_CLASS_INIT_COUNT) != 0) {
84       class_init_count = 0;
85     }
86     if ((flags & KIND_CLASS_INIT_TIME) != 0) {
87       class_init_time_ns = 0;
88     }
89   }
90 
91   // Number of objects allocated.
92   uint64_t allocated_objects;
93   // Cumulative size of all objects allocated.
94   uint64_t allocated_bytes;
95 
96   // Number of objects freed.
97   uint64_t freed_objects;
98   // Cumulative size of all freed objects.
99   uint64_t freed_bytes;
100 
101   // Number of times an allocation triggered a GC.
102   uint64_t gc_for_alloc_count;
103 
104   // Number of initialized classes.
105   uint64_t class_init_count;
106   // Cumulative time spent in class initialization.
107   uint64_t class_init_time_ns;
108 
109   DISALLOW_COPY_AND_ASSIGN(RuntimeStats);
110 };
111 
112 }  // namespace art
113 
114 #endif  // ART_RUNTIME_RUNTIME_STATS_H_
115