|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright (C) 2025 John Harrigan <[email protected]> & Matt Lucius <[email protected]> |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or |
| 6 | +# modify it under the terms of the GNU General Public License |
| 7 | +# as published by the Free Software Foundation; either version 2 |
| 8 | +# of the License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program; if not, write to the Free Software |
| 17 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | +# |
| 19 | +# Executed by systemd service 'PCPrecord.service' |
| 20 | +# See: /etc/systemd/system/PCPrecord.service |
| 21 | +################################################################ |
| 22 | + |
| 23 | +# GLOBALS ################### |
| 24 | +# Include the PCP Functions file |
| 25 | +source $PWD/pcp_functions.inc |
| 26 | + |
| 27 | +FIFO="/tmp/pcpFIFO" # get from cmdline |
| 28 | +sample_rate=5 # hardcode DEFAULT for now |
| 29 | +pmlogger_running="false" # Initialize service as OFF |
| 30 | +om_workload_file="/tmp/openmetrics_workload.txt" |
| 31 | +om_workload_file_reset="/tmp/openmetrics_workload_reset.txt" |
| 32 | + |
| 33 | +############################# |
| 34 | +# Functions ################# |
| 35 | + |
| 36 | +reset_om_metrics() |
| 37 | +{ |
| 38 | + echo "Reset OpenMetrics values" |
| 39 | + cp ${om_workload_file_reset} ${om_workload_file} |
| 40 | +} |
| 41 | + |
| 42 | +error_exit() |
| 43 | +{ |
| 44 | + if [[ "$?" != "0" ]]; then |
| 45 | + systemd-notify --status="ERROR: $1" |
| 46 | + # Additional error handling logic can be added here |
| 47 | + rm -f "$FIFO" |
| 48 | + # Reset openmetric.workload metric values prior to leaving |
| 49 | + reset_om_metrics |
| 50 | +## if pmlogger_running = True then attempt forcible STOP? |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +} |
| 54 | +# END Functions ################# |
| 55 | + |
| 56 | +# Main ################# |
| 57 | +# Initialize openmetric.workload metric values |
| 58 | +reset_om_metrics |
| 59 | + |
| 60 | +# Verify required files and Packages are available |
| 61 | +#---------------------------------- |
| 62 | +test -f "${om_workload_file}" |
| 63 | +error_exit "Initialization: ${om_workload_file} not found!" |
| 64 | + |
| 65 | +# Remove and recreate FIFO on every service 'start' |
| 66 | +rm -f "$FIFO" |
| 67 | +mkfifo "$FIFO" |
| 68 | +error_exit "Initialization: mkfifo $FIFO failed" |
| 69 | + |
| 70 | +## DEBUG - measure processing interval: $postaction-$preaction |
| 71 | +action='NONE' |
| 72 | +interval=0.0 |
| 73 | + |
| 74 | +# Infinite Loop ################# |
| 75 | +# Read FIFO and perform requested ACTION (start, stop, ...) |
| 76 | +# Access each word in $action string for parsing 'actions' & 'metric' |
| 77 | +# NOTE: 'Start, Stop, Reset' actions have no metrics |
| 78 | +while : ; do |
| 79 | + # Required or we get TIMEOUT on 'read action < "$FIFO" ' |
| 80 | + # Signal readiness for next $action. SYNC point w/client Workload |
| 81 | + # Report timing interval for most recent ACTION |
| 82 | + systemd-notify --ready --status="READY: last-action - $action = ${interval}ms" |
| 83 | + # Read the Request/'$action' and then process it |
| 84 | + read action < "$FIFO" # Blocks until data is available |
| 85 | + # Signal busy Processing this $action |
| 86 | + systemd-notify --status="$action PMLOGGER Request" |
| 87 | + action_arr=($action) # Array of 'words' in Request read from FIFO |
| 88 | +## DEBUG - measure processing interval for ACTION: $postaction-$preaction |
| 89 | + preaction=$(mark_ms) |
| 90 | + case "${action_arr[0]}" in |
| 91 | + Start) # 'Start $archive_dir $test_name $conf_file' |
| 92 | + archive_dir="${action_arr[1]}" |
| 93 | + archive_name="${action_arr[2]}" |
| 94 | + conf_file="${action_arr[3]}" |
| 95 | + # Start PMLOGGER to create ARCHIVE |
| 96 | + if [[ "$pmlogger_running" = "false" ]]; then |
| 97 | + # Signal Processing this $action |
| 98 | + systemd-notify --status="DEBUG: $action PMLOGGER Request" |
| 99 | + # These functions attempt to catch errors and verify success |
| 100 | + pcp_verify $conf_file |
| 101 | + error_exit "pcp_verify: Unable to start PMLOGGER" |
| 102 | + pcp_start $conf_file $sample_rate $archive_dir $archive_name |
| 103 | + error_exit "pcp_start: Unable to start PMLOGGER" |
| 104 | + pmlogger_running="true" # Record this STATE info |
| 105 | + fi |
| 106 | + ;; |
| 107 | + Stop) # artifacts_dir="${action_arr[1]}" |
| 108 | + # Terminate PMLOGGER |
| 109 | + if [[ "$pmlogger_running" = "true" ]]; then |
| 110 | + # Will ZATHRAS Store PCP Archive related artifacts ? |
| 111 | + # - Currently Missing from PCPSTOP logic |
| 112 | + ##pcp_stop "${artifacts_dir}" |
| 113 | + pcp_stop |
| 114 | + error_exit "pcp_stop: Unable to stop PMLOGGER" |
| 115 | + pmlogger_running="false" |
| 116 | + fi |
| 117 | + ;; |
| 118 | + Reset) # om_workload_file="${action_arr[1]}" |
| 119 | + # RESET the Workload Metrics |
| 120 | + # the only Request that doesn't require $pmlogger_running |
| 121 | + reset_om_metrics |
| 122 | + error_exit "reset_om_metrics: Unable to RESET Workload Metrics" |
| 123 | + ;; |
| 124 | + throughput|latency|numthreads|runtime) # Workload Metrics |
| 125 | + # metric="${action_arr[1]}" om_workload_file=$2 |
| 126 | + if [[ "$pmlogger_running" = "true" ]]; then |
| 127 | + # Forward workload metric to openmetrics_workload.txt |
| 128 | + # Change only one metric line at a time |
| 129 | + # Replaces the entire line using sed |
| 130 | + # Should I only print 'action_arr[0] & action_arr[1]' |
| 131 | + sed -i "s/^.*${action_arr[0]}.*$/${action}/" "$om_workload_file" |
| 132 | + fi |
| 133 | + ;; |
| 134 | + running|iteration) # Workload States |
| 135 | + # state="${action_arr[1]}" om_workload_file=$2 |
| 136 | + if [[ "$pmlogger_running" = "true" ]]; then |
| 137 | + sed -i "s/^.*${action_arr[0]}.*$/${action}/" "$om_workload_file" |
| 138 | + fi |
| 139 | + ;; |
| 140 | + *) |
| 141 | + systemd-notify --status="Unrecognized action - IGNORED" |
| 142 | + ;; |
| 143 | + esac |
| 144 | +## DEBUG - measure time interval for processing ACTION |
| 145 | + postaction=$(mark_ms) |
| 146 | + interval=$(( 10*(postaction - preaction) )) |
| 147 | +done |
| 148 | + |
| 149 | +# Cleanup |
| 150 | +echo "Cleaning up" |
| 151 | + |
| 152 | +# Reset openmetric.workload metric values prior to leaving |
| 153 | +reset_om_metrics |
| 154 | + |
| 155 | +exit 0 |
0 commit comments