|
| 1 | +/* |
| 2 | +* Copyright The OpenTelemetry Authors |
| 3 | +* SPDX-License-Identifier: Apache-2.0 |
| 4 | +*/ |
| 5 | +package oteldemo.problempattern; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +import io.grpc.ManagedChannelBuilder; |
| 12 | + |
| 13 | +import org.apache.logging.log4j.LogManager; |
| 14 | +import org.apache.logging.log4j.Logger; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class is designed to simulate a high CPU load scenario. |
| 18 | + * It contains methods to start and stop a specified number of worker threads designed to |
| 19 | + * perform CPU-intensive calculations. |
| 20 | + */ |
| 21 | +public class CPULoad { |
| 22 | + private static final Logger logger = LogManager.getLogger(CPULoad.class.getName()); |
| 23 | + private static final int THREAD_COUNT = 4; |
| 24 | + private boolean running = false; |
| 25 | + private final List<Logarithmizer> runningWorkers = new ArrayList<>(); |
| 26 | + |
| 27 | + private static CPULoad instance; |
| 28 | + |
| 29 | + /** |
| 30 | + * Singleton pattern to get the instance of CPULoad. |
| 31 | + * @return The singleton instance of CPULoad. |
| 32 | + */ |
| 33 | + public static CPULoad getInstance() { |
| 34 | + if (instance == null) { |
| 35 | + instance = new CPULoad(); |
| 36 | + } |
| 37 | + return instance; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Starts or stops the CPU load generation based on the input parameter. |
| 42 | + * If enabled, it launches worker threads. If disabled, it stops any running threads. |
| 43 | + * |
| 44 | + * @param enabled Flag to start (true) or stop (false) the CPU load simulation. |
| 45 | + */ |
| 46 | + public void execute(Boolean enabled) { |
| 47 | + if (enabled) { |
| 48 | + logger.info("High CPU-Load problempattern enabled"); |
| 49 | + if (!running) { |
| 50 | + spawnLoadWorkers(THREAD_COUNT); |
| 51 | + running = true; |
| 52 | + } |
| 53 | + } else { |
| 54 | + running = false; |
| 55 | + stopWorkers(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates and starts a specified number of Logarithmizer threads to simulate CPU load. |
| 61 | + * |
| 62 | + * @param threadCount The number of threads to be started. |
| 63 | + */ |
| 64 | + private void spawnLoadWorkers(int threadCount) { |
| 65 | + synchronized(runningWorkers) { |
| 66 | + for (int i = 0; i < threadCount; i++) { |
| 67 | + Logarithmizer logarithmizer = new Logarithmizer(); |
| 68 | + Thread thread = new Thread(logarithmizer); |
| 69 | + thread.setDaemon(true); |
| 70 | + thread.start(); |
| 71 | + runningWorkers.add(logarithmizer); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Signals all running Logarithmizer threads to stop and clears the list of running workers. |
| 78 | + */ |
| 79 | + private void stopWorkers() { |
| 80 | + synchronized(runningWorkers) { |
| 81 | + for (Logarithmizer logarithmizer : runningWorkers) { |
| 82 | + logarithmizer.setShouldRun(false); |
| 83 | + } |
| 84 | + runningWorkers.clear(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Inner class representing a worker focused on calculating logarithms to consume CPU resources. |
| 90 | + */ |
| 91 | + private static class Logarithmizer implements Runnable { |
| 92 | + |
| 93 | + private volatile boolean shouldRun = true; |
| 94 | + |
| 95 | + /** |
| 96 | + * Continuously calculates the logarithm of the current system time until |
| 97 | + * requested to stop. |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public void run() { |
| 101 | + while (shouldRun) { |
| 102 | + Math.log(System.currentTimeMillis()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets the shouldRun flag to control whether this Logarithmizer should continue |
| 108 | + * to run. |
| 109 | + * |
| 110 | + * @param shouldRun A boolean flag to continue (true) or stop (false) the logarithm computation. |
| 111 | + */ |
| 112 | + public void setShouldRun(boolean shouldRun) { |
| 113 | + this.shouldRun = shouldRun; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments