Mercurial > hg > thermostat-ng > agent
view plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackend.java @ 2772:9e3d6eedbbee
Add jvm-cpu plugin
Reviewed-by: jmatsuok, stooke
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024663.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/025090.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025307.html
author | Christopher Koehler <chkoehle@redhat.com> |
---|---|
date | Tue, 10 Oct 2017 12:29:18 -0400 |
parents | 3f1f63227847 |
children | c30824312ec5 |
line wrap: on
line source
/* * Copyright 2012-2017 Red Hat, Inc. * * This file is part of Thermostat. * * Thermostat is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2, or (at your * option) any later version. * * Thermostat is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Thermostat; see the file COPYING. If not see * <http://www.gnu.org/licenses/>. * * Linking this code with other modules is making a combined work * based on this code. Thus, the terms and conditions of the GNU * General Public License cover the whole combination. * * As a special exception, the copyright holders of this code give * you permission to link this code with independent modules to * produce an executable, regardless of the license terms of these * independent modules, and to copy and distribute the resulting * executable under terms of your choice, provided that you also * meet, for each linked independent module, the terms and conditions * of the license of that module. An independent module is a module * which is not derived from or based on this code. If you modify * this code, you may extend this exception to your version of the * library, but you are not obligated to do so. If you do not wish * to do so, delete this exception statement from your version. */ package com.redhat.thermostat.vm.cpu.agent.internal; import java.io.BufferedReader; import java.io.IOException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.logging.Level; import java.util.logging.Logger; import com.redhat.thermostat.backend.Backend; import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.SystemClock; import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.portability.PortableHostFactory; import com.redhat.thermostat.common.portability.SysConf; import com.redhat.thermostat.common.portability.linux.ProcDataSource; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.jvm.overview.agent.VmPollingAction; import com.redhat.thermostat.jvm.overview.agent.VmPollingBackend; import com.redhat.thermostat.shared.config.OS; import com.redhat.thermostat.storage.core.WriterID; import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.osgi.framework.BundleContext; @Component @Service(value = Backend.class) public class VmCpuBackend extends VmPollingBackend { private static final Logger LOGGER = LoggingUtils.getLogger(VmCpuBackend.class); @Reference private VmCpuStatDAO vmCpuStatDAO; @Reference private WriterID writerId; private VmCpuBackendAction action; public VmCpuBackend() { this(Executors.newSingleThreadScheduledExecutor()); } VmCpuBackend(ScheduledExecutorService executor) { super("VM CPU Backend", "Gathers CPU statistics for a JVM", "Red Hat, Inc.", "1.0", executor); } @Activate protected void componentActivated(BundleContext context) { this.action = new VmCpuBackendAction(writerId, vmCpuStatDAO); initialize(context); setVersion(new Version(context.getBundle()).getVersionNumber()); registerAction(action); } @Deactivate protected void componentDeactivated() { if (isActive()) { deactivate(); } } // DS bind method. protected void bindVmCpuStats(VmCpuStatDAO dao) { this.vmCpuStatDAO = dao; } // DS bind method. protected void bindWriterId(WriterID id) { this.writerId = id; } private static class VmCpuBackendAction implements VmPollingAction { private VmCpuStatBuilder builder; private VmCpuStatDAO dao; private VmCpuBackendAction(final WriterID id, VmCpuStatDAO dao) { Clock clock = new SystemClock(); long ticksPerSecond = SysConf.getClockTicksPerSecond(); ProcDataSource source = new ProcDataSource(); int numCpus = getCpuCount(source); ProcessStatusInfoBuilder PSIBuilder = new ProcessStatusInfoBuilderImpl(); builder = new VmCpuStatBuilder(clock, numCpus, ticksPerSecond, PSIBuilder, id); this.dao = dao; } @Override public void run(String vmId, int pid) { if (builder.knowsAbout(pid)) { VmCpuStat dataBuilt = builder.build(vmId, pid); if (dataBuilt != null) { dao.put(dataBuilt); } } else { builder.learnAbout(pid); } } private int getCpuCount(ProcDataSource dataSource) { return OS.IS_LINUX ? getLinuxCpuCount(dataSource) : getCpuCount(); } private int getCpuCount() { return PortableHostFactory.getInstance().getCPUCount(); } private int getLinuxCpuCount(ProcDataSource dataSource) { final String KEY_PROCESSOR_ID = "processor"; int cpuCount = 0; try (BufferedReader bufferedReader = new BufferedReader(dataSource.getCpuInfoReader())) { String line; while ((line = bufferedReader.readLine()) != null) { if (line.startsWith(KEY_PROCESSOR_ID)) { cpuCount++; } } } catch (IOException ioe) { LOGGER.log(Level.WARNING, "Unable to read cpu info"); } return cpuCount; } } @Override public void vmStatusChanged(Status newStatus, String vmId, int pid) { super.vmStatusChanged(newStatus, vmId, pid); if (Status.VM_STOPPED.equals(newStatus)) { action.builder.forgetAbout(pid); } } // For testing purposes only. void setVmCpuStatBuilder(VmCpuStatBuilder vmCpuStatBuilder) { action.builder = vmCpuStatBuilder; } }