|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/resmonitor/fillcpu.c |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. The |
| 7 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance with the |
| 9 | + * License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | + * License for the specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | +/**************************************************************************** |
| 22 | + * Included Files |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <fcntl.h> |
| 26 | +#include <signal.h> |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <string.h> |
| 30 | +#include <sys/stat.h> |
| 31 | +#include <sys/time.h> |
| 32 | +#include <sys/types.h> |
| 33 | +#include <syslog.h> |
| 34 | +#include <unistd.h> |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Pre-processor Definitions |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +#define PATH "/proc" |
| 41 | +#define CPULOAD "cpuload" |
| 42 | +#define LOADAVG "loadavg" |
| 43 | + |
| 44 | +/**************************************************************************** |
| 45 | + * Private Data |
| 46 | + ****************************************************************************/ |
| 47 | + |
| 48 | +static int go = 1; |
| 49 | + |
| 50 | +/**************************************************************************** |
| 51 | + * Private Functions |
| 52 | + ****************************************************************************/ |
| 53 | + |
| 54 | +static void handler(int sig) |
| 55 | +{ |
| 56 | + go = 0; |
| 57 | +} |
| 58 | + |
| 59 | +static void show_usages(void) |
| 60 | +{ |
| 61 | + syslog(LOG_WARNING, |
| 62 | + "Usage: CMD [-c <cpu>] [-f <filepath>] [-a]\n" |
| 63 | + "\t\t-c: set cpu occupation that you except, default 80\n" |
| 64 | + "\t\t-f: set write payload path, eg. /tmp/payload, program will " |
| 65 | + "write to memory if -f not set\n" |
| 66 | + "\t\t-a: the cpu (set by -c) is the cpu occupied by this program\n"); |
| 67 | + exit(1); |
| 68 | +} |
| 69 | + |
| 70 | +static float get_cpu(int pid) |
| 71 | +{ |
| 72 | + float cpu = 0; |
| 73 | + char filepath[20]; |
| 74 | + int ret; |
| 75 | + if (pid <= 0) |
| 76 | + { |
| 77 | + ret = snprintf(filepath, 20, "%s/%s", PATH, CPULOAD); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + ret = snprintf(filepath, 20, "%s/%d/%s", PATH, pid, LOADAVG); |
| 82 | + } |
| 83 | + |
| 84 | + if (ret < 0) |
| 85 | + { |
| 86 | + /* syslog(LOG_ERR, "snprintf error\n"); */ |
| 87 | + |
| 88 | + return cpu; |
| 89 | + } |
| 90 | + |
| 91 | + FILE *fp = fopen(filepath, "r"); |
| 92 | + if (!fp) |
| 93 | + { |
| 94 | + return cpu; |
| 95 | + } |
| 96 | + |
| 97 | + char buf[8]; |
| 98 | + fgets(buf, 8, fp); |
| 99 | + sscanf(buf, "%f", &cpu); |
| 100 | + fclose(fp); |
| 101 | + return cpu; |
| 102 | +} |
| 103 | + |
| 104 | +static int writefile(char *filepath, char *buffer1, char *buffer2) |
| 105 | +{ |
| 106 | + if (strlen(filepath) == 0) |
| 107 | + { |
| 108 | + memset(buffer2, '*', 1024); |
| 109 | + memcpy(buffer1, buffer2, 1024); |
| 110 | + return 0; |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + int fd; |
| 115 | + memset(buffer1, '*', 1024); |
| 116 | + if ((fd = open(filepath, O_WRONLY | O_CREAT, 0700)) <= 0) |
| 117 | + { |
| 118 | + syslog(LOG_ERR, "open file error\n"); |
| 119 | + return -1; |
| 120 | + } |
| 121 | + |
| 122 | + if (write(fd, buffer1, 1024) <= 0) |
| 123 | + { |
| 124 | + syslog(LOG_ERR, "write file error\n"); |
| 125 | + close(fd); |
| 126 | + return -1; |
| 127 | + } |
| 128 | + |
| 129 | + return close(fd); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +int main(int argc, char *argv[]) |
| 134 | +{ |
| 135 | + char buf1[1024]; |
| 136 | + char buf2[1024]; |
| 137 | + char filepath[40]; |
| 138 | + struct timeval sleeptime; |
| 139 | + int n = 0; |
| 140 | + int lowcount = 0; |
| 141 | + int cpu = 80; |
| 142 | + bool ispid = false; |
| 143 | + int time = 10000; |
| 144 | + float fcpu; |
| 145 | + int o; |
| 146 | + |
| 147 | + memset(filepath, 0, 40); |
| 148 | + go = 1; |
| 149 | + if (argc == 1) |
| 150 | + { |
| 151 | + show_usages(); |
| 152 | + } |
| 153 | + |
| 154 | + while ((o = getopt(argc, argv, "c:f:a")) != EOF) |
| 155 | + { |
| 156 | + switch (o) |
| 157 | + { |
| 158 | + case 'c': |
| 159 | + cpu = atoi(optarg); |
| 160 | + break; |
| 161 | + case 'f': |
| 162 | + snprintf(filepath, 40, "%s", optarg); |
| 163 | + break; |
| 164 | + case 'a': |
| 165 | + ispid = true; |
| 166 | + break; |
| 167 | + default: |
| 168 | + show_usages(); |
| 169 | + break; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + signal(SIGINT, handler); |
| 174 | + signal(SIGKILL, handler); |
| 175 | + |
| 176 | + while (go) |
| 177 | + { |
| 178 | + if (time < 1000) |
| 179 | + { |
| 180 | + time = 1000; |
| 181 | + lowcount++; |
| 182 | + if (lowcount > 4) |
| 183 | + { |
| 184 | + lowcount = 0; |
| 185 | + n += 2; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + else if (time > 10000) |
| 190 | + { |
| 191 | + time = 10000; |
| 192 | + n -= 1; |
| 193 | + n = (n < 0 ? 0 : n); |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + lowcount = 0; |
| 198 | + } |
| 199 | + |
| 200 | + sleeptime.tv_sec = 0; |
| 201 | + sleeptime.tv_usec = time; |
| 202 | + select(0, NULL, NULL, NULL, &sleeptime); |
| 203 | + if (ispid) |
| 204 | + { |
| 205 | + fcpu = get_cpu(getpid()); |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + fcpu = get_cpu(0); |
| 210 | + } |
| 211 | + |
| 212 | + if (fcpu > cpu) |
| 213 | + { |
| 214 | + time += 1000; |
| 215 | + } |
| 216 | + else |
| 217 | + { |
| 218 | + time -= 1000; |
| 219 | + } |
| 220 | + |
| 221 | + for (int i = 0; i < n; i++) |
| 222 | + { |
| 223 | + if (writefile(filepath, buf1, buf2) != 0) |
| 224 | + { |
| 225 | + break; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + syslog(LOG_INFO, "program complete!\n"); |
| 231 | + return 0; |
| 232 | +} |
0 commit comments