Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIOT-GDA-02-007: Connect SystemCpuUtilTask and SystemMemUtilTask to SystemPerformanceManager #44

Open
labbenchstudios opened this issue Jul 15, 2020 · 1 comment
Labels
exercise New feature to implement as an exercise
Milestone

Comments

@labbenchstudios
Copy link
Contributor

labbenchstudios commented Jul 15, 2020

Description

  • Connect SystemCpuUtilTask and SystemMemUtilTask into SystemPerformanceManager by invoking their respective getTelemetryValue() methods within the SystemPerformanceManager method called handleTelemetry() using a scheduled thread. This work should be implemented within the SystemPerformanceManager class.

Review the README

  • Please see README.md for further information on, and use of, this content.
  • License for embedded documentation and source codes: PIOT-DOC-LIC

Estimated effort may vary greatly

  • The estimated level of effort for this exercise shown in the 'Estimate' section below is a very rough approximation. The actual level of effort may vary greatly depending on your development and test environment, experience with the requisite technologies, and many other factors.

Actions

NOTE: The implementation examples depicted here are only one way to implement the requirements listed. Your own implementation may vary of course.

  • Add the following import statements:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
  • Add the following members to the class:
private ScheduledExecutorService schedExecSvc = null;
private SystemCpuUtilTask sysCpuUtilTask = null;
private SystemMemUtilTask sysMemUtilTask = null;

private Runnable taskRunner = null;
private boolean isStarted = false;
  • Create a public method named 'handleTelemetry()' that includes the calls to retrieve the CPU and Memory utilization (again, these may not function properly on every OS).
  • Log either an information or debug / fine-grained message that includes the values of cpuUtil and memUtil.
public void handleTelemetry()
{
	float cpuUtil = this.sysCpuUtilTask.getTelemetryValue();
	float memUtil = this.sysMemUtilTask.getTelemetryValue();
	
	_Logger.fine("CPU utilization: " + cpuUtil + ", Mem utilization: " + memUtil);
}
  • Within the constructor, create the instances of SystemCpuUtilTask and SystemMemUtilTask, as well as the scheduler service to handle interval-based calling to retrieve each task's value:
public SystemPerformanceManager()
{
	this.pollRate =
		ConfigUtil.getInstance().getInteger(
			ConfigConst.GATEWAY_DEVICE, ConfigConst.POLL_CYCLES_KEY, ConfigConst.DEFAULT_POLL_CYCLES);
	
	if (this.pollRate <= 0) {
		this.pollRate = ConfigConst.DEFAULT_POLL_CYCLES;
	}
	
	this.schedExecSvc   = Executors.newScheduledThreadPool(1);
	this.sysCpuUtilTask = new SystemCpuUtilTask();
	this.sysMemUtilTask = new SystemMemUtilTask();
	
	this.taskRunner = () -> {
		this.handleTelemetry();
	};
}
  • Update the startManager() method, invoke the this.schedExecSvc.scheduleAtFixedRate() method to start the scheduler.
public boolean startManager()
{
	if (! this.isStarted) {
		_Logger.info("SystemPerformanceManager is starting...");
		
		ScheduledFuture<?> futureTask =
			this.schedExecSvc.scheduleAtFixedRate(this.taskRunner, 1L, this.pollRate, TimeUnit.SECONDS);
		
		this.isStarted = true;
	} else {
		_Logger.info("SystemPerformanceManager is already started.");
	}
	
	return this.isStarted;
}
  • Within the stopManager() method, invoke the this.schedExecSvc.shutdown(); call to stop the scheduler.
public boolean stopManager()
{
	this.schedExecSvc.shutdown();
	this.isStarted = false;
	
	_Logger.info("SystemPerformanceManager is stopped.");
	
	return true;
}

Estimate

  • Medium

Tests

  • Integration tests (in ./src/test/java/programmingtheiot/part01/integration)
    • Run ./app/GatewayDeviceAppTest. Integration test should pass and generate output similar to the following:
Jul 19, 2020 1:53:19 PM programmingtheiot.gda.app.GatewayDeviceApp <init>
INFO: Initializing GDA...
Jul 19, 2020 1:53:19 PM programmingtheiot.gda.app.GatewayDeviceApp parseArgs
INFO: No command line args to parse.
Jul 19, 2020 1:53:19 PM programmingtheiot.gda.app.GatewayDeviceApp initConfig
INFO: Attempting to load configuration: Default.
Jul 19, 2020 1:53:19 PM programmingtheiot.gda.app.GatewayDeviceApp startApp
INFO: Starting GDA...
Jul 19, 2020 1:53:19 PM programmingtheiot.gda.system.SystemPerformanceManager startManager
INFO: SystemPerformanceManager is starting...
Jul 19, 2020 1:53:19 PM programmingtheiot.gda.app.GatewayDeviceApp startApp
INFO: GDA started successfully.
Jul 19, 2020 1:53:20 PM programmingtheiot.gda.system.SystemPerformanceManager handleTelemetry
INFO: Handle telemetry results: cpuUtil=-1.0, memUtil=0.1469148
Jul 19, 2020 1:53:50 PM programmingtheiot.gda.system.SystemPerformanceManager handleTelemetry
INFO: Handle telemetry results: cpuUtil=-1.0, memUtil=0.1469148
Jul 19, 2020 1:54:20 PM programmingtheiot.gda.system.SystemPerformanceManager handleTelemetry
INFO: Handle telemetry results: cpuUtil=-1.0, memUtil=0.1469148
Jul 19, 2020 1:54:24 PM programmingtheiot.gda.app.GatewayDeviceApp stopApp
INFO: Stopping GDA...
Jul 19, 2020 1:54:24 PM programmingtheiot.gda.system.SystemPerformanceManager stopManager
INFO: SystemPerformanceManager is stopped.
Jul 19, 2020 1:54:24 PM programmingtheiot.gda.app.GatewayDeviceApp stopApp
INFO: GDA stopped successfully with exit code 0.
@labbenchstudios labbenchstudios self-assigned this Jul 15, 2020
@labbenchstudios labbenchstudios changed the title PIOT-GDA-02-006: Connect SystemCpuUtilTask to SystemPerformanceManager PIOT-GDA-02-007: Connect SystemCpuUtilTask to SystemPerformanceManager Jul 16, 2020
@labbenchstudios labbenchstudios changed the title PIOT-GDA-02-007: Connect SystemCpuUtilTask to SystemPerformanceManager PIOT-GDA-02-006: Connect SystemCpuUtilTask and SystemMemUtilTask to SystemPerformanceManager Jul 18, 2020
@labbenchstudios labbenchstudios removed their assignment Aug 17, 2020
@labbenchstudios labbenchstudios transferred this issue from programming-the-iot/java-components Sep 7, 2020
@labbenchstudios labbenchstudios added the exercise New feature to implement as an exercise label Sep 7, 2020
@labbenchstudios labbenchstudios added this to the Chapter 02 milestone Sep 7, 2020
@labbenchstudios labbenchstudios changed the title PIOT-GDA-02-006: Connect SystemCpuUtilTask and SystemMemUtilTask to SystemPerformanceManager PIOT-GDA-02-007: Connect SystemCpuUtilTask and SystemMemUtilTask to SystemPerformanceManager Sep 18, 2020
@labbenchstudios
Copy link
Contributor Author

Fixed var names in example code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exercise New feature to implement as an exercise
Projects
Programming the IoT - Exercises Kanba...
  
Lab Module 02 - Edge Tier Apps
Development

No branches or pull requests

1 participant