• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include "defs.h"
13 #if DEBUG_MEM_USAGE
14 #include "chcore/container/list.h"
15 #include "chcore/type.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 bool collecting_switch = false;
20 
21 struct memory_use_msg free_mem_msg[RECORD_SIZE];
22 
23 char *types[RECORD_SIZE] = {"dentry", "string", "inode", "symlink", "data page"};
24 
25 struct list_head order_list_node_head;
26 
tmpfs_get_mem_usage()27 void tmpfs_get_mem_usage()
28 {
29     struct addr_to_size *temp;
30     unsigned long unfreed_memory = 0;
31 
32     if (!collecting_switch) {
33         init_list_head(&order_list_node_head);
34         for (int i = 0; i < RECORD_SIZE; i++) {
35             free_mem_msg[i].typename = types[i];
36             init_list_head(&free_mem_msg[i].addr_to_size_head);
37         }
38         collecting_switch = true;
39     } else {
40         for (int i = 0; i < RECORD_SIZE; i++) {
41             printf("%s\n", free_mem_msg[i].typename);
42             for_each_in_list (temp,
43                               struct addr_to_size,
44                               node,
45                               &(free_mem_msg[i].addr_to_size_head)) {
46                 printf("--------------------------------------------\n");
47                 printf("addr:0x%lx size:0x%lx\n", temp->addr, temp->size);
48                 unfreed_memory += temp->size;
49             }
50             printf("============================================\n");
51         }
52         printf("unfreed memory used by tmpfs: 0x%lx\n", unfreed_memory);
53     }
54 }
55 
tmpfs_record_mem_usage(void * addr,size_t size,int type)56 void tmpfs_record_mem_usage(void *addr, size_t size, int type)
57 {
58     if (!collecting_switch) {
59         return;
60     }
61     struct memory_use_msg *msg;
62     struct addr_to_size *new_record;
63 
64     msg = &(free_mem_msg[type]);
65 
66     new_record = malloc(sizeof(struct addr_to_size));
67     memset(new_record, 0, sizeof(struct addr_to_size));
68     new_record->addr = (vaddr_t)addr;
69     new_record->size = size;
70 
71     list_add(&(new_record->order_list_node), &order_list_node_head);
72     list_add(&(new_record->node), &msg->addr_to_size_head);
73 }
74 
tmpfs_revoke_mem_usage(void * addr,int type)75 void tmpfs_revoke_mem_usage(void *addr, int type)
76 {
77     if (!collecting_switch) {
78         return;
79     }
80     struct addr_to_size *record = NULL;
81     bool find = false;
82 
83     for_each_in_list (
84         record, struct addr_to_size, order_list_node, &order_list_node_head) {
85         if (record->addr == (vaddr_t)addr) {
86             find = true;
87             break;
88         }
89     }
90 
91     if (find) {
92         list_del(&(record->node));
93         list_del(&(record->order_list_node));
94         free(record);
95     }
96 }
97 #endif