2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <pagemap/pagemap.h>
22 #define SIMPLEQ_INSERT_SIMPLEQ_TAIL(head_a, head_b) \
24 if (!SIMPLEQ_EMPTY(head_b)) { \
25 if ((head_a)->sqh_first == NULL) \
26 (head_a)->sqh_first = (head_b)->sqh_first; \
27 *(head_a)->sqh_last = (head_b)->sqh_first; \
28 (head_a)->sqh_last = (head_b)->sqh_last; \
30 } while (/*CONSTCOND*/0)
32 /* We use an array of int to store the references to a given offset in the swap
33 1 GiB swap means 512KiB size array: offset are the index */
34 typedef unsigned short pm_pswap_refcount_t;
35 struct pm_proportional_swap {
36 unsigned int array_size;
37 pm_pswap_refcount_t *offset_array;
40 void pm_memusage_zero(pm_memusage_t *mu) {
41 mu->vss = mu->rss = mu->pss = mu->uss = mu->swap = 0;
43 SIMPLEQ_INIT(&mu->swap_offset_list);
46 void pm_memusage_pswap_init_handle(pm_memusage_t *mu, pm_proportional_swap_t *p_swap) {
50 void pm_memusage_add(pm_memusage_t *a, pm_memusage_t *b) {
56 SIMPLEQ_INSERT_SIMPLEQ_TAIL(&a->swap_offset_list, &b->swap_offset_list);
59 pm_proportional_swap_t * pm_memusage_pswap_create(int swap_size)
61 pm_proportional_swap_t *p_swap = NULL;
63 p_swap = malloc(sizeof(pm_proportional_swap_t));
65 fprintf(stderr, "Error allocating proportional swap.\n");
67 p_swap->array_size = swap_size / getpagesize();
68 p_swap->offset_array = calloc(p_swap->array_size, sizeof(pm_pswap_refcount_t));
69 if (p_swap->offset_array == NULL) {
70 fprintf(stderr, "Error allocating proportional swap offset array.\n");
79 void pm_memusage_pswap_destroy(pm_proportional_swap_t *p_swap) {
81 free(p_swap->offset_array);
86 void pm_memusage_pswap_add_offset(pm_memusage_t *mu, unsigned int offset) {
87 pm_swap_offset_t *soff;
89 if (mu->p_swap == NULL)
92 if (offset >= mu->p_swap->array_size) {
93 fprintf(stderr, "SWAP offset %d is out of swap bounds.\n", offset);
97 if (mu->p_swap->offset_array[offset] == USHRT_MAX) {
98 fprintf(stderr, "SWAP offset %d ref. count if overflowing ushort type.\n", offset);
100 mu->p_swap->offset_array[offset]++;
103 soff = malloc(sizeof(pm_swap_offset_t));
105 soff->offset = offset;
106 SIMPLEQ_INSERT_TAIL(&mu->swap_offset_list, soff, simpleqe);
110 void pm_memusage_pswap_get_usage(pm_memusage_t *mu, pm_swapusage_t *su) {
112 int pagesize = getpagesize();
113 pm_swap_offset_t *elem;
118 su->proportional = su->unique = 0;
119 SIMPLEQ_FOREACH(elem, &mu->swap_offset_list, simpleqe) {
120 su->proportional += pagesize / mu->p_swap->offset_array[elem->offset];
121 su->unique += mu->p_swap->offset_array[elem->offset] == 1 ? pagesize : 0;
125 void pm_memusage_pswap_free(pm_memusage_t *mu) {
126 pm_swap_offset_t *elem = SIMPLEQ_FIRST(&mu->swap_offset_list);
128 SIMPLEQ_REMOVE_HEAD(&mu->swap_offset_list, simpleqe);
130 elem = SIMPLEQ_FIRST(&mu->swap_offset_list);