|
| 1 | +/* |
| 2 | + * * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * * |
| 5 | + * * This code is free software; you can redistribute it and/or modify it |
| 6 | + * * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * * published by the Free Software Foundation. |
| 8 | + * * |
| 9 | + * * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * * accompanied this code). |
| 14 | + * * |
| 15 | + * * You should have received a copy of the GNU General Public License version |
| 16 | + * * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * * |
| 19 | + * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * * or visit www.oracle.com if you need additional information or have any |
| 21 | + * * questions. |
| 22 | + * */ |
| 23 | +#ifndef PREDICT_H |
| 24 | +#define PREDICT_H |
| 25 | + |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <stdarg.h> |
| 30 | +#include <string.h> |
| 31 | +#include <syslog.h> |
| 32 | + |
| 33 | +#ifdef __cplusplus |
| 34 | +extern "C" { |
| 35 | +#endif |
| 36 | + |
| 37 | +/* Size of sliding window for computing trend line */ |
| 38 | +#define LSQ_LOOKBACK 8 |
| 39 | + |
| 40 | +/* How often should data be sampled and trend analyzed*/ |
| 41 | +#define LOW_PERIODICITY 60 |
| 42 | +#define NORM_PERIODICITY 30 |
| 43 | +#define HIGH_PERIODICITY 15 |
| 44 | + |
| 45 | +#define MAX_ORDER 11 |
| 46 | +#define MEMPREDICT_RECLAIM 0x01 |
| 47 | +#define MEMPREDICT_COMPACT 0x02 |
| 48 | +#define MEMPREDICT_LOWER_WMARKS 0x04 |
| 49 | + |
| 50 | +struct lsq_struct { |
| 51 | + int next; |
| 52 | + int ready; |
| 53 | + long long y[LSQ_LOOKBACK]; |
| 54 | + long long x[LSQ_LOOKBACK]; |
| 55 | +}; |
| 56 | + |
| 57 | +enum output_type { |
| 58 | + OUTPUT_OBSERVATIONS, |
| 59 | + OUTPUT_PREDICTIONS, |
| 60 | + MAX_OUTPUT |
| 61 | +}; |
| 62 | + |
| 63 | +struct frag_info { |
| 64 | + long long free_pages; |
| 65 | + long long msecs; |
| 66 | +}; |
| 67 | + |
| 68 | +#define BATCHSIZE 8192 |
| 69 | + |
| 70 | +#define EOF_RET -1 |
| 71 | + |
| 72 | +#define MAX_VERBOSE 5 |
| 73 | +#define MAX_NEGDENTRY 100 |
| 74 | +#define MAX_NEGDENTRY_DEFAULT 15 /* default is 1.5% */ |
| 75 | + |
| 76 | +#define MAX_NUMANODES 1024 |
| 77 | + |
| 78 | +/* |
| 79 | + * Number of consecutive samples showing growth in unaccounted memory |
| 80 | + * that will trigger memory leak warning |
| 81 | + */ |
| 82 | +#define UNACCT_MEM_GRTH_MAX 10 |
| 83 | + |
| 84 | +/* Minimum % change in meminfo numbers to trigger warnings */ |
| 85 | +#define MEM_TRIGGER_DELTA 10 |
| 86 | + |
| 87 | +#define FLDLEN 20 |
| 88 | + |
| 89 | +/* |
| 90 | + * System files to control reclamation and compaction |
| 91 | + */ |
| 92 | +#define COMPACT_PATH_FORMAT "/sys/devices/system/node/node%d/compact" |
| 93 | +#define RESCALE_WMARK "/proc/sys/vm/watermark_scale_factor" |
| 94 | + |
| 95 | +/* |
| 96 | + * System files to control negative dentries |
| 97 | + */ |
| 98 | +#define NEG_DENTRY_LIMIT "/proc/sys/fs/negative-dentry-limit" |
| 99 | + |
| 100 | +enum memdata_items { |
| 101 | + MEMAVAIL, |
| 102 | + BUFFERS, |
| 103 | + CACHED, |
| 104 | + SWPCACHED, |
| 105 | + UNEVICTABLE, |
| 106 | + MLOCKED, |
| 107 | + ANONPAGES, |
| 108 | + MAPPED, |
| 109 | + SHMEM, |
| 110 | + KRECLAIMABLE, |
| 111 | + SLAB, |
| 112 | + SUNRECLAIM, |
| 113 | + KSTACK, |
| 114 | + PGTABLE, |
| 115 | + SECPGTABLE, |
| 116 | + VMALLOCUSED, |
| 117 | + CMA, |
| 118 | + NR_MEMDATA_ITEMS |
| 119 | +}; |
| 120 | + |
| 121 | +enum trigger_type { |
| 122 | + NO_TRIGGER, |
| 123 | + MEMORY_PRESSURE_TRIGGER, |
| 124 | + SUDDEN_MEMORY_LEAK_TRIGGER, |
| 125 | + BACKGROUND_MEMORY_LEAK_TRIGGER, |
| 126 | + SLOW_MEMORY_LEAK_TRIGGER |
| 127 | +}; |
| 128 | + |
| 129 | +struct curr_mem_info { |
| 130 | + unsigned long pr_memdata[NR_MEMDATA_ITEMS]; |
| 131 | + unsigned long freemem; |
| 132 | + unsigned long prv_free; |
| 133 | + unsigned long mem_remain; |
| 134 | + unsigned long unacct_mem; |
| 135 | + long unmapped_pages; |
| 136 | +}; |
| 137 | + |
| 138 | +struct adaptivemmd_opts { |
| 139 | + int unacct_mem_grth_max; |
| 140 | + int mem_trigger_delta; |
| 141 | + int aggressiveness; |
| 142 | + unsigned int maxwsf; /* Highest value to set watermark_scale_factor to. */ |
| 143 | + unsigned int mywsf; |
| 144 | + int max_compaction_order; /* Highest order pages to look at for fragmentation. */ |
| 145 | + int compaction_requested[MAX_NUMANODES]; |
| 146 | + unsigned long last_bigpages[MAX_NUMANODES]; |
| 147 | + unsigned long last_reclaimed; |
| 148 | + unsigned long total_free_pages; |
| 149 | + unsigned long total_cache_pages; |
| 150 | + unsigned long total_hugepages; |
| 151 | + unsigned long base_psize; |
| 152 | + long compaction_rate; |
| 153 | + long reclaim_rate; |
| 154 | + struct lsq_struct page_lsq[MAX_NUMANODES][MAX_ORDER]; |
| 155 | + FILE *ifile; |
| 156 | + struct timespec spec_before; |
| 157 | + int periodicity; |
| 158 | + int dry_run; |
| 159 | + |
| 160 | + bool memleak_check_enabled; |
| 161 | + bool memory_pressure_check_enabled; /* aka ENABLE_FREE_PAGE_MGMT */ |
| 162 | + int maxgap; /* Maximum gap between low and high watermarks (in GB) */ |
| 163 | + bool neg_dentry_check_enabled; /* aka ENABLE_NEG_DENTRY_MGMT */ |
| 164 | + int neg_dentry_pct; /* aka NEG_DENTRY_CAP negative dentry memory usage cap is expressed as |
| 165 | + * a percentage of total memory on the system. */ |
| 166 | + unsigned long base_mem; |
| 167 | + unsigned long mem_remain; |
| 168 | + unsigned long gr_count; |
| 169 | + unsigned long prv_free; |
| 170 | + unsigned long memdata[NR_MEMDATA_ITEMS]; |
| 171 | + unsigned long pr_memdata[NR_MEMDATA_ITEMS]; |
| 172 | + unsigned long min_wmark[MAX_NUMANODES]; |
| 173 | + unsigned long low_wmark[MAX_NUMANODES]; |
| 174 | + unsigned long high_wmark[MAX_NUMANODES]; |
| 175 | + unsigned long managed_pages[MAX_NUMANODES]; |
| 176 | + int skip_dmazone; |
| 177 | + int debug_mode; |
| 178 | + int verbose; |
| 179 | + |
| 180 | + long unmapped_pages; |
| 181 | + unsigned long unacct_mem; |
| 182 | + |
| 183 | + struct curr_mem_info curr_mem_info; |
| 184 | + |
| 185 | + unsigned long final_result; |
| 186 | + |
| 187 | + enum trigger_type mem_pressure_trigger; |
| 188 | + enum trigger_type mem_leak_trigger; |
| 189 | + |
| 190 | + char *capture_data_dir; /* directory to capture data to - must already exist */ |
| 191 | + int max_triggers; /* used during capture data to limit amount of captured data */ |
| 192 | + int trigger_count; |
| 193 | + char *log_file; /* capture messages for testing */ |
| 194 | + int iteration; |
| 195 | +}; |
| 196 | + |
| 197 | +struct adaptivemmd_effects_opts { |
| 198 | + const struct adaptived_cause *cse; |
| 199 | +}; |
| 200 | + |
| 201 | +int run_adaptivemm_init(struct adaptivemmd_opts * const opts, int interval); |
| 202 | + |
| 203 | +int run_adaptivemm(struct adaptivemmd_opts * const opts); |
| 204 | + |
| 205 | +int run_adaptivemm_effects(struct adaptivemmd_opts * const opts); |
| 206 | + |
| 207 | + |
| 208 | +#define log_err(...) log_msg(LOG_ERR, __VA_ARGS__) |
| 209 | +#define log_warn(...) log_msg(LOG_WARNING, __VA_ARGS__) |
| 210 | +#define log_dbg(...) if (debug_mode) \ |
| 211 | + log_msg(LOG_DEBUG, __VA_ARGS__) |
| 212 | +#define log_info(verb_level, ...) if (verbose >= verb_level) \ |
| 213 | + log_msg(LOG_INFO, __VA_ARGS__) |
| 214 | + |
| 215 | +/* Use pr_info to log info irrespective of verbosity level */ |
| 216 | +#define pr_info(...) log_msg(LOG_INFO, __VA_ARGS__) |
| 217 | + |
| 218 | +extern void log_msg(int level, char *fmt, ...); |
| 219 | + |
| 220 | +#ifdef __cplusplus |
| 221 | +} |
| 222 | +#endif |
| 223 | + |
| 224 | +#endif /* PREDICT_H */ |
0 commit comments