From f4af520b2a6ac951968df36a890aa8c80b07adb8 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Fri, 4 Mar 2022 11:32:39 +0100 Subject: [PATCH 1/7] Fix error print in LustreCollector --- collectors/lustreMetric.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collectors/lustreMetric.go b/collectors/lustreMetric.go index f98d746..cb3df06 100644 --- a/collectors/lustreMetric.go +++ b/collectors/lustreMetric.go @@ -116,7 +116,7 @@ func (m *LustreCollector) Init(config json.RawMessage) error { return err } if user.Uid != "0" { - cclog.ComponentError(m.name, "Lustre file system statistics can only be queried by user root:", err.Error()) + cclog.ComponentError(m.name, "Lustre file system statistics can only be queried by user root") return err } From 64c41be34ccb9e47d754607e10b9a1dfc0f76b0d Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Fri, 4 Mar 2022 11:37:45 +0100 Subject: [PATCH 2/7] Fix name for ClusterCockpit --- scripts/cc-metric-collector.sysusers | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/cc-metric-collector.sysusers b/scripts/cc-metric-collector.sysusers index 5c17ec7..6ce3700 100644 --- a/scripts/cc-metric-collector.sysusers +++ b/scripts/cc-metric-collector.sysusers @@ -1,2 +1,2 @@ -#Type Name ID GECOS Home directory Shell -u hpcop - "User for ClusterCockpit" /run/cc-metric-collector /sbin/nologin +#Type Name ID GECOS Home directory Shell +u clustercockpit - "User for ClusterCockpit" /run/cc-metric-collector /sbin/nologin From ff08eaeb4320de85f6b4df348f7b6dd74103ab27 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Fri, 4 Mar 2022 11:49:55 +0100 Subject: [PATCH 3/7] Set proper user for files --- scripts/cc-metric-collector.spec | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/cc-metric-collector.spec b/scripts/cc-metric-collector.spec index 00a4aa4..bafff00 100644 --- a/scripts/cc-metric-collector.spec +++ b/scripts/cc-metric-collector.spec @@ -51,16 +51,19 @@ install -Dpm 0644 scripts/%{name}.sysusers %{buildroot}%{_sysusersdir}/%{name}.c %systemd_preun %{name}.service %files +# Binary +%attr(-,clustercockpit,clustercockpit) %{_sbindir}/%{name} +# Configuration %dir %{_sysconfdir}/%{name} -%{_sbindir}/%{name} +%attr(0600,clustercockpit,clustercockpit) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.json +%attr(0600,clustercockpit,clustercockpit) %config(noreplace) %{_sysconfdir}/%{name}/collectors.json +%attr(0600,clustercockpit,clustercockpit) %config(noreplace) %{_sysconfdir}/%{name}/sinks.json +%attr(0600,clustercockpit,clustercockpit) %config(noreplace) %{_sysconfdir}/%{name}/receivers.json +%attr(0600,clustercockpit,clustercockpit) %config(noreplace) %{_sysconfdir}/%{name}/router.json +# Systemd +%{_sysusersdir}/%{name}.conf %{_unitdir}/%{name}.service %{_sysconfdir}/default/%{name} -%attr(0600,root,root) %config(noreplace) %{_sysconfdir}/%{name}/%{name}.json -%attr(0600,root,root) %config(noreplace) %{_sysconfdir}/%{name}/collectors.json -%attr(0600,root,root) %config(noreplace) %{_sysconfdir}/%{name}/sinks.json -%attr(0600,root,root) %config(noreplace) %{_sysconfdir}/%{name}/receivers.json -%attr(0600,root,root) %config(noreplace) %{_sysconfdir}/%{name}/router.json -%{_sysusersdir}/%{name}.conf %changelog * Thu Mar 03 2022 Thomas Gruber - 0.3 From 3f76947f5408c499beb6c077d9b0a161e79211ff Mon Sep 17 00:00:00 2001 From: Thomas Gruber Date: Tue, 15 Mar 2022 16:41:11 +0100 Subject: [PATCH 4/7] Merge latest developments into main (#67) * Update configuration.md Add an additional receiver to have better alignment of components * Change default GpfsCollector command to `mmpmon` (#53) * Set default cmd to 'mmpmon' * Reuse looked up path * Cast const to string * Just download LIKWID to get the headers (#54) * Just download LIKWID to get the headers * Remove perl-Data-Dumper from BuildRequires, only required by LIKWID build * Add HttpReceiver as counterpart to the HttpSink (#49) * Use GBytes as unit for large memory numbers * Make maxForward configurable, save old name in meta in rename metrics and make the hostname tag key configurable * Single release action (#55) Building all RPMs and releasing in a single workflow * Makefile target to build binary-only Debian packages (#61) * Add 'install' and 'DEB' make targets to build binary-only Debian packages * Add control file for DEB builds * Use a single line for bash loop in make clean * Add config options for retry intervals of InfluxDB clients (#59) * Refactoring of LikwidCollector and metric units (#62) * Reduce complexity of LikwidCollector and allow metric units * Add unit to LikwidCollector docu and fix some typos * Make library path configurable * Use old metric name in Ganglia if rename has happened in the router (#60) * Use old metric name if rename has happened in the router * Also check for Ganglia renames for the oldname * Derived metrics (#57) * Add time-based derivatived (e.g. bandwidth) to some collectors * Add documentation * Add comments * Fix: Only compute rates with a valid previous state * Only compute rates with a valid previous state * Define const values for net/dev fields * Set default config values * Add comments * Refactor: Consolidate data structures * Refactor: Consolidate data structures * Refactor: Avoid struct deep copy * Refactor: Avoid redundant tag maps * Refactor: Use int64 type for absolut values Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Simplified iota usage * Move unit tag to meta data tags * Derived metrics (#65) * Add time-based derivatived (e.g. bandwidth) to some collectors * Add documentation * Add comments * Fix: Only compute rates with a valid previous state * Only compute rates with a valid previous state * Define const values for net/dev fields * Set default config values * Add comments * Refactor: Consolidate data structures * Refactor: Consolidate data structures * Refactor: Avoid struct deep copy * Refactor: Avoid redundant tag maps * Refactor: Use int64 type for absolut values * Update LustreCollector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Meta to tags list and map for sinks (#63) * Change ccMetric->Influx functions * Use a meta_as_tags string list in config but create a lookup map afterwards * Add meta as tag logic to sampleSink * Fix staticcheck warnings (#66) Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> --- .github/workflows/AlmaLinux.yml | 64 --- .../workflows/RedHatUniversalBaseImage.yml | 64 --- .github/workflows/Release.yml | 184 +++++++++ Makefile | 53 ++- collectors.json | 6 + collectors/Makefile | 88 +---- collectors/cpufreqCpuinfoMetric.go | 6 +- collectors/cpufreqMetric.go | 16 +- collectors/cpustatMetric.go | 9 +- collectors/customCmdMetric.go | 2 +- collectors/gpfsMetric.go | 57 ++- collectors/gpfsMetric.md | 10 +- collectors/infinibandMetric.go | 60 ++- collectors/infinibandMetric.md | 8 +- collectors/ipmiMetric.go | 2 +- collectors/likwidMetric.go | 300 ++++---------- collectors/likwidMetric.md | 36 +- collectors/lustreMetric.go | 370 ++++++++++++++---- collectors/lustreMetric.md | 47 ++- collectors/memstatMetric.go | 38 +- collectors/metricCollector.go | 2 +- collectors/netstatMetric.go | 167 +++++--- collectors/netstatMetric.md | 16 +- collectors/tempMetric.go | 6 +- collectors/topprocsMetric.go | 4 +- docs/configuration.md | 6 +- internal/ccMetric/ccMetric.go | 25 +- internal/metricRouter/metricRouter.go | 20 +- receivers/README.md | 1 + receivers/httpReceiver.go | 118 ++++++ receivers/httpReceiver.md | 23 ++ scripts/cc-metric-collector.deb.control | 12 + scripts/cc-metric-collector.spec | 4 +- sinks/gangliaCommon.go | 11 + sinks/gangliaSink.go | 7 +- sinks/httpSink.go | 7 +- sinks/influxAsyncSink.go | 66 +++- sinks/influxAsyncSink.md | 12 +- sinks/influxSink.go | 61 ++- sinks/influxSink.md | 12 +- sinks/libgangliaSink.go | 9 +- sinks/metricSink.go | 8 +- sinks/natsSink.go | 9 +- sinks/sampleSink.go | 11 +- sinks/stdoutSink.go | 6 +- 45 files changed, 1329 insertions(+), 714 deletions(-) delete mode 100644 .github/workflows/AlmaLinux.yml delete mode 100644 .github/workflows/RedHatUniversalBaseImage.yml create mode 100644 .github/workflows/Release.yml create mode 100644 receivers/httpReceiver.go create mode 100644 receivers/httpReceiver.md create mode 100644 scripts/cc-metric-collector.deb.control diff --git a/.github/workflows/AlmaLinux.yml b/.github/workflows/AlmaLinux.yml deleted file mode 100644 index dd06dd2..0000000 --- a/.github/workflows/AlmaLinux.yml +++ /dev/null @@ -1,64 +0,0 @@ -# See: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions - -# Workflow name -name: AlmaLinux 8.5 RPM build - -# Run on tag push -on: - push: - tags: - - '**' - -jobs: - - # - # Build on AlmaLinux 8.5 using go-toolset - # - AlmaLinux-RPM-build: - runs-on: ubuntu-latest - # See: https://hub.docker.com/_/almalinux - container: almalinux:8.5 - steps: - - # Use dnf to install development packages - - name: Install development packages - run: dnf --assumeyes group install "Development Tools" "RPM Development Tools" - - # Checkout git repository and submodules - # fetch-depth must be 0 to use git describe - # See: https://github.com/marketplace/actions/checkout - - name: Checkout - uses: actions/checkout@v2 - with: - submodules: recursive - fetch-depth: 0 - - # Use dnf to install build dependencies - - name: Install build dependencies - run: dnf --assumeyes builddep scripts/cc-metric-collector.spec - - - name: RPM build MetricCollector - id: rpmbuild - run: make RPM - - # See: https://github.com/actions/upload-artifact - - name: Save RPM as artifact - uses: actions/upload-artifact@v2 - with: - name: cc-metric-collector RPM for AlmaLinux 8.5 - path: ${{ steps.rpmbuild.outputs.RPM }} - - name: Save SRPM as artifact - uses: actions/upload-artifact@v2 - with: - name: cc-metric-collector SRPM for AlmaLinux 8.5 - path: ${{ steps.rpmbuild.outputs.SRPM }} - - # See: https://github.com/softprops/action-gh-release - - name: Release - uses: softprops/action-gh-release@v1 - if: startsWith(github.ref, 'refs/tags/') - with: - name: cc-metric-collector-${{github.ref_name}} - files: | - ${{ steps.rpmbuild.outputs.RPM }} - ${{ steps.rpmbuild.outputs.SRPM }} diff --git a/.github/workflows/RedHatUniversalBaseImage.yml b/.github/workflows/RedHatUniversalBaseImage.yml deleted file mode 100644 index 205a133..0000000 --- a/.github/workflows/RedHatUniversalBaseImage.yml +++ /dev/null @@ -1,64 +0,0 @@ -# See: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions - -# Workflow name -name: Red Hat Universal Base Image 8 RPM build - -# Run on tag push -on: - push: - tags: - - '**' - -jobs: - - # - # Build on UBI 8 using go-toolset - # - UBI-8-RPM-build: - runs-on: ubuntu-latest - # See: https://catalog.redhat.com/software/containers/ubi8/ubi/5c359854d70cc534b3a3784e?container-tabs=gti - container: registry.access.redhat.com/ubi8/ubi:8.5-226.1645809065 - steps: - - # Use dnf to install development packages - - name: Install development packages - run: dnf --assumeyes --disableplugin=subscription-manager install rpm-build go-srpm-macros rpm-build-libs rpm-libs gcc make python38 git - - # Checkout git repository and submodules - # fetch-depth must be 0 to use git describe - # See: https://github.com/marketplace/actions/checkout - - name: Checkout - uses: actions/checkout@v2 - with: - submodules: recursive - fetch-depth: 0 - - # Use dnf to install build dependencies - - name: Install build dependencies - run: dnf --assumeyes --disableplugin=subscription-manager builddep scripts/cc-metric-collector.spec - - - name: RPM build MetricCollector - id: rpmbuild - run: make RPM - - # See: https://github.com/actions/upload-artifact - - name: Save RPM as artifact - uses: actions/upload-artifact@v2 - with: - name: cc-metric-collector RPM for UBI 8 - path: ${{ steps.rpmbuild.outputs.RPM }} - - name: Save SRPM as artifact - uses: actions/upload-artifact@v2 - with: - name: cc-metric-collector SRPM for UBI 8 - path: ${{ steps.rpmbuild.outputs.SRPM }} - - # See: https://github.com/softprops/action-gh-release - - name: Release - uses: softprops/action-gh-release@v1 - if: startsWith(github.ref, 'refs/tags/') - with: - name: cc-metric-collector-${{github.ref_name}} - files: | - ${{ steps.rpmbuild.outputs.RPM }} - ${{ steps.rpmbuild.outputs.SRPM }} diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml new file mode 100644 index 0000000..9ce5615 --- /dev/null +++ b/.github/workflows/Release.yml @@ -0,0 +1,184 @@ +# See: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions + +# Workflow name +name: Release + +# Run on tag push +on: + push: + tags: + - '**' + +jobs: + + # + # Build on AlmaLinux 8.5 using go-toolset + # + AlmaLinux-RPM-build: + runs-on: ubuntu-latest + # See: https://hub.docker.com/_/almalinux + container: almalinux:8.5 + # The job outputs link to the outputs of the 'rpmrename' step + # Only job outputs can be used in child jobs + outputs: + rpm : ${{steps.rpmrename.outputs.RPM}} + srpm : ${{steps.rpmrename.outputs.SRPM}} + steps: + + # Use dnf to install development packages + - name: Install development packages + run: dnf --assumeyes group install "Development Tools" "RPM Development Tools" + + # Checkout git repository and submodules + # fetch-depth must be 0 to use git describe + # See: https://github.com/marketplace/actions/checkout + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: recursive + fetch-depth: 0 + + # Use dnf to install build dependencies + - name: Install build dependencies + run: dnf --assumeyes builddep scripts/cc-metric-collector.spec + + - name: RPM build MetricCollector + id: rpmbuild + run: make RPM + + # AlmaLinux 8.5 is a derivate of RedHat Enterprise Linux 8 (UBI8), + # so the created RPM both contain the substring 'el8' in the RPM file names + # This step replaces the substring 'el8' to 'alma85'. It uses the move operation + # because it is unclear whether the default AlmaLinux 8.5 container contains the + # 'rename' command. This way we also get the new names for output. + - name: Rename RPMs (s/el8/alma85/) + id: rpmrename + run: | + OLD_RPM="${{steps.rpmbuild.outputs.RPM}}" + OLD_SRPM="${{steps.rpmbuild.outputs.SRPM}}" + NEW_RPM="${OLD_RPM/el8/alma85}" + NEW_SRPM=${OLD_SRPM/el8/alma85} + mv "${OLD_RPM}" "${NEW_RPM}" + mv "${OLD_SRPM}" "${NEW_SRPM}" + echo "::set-output name=SRPM::${NEW_SRPM}" + echo "::set-output name=RPM::${NEW_RPM}" + + # See: https://github.com/actions/upload-artifact + - name: Save RPM as artifact + uses: actions/upload-artifact@v2 + with: + name: cc-metric-collector RPM for AlmaLinux 8.5 + path: ${{ steps.rpmrename.outputs.RPM }} + - name: Save SRPM as artifact + uses: actions/upload-artifact@v2 + with: + name: cc-metric-collector SRPM for AlmaLinux 8.5 + path: ${{ steps.rpmrename.outputs.SRPM }} + + # + # Build on UBI 8 using go-toolset + # + UBI-8-RPM-build: + runs-on: ubuntu-latest + # See: https://catalog.redhat.com/software/containers/ubi8/ubi/5c359854d70cc534b3a3784e?container-tabs=gti + container: registry.access.redhat.com/ubi8/ubi:8.5-226.1645809065 + # The job outputs link to the outputs of the 'rpmbuild' step + outputs: + rpm : ${{steps.rpmbuild.outputs.RPM}} + srpm : ${{steps.rpmbuild.outputs.SRPM}} + steps: + + # Use dnf to install development packages + - name: Install development packages + run: dnf --assumeyes --disableplugin=subscription-manager install rpm-build go-srpm-macros rpm-build-libs rpm-libs gcc make python38 git + + # Checkout git repository and submodules + # fetch-depth must be 0 to use git describe + # See: https://github.com/marketplace/actions/checkout + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: recursive + fetch-depth: 0 + + # Use dnf to install build dependencies + - name: Install build dependencies + run: dnf --assumeyes --disableplugin=subscription-manager builddep scripts/cc-metric-collector.spec + + - name: RPM build MetricCollector + id: rpmbuild + run: make RPM + + # See: https://github.com/actions/upload-artifact + - name: Save RPM as artifact + uses: actions/upload-artifact@v2 + with: + name: cc-metric-collector RPM for UBI 8 + path: ${{ steps.rpmbuild.outputs.RPM }} + - name: Save SRPM as artifact + uses: actions/upload-artifact@v2 + with: + name: cc-metric-collector SRPM for UBI 8 + path: ${{ steps.rpmbuild.outputs.SRPM }} + + # + # Create release with fresh RPMs + # + Release: + runs-on: ubuntu-latest + # We need the RPMs, so add dependency + needs: [AlmaLinux-RPM-build, UBI-8-RPM-build] + + steps: + # See: https://github.com/actions/download-artifact + - name: Download AlmaLinux 8.5 RPM + uses: actions/download-artifact@v2 + with: + name: cc-metric-collector RPM for AlmaLinux 8.5 + - name: Download AlmaLinux 8.5 SRPM + uses: actions/download-artifact@v2 + with: + name: cc-metric-collector SRPM for AlmaLinux 8.5 + + - name: Download UBI 8 RPM + uses: actions/download-artifact@v2 + with: + name: cc-metric-collector RPM for UBI 8 + - name: Download UBI 8 SRPM + uses: actions/download-artifact@v2 + with: + name: cc-metric-collector SRPM for UBI 8 + + # The download actions do not publish the name of the downloaded file, + # so we re-use the job outputs of the parent jobs. The files are all + # downloaded to the current folder. + # The gh-release action afterwards does not accept file lists but all + # files have to be listed at 'files'. The step creates one output per + # RPM package (2 per distro) + - name: Set RPM variables + id: files + run: | + ALMA_85_RPM=$(basename "${{ needs.AlmaLinux-RPM-build.outputs.rpm}}") + ALMA_85_SRPM=$(basename "${{ needs.AlmaLinux-RPM-build.outputs.srpm}}") + UBI_8_RPM=$(basename "${{ needs.UBI-8-RPM-build.outputs.rpm}}") + UBI_8_SRPM=$(basename "${{ needs.UBI-8-RPM-build.outputs.srpm}}") + echo "ALMA_85_RPM::${ALMA_85_RPM}" + echo "ALMA_85_SRPM::${ALMA_85_SRPM}" + echo "UBI_8_RPM::${UBI_8_RPM}" + echo "UBI_8_SRPM::${UBI_8_SRPM}" + echo "::set-output name=ALMA_85_RPM::${ALMA_85_RPM}" + echo "::set-output name=ALMA_85_SRPM::${ALMA_85_SRPM}" + echo "::set-output name=UBI_8_RPM::${UBI_8_RPM}" + echo "::set-output name=UBI_8_SRPM::${UBI_8_SRPM}" + + # See: https://github.com/softprops/action-gh-release + - name: Release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + name: cc-metric-collector-${{github.ref_name}} + files: | + ${{ steps.files.outputs.ALMA_85_RPM }} + ${{ steps.files.outputs.ALMA_85_SRPM }} + ${{ steps.files.outputs.UBI_8_RPM }} + ${{ steps.files.outputs.UBI_8_SRPM }} \ No newline at end of file diff --git a/Makefile b/Makefile index d747899..0a7ad04 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,8 @@ COMPONENT_DIRS := collectors \ internal/ccTopology \ internal/multiChanTicker +BINDIR = bin + .PHONY: all all: $(APP) @@ -24,15 +26,27 @@ $(APP): $(GOSRC) go get go build -o $(APP) $(GOSRC_APP) +install: $(APP) + @WORKSPACE=$(PREFIX) + @if [ -z "$${WORKSPACE}" ]; then exit 1; fi + @mkdir --parents --verbose $${WORKSPACE}/usr/$(BINDIR) + @install -Dpm 755 $(APP) $${WORKSPACE}/usr/$(BINDIR)/$(APP) + @mkdir --parents --verbose $${WORKSPACE}/etc/cc-metric-collector $${WORKSPACE}/etc/default $${WORKSPACE}/etc/systemd/system $${WORKSPACE}/etc/init.d + @install -Dpm 600 config.json $${WORKSPACE}/etc/cc-metric-collector/cc-metric-collector.json + @sed -i -e s+"\"./"+"\"/etc/cc-metric-collector/"+g $${WORKSPACE}/etc/cc-metric-collector/cc-metric-collector.json + @install -Dpm 600 sinks.json $${WORKSPACE}/etc/cc-metric-collector/sinks.json + @install -Dpm 600 collectors.json $${WORKSPACE}/etc/cc-metric-collector/collectors.json + @install -Dpm 600 router.json $${WORKSPACE}/etc/cc-metric-collector/router.json + @install -Dpm 600 receivers.json $${WORKSPACE}/etc/cc-metric-collector/receivers.json + @install -Dpm 600 scripts/cc-metric-collector.config $${WORKSPACE}/etc/default/cc-metric-collector + @install -Dpm 644 scripts/cc-metric-collector.service $${WORKSPACE}/etc/systemd/system/cc-metric-collector.service + @install -Dpm 644 scripts/cc-metric-collector.init $${WORKSPACE}/etc/init.d/cc-metric-collector + + .PHONY: clean .ONESHELL: clean: - @for COMP in $(COMPONENT_DIRS) - do - if [[ -e $$COMP/Makefile ]]; then - make -C $$COMP clean - fi - done + @for COMP in $(COMPONENT_DIRS); do if [ -e $$COMP/Makefile ]; then make -C $$COMP clean; fi; done rm -f $(APP) .PHONY: fmt @@ -69,7 +83,7 @@ RPM: scripts/cc-metric-collector.spec @COMMITISH="HEAD" @VERS=$$(git describe --tags $${COMMITISH}) @VERS=$${VERS#v} - @VERS=$${VERS//-/_} + @VERS=$$(echo $$VERS | sed -e s+'-'+'_'+g) @eval $$(rpmspec --query --queryformat "NAME='%{name}' VERSION='%{version}' RELEASE='%{release}' NVR='%{NVR}' NVRA='%{NVRA}'" --define="VERS $${VERS}" "$${SPECFILE}") @PREFIX="$${NAME}-$${VERSION}" @FORMAT="tar.gz" @@ -86,3 +100,28 @@ RPM: scripts/cc-metric-collector.spec @ echo "::set-output name=SRPM::$${SRPMFILE}" @ echo "::set-output name=RPM::$${RPMFILE}" @fi + +.PHONY: DEB +DEB: scripts/cc-metric-collector.deb.control $(APP) + @BASEDIR=$${PWD} + @WORKSPACE=$${PWD}/.dpkgbuild + @DEBIANDIR=$${WORKSPACE}/debian + @DEBIANBINDIR=$${WORKSPACE}/DEBIAN + @mkdir --parents --verbose $$WORKSPACE $$DEBIANBINDIR + #@mkdir --parents --verbose $$DEBIANDIR + @CONTROLFILE="$${BASEDIR}/scripts/cc-metric-collector.deb.control" + @COMMITISH="HEAD" + @VERS=$$(git describe --tags --abbrev=0 $${COMMITISH}) + @VERS=$${VERS#v} + @VERS=$$(echo $$VERS | sed -e s+'-'+'_'+g) + @ARCH=$$(uname -m) + @ARCH=$$(echo $$ARCH | sed -e s+'_'+'-'+g) + @PREFIX="$${NAME}-$${VERSION}_$${ARCH}" + @SIZE_BYTES=$$(du -bcs --exclude=.dpkgbuild "$$WORKSPACE"/ | awk '{print $$1}' | head -1 | sed -e 's/^0\+//') + @SIZE="$$(awk -v size="$$SIZE_BYTES" 'BEGIN {print (size/1024)+1}' | awk '{print int($$0)}')" + #@sed -e s+"{VERSION}"+"$$VERS"+g -e s+"{INSTALLED_SIZE}"+"$$SIZE"+g -e s+"{ARCH}"+"$$ARCH"+g $$CONTROLFILE > $${DEBIANDIR}/control + @sed -e s+"{VERSION}"+"$$VERS"+g -e s+"{INSTALLED_SIZE}"+"$$SIZE"+g -e s+"{ARCH}"+"$$ARCH"+g $$CONTROLFILE > $${DEBIANBINDIR}/control + @make PREFIX=$${WORKSPACE} install + @DEB_FILE="cc-metric-collector_$${VERS}_$${ARCH}.deb" + @dpkg-deb -b $${WORKSPACE} "$$DEB_FILE" + @rm -r "$${WORKSPACE}" diff --git a/collectors.json b/collectors.json index 27ef822..be3eea7 100644 --- a/collectors.json +++ b/collectors.json @@ -12,6 +12,12 @@ "proc_total" ] }, + "netstat": { + "include_devices": [ + "enp5s0" + ], + "send_derived_values": true + }, "numastats": {}, "nvidia": {}, "tempstat": { diff --git a/collectors/Makefile b/collectors/Makefile index 379a2d6..b07bccd 100644 --- a/collectors/Makefile +++ b/collectors/Makefile @@ -1,79 +1,25 @@ -# Use central installation -CENTRAL_INSTALL = false -# How to access hardware performance counters through LIKWID. -# Recommended is 'direct' mode -ACCESSMODE = direct -####################################################################### -# if CENTRAL_INSTALL == true -####################################################################### -# Path to central installation (if CENTRAL_INSTALL=true) -LIKWID_BASE=/apps/likwid/5.2.1 -# LIKWID version (should be same major version as central installation, 5.2.x) +all: likwid + + +# LIKWID version LIKWID_VERSION = 5.2.1 -####################################################################### -# if CENTRAL_INSTALL == false and ACCESSMODE == accessdaemon -####################################################################### -# Where to install the accessdaemon -DAEMON_INSTALLDIR = /usr/local -# Which user to use for the accessdaemon -DAEMON_USER = root -# Which group to use for the accessdaemon -DAEMON_GROUP = root +.ONESHELL: +.PHONY: likwid +likwid: + INSTALL_FOLDER="$${PWD}/likwid" + BUILD_FOLDER="$${PWD}/likwidbuild" + if [ -d $${INSTALL_FOLDER} ]; then rm -r $${INSTALL_FOLDER}; fi + mkdir --parents --verbose $${INSTALL_FOLDER} $${BUILD_FOLDER} + wget -P "$${BUILD_FOLDER}" ftp://ftp.rrze.uni-erlangen.de/mirrors/likwid/likwid-$(LIKWID_VERSION).tar.gz + tar -C $${BUILD_FOLDER} -xf $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION).tar.gz + install -Dpm 0644 $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION)/src/includes/likwid*.h $${INSTALL_FOLDER}/ + install -Dpm 0644 $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION)/src/includes/bstrlib.h $${INSTALL_FOLDER}/ + rm -r $${BUILD_FOLDER} - -################################################# -# No need to change anything below this line -################################################# -INSTALL_FOLDER = ./likwid -BUILD_FOLDER = ./likwid/build - -ifneq ($(strip $(CENTRAL_INSTALL)),true) -LIKWID_BASE := $(shell pwd)/$(INSTALL_FOLDER) -DAEMON_BASE := $(LIKWID_BASE) -GROUPS_BASE := $(LIKWID_BASE)/groups -all: $(INSTALL_FOLDER)/liblikwid.a cleanup -else -DAEMON_BASE= $(LIKWID_BASE)/sbin -all: $(INSTALL_FOLDER)/liblikwid.a cleanup -endif - - - -$(BUILD_FOLDER)/likwid-$(LIKWID_VERSION).tar.gz: $(BUILD_FOLDER) - wget -P $(BUILD_FOLDER) ftp://ftp.rrze.uni-erlangen.de/mirrors/likwid/likwid-$(LIKWID_VERSION).tar.gz - -$(BUILD_FOLDER): - mkdir -p $(BUILD_FOLDER) - -$(INSTALL_FOLDER): - mkdir -p $(INSTALL_FOLDER) - -$(BUILD_FOLDER)/likwid-$(LIKWID_VERSION): $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION).tar.gz - tar -C $(BUILD_FOLDER) -xf $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION).tar.gz - -$(INSTALL_FOLDER)/liblikwid.a: $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION) $(INSTALL_FOLDER) - cd "$(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)" && make "PREFIX=$(LIKWID_BASE)" "SHARED_LIBRARY=false" "ACCESSMODE=$(ACCESSMODE)" "INSTALLED_ACCESSDAEMON=$(DAEMON_INSTALLDIR)/likwid-accessD" - cp \ - $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)/liblikwid.a \ - $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)/ext/hwloc/liblikwid-hwloc.a \ - $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)/src/includes/likwid*.h \ - $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)/src/includes/bstrlib.h \ - $(INSTALL_FOLDER) - -$(DAEMON_INSTALLDIR)/likwid-accessD: $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)/likwid-accessD - sudo -u $(DAEMON_USER) -g $(DAEMON_GROUP) install -m 4775 $(BUILD_FOLDER)/likwid-$(LIKWID_VERSION)/likwid-accessD $(DAEMON_INSTALLDIR)/likwid-accessD - -prepare_collector: likwidMetric.go - cp likwidMetric.go likwidMetric.go.orig - sed -i -e s+"const GROUPPATH =.*"+"const GROUPPATH = \`$(GROUPS_BASE)\`"+g likwidMetric.go - -cleanup: - rm -rf $(BUILD_FOLDER) - -clean: cleanup +clean: rm -rf likwid .PHONY: clean diff --git a/collectors/cpufreqCpuinfoMetric.go b/collectors/cpufreqCpuinfoMetric.go index 44a3b0c..6c3de7a 100644 --- a/collectors/cpufreqCpuinfoMetric.go +++ b/collectors/cpufreqCpuinfoMetric.go @@ -57,7 +57,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { const cpuInfoFile = "/proc/cpuinfo" file, err := os.Open(cpuInfoFile) if err != nil { - return fmt.Errorf("Failed to open file '%s': %v", cpuInfoFile, err) + return fmt.Errorf("failed to open file '%s': %v", cpuInfoFile, err) } defer file.Close() @@ -106,14 +106,14 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { topology.coreID = coreID topology.coreID_int, err = strconv.ParseInt(coreID, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert coreID '%s' to int64: %v", coreID, err) + return fmt.Errorf("unable to convert coreID '%s' to int64: %v", coreID, err) } // Physical package ID topology.physicalPackageID = physicalPackageID topology.physicalPackageID_int, err = strconv.ParseInt(physicalPackageID, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert physicalPackageID '%s' to int64: %v", physicalPackageID, err) + return fmt.Errorf("unable to convert physicalPackageID '%s' to int64: %v", physicalPackageID, err) } // increase maximun socket / package ID, when required diff --git a/collectors/cpufreqMetric.go b/collectors/cpufreqMetric.go index 5146baa..0bf6d4c 100644 --- a/collectors/cpufreqMetric.go +++ b/collectors/cpufreqMetric.go @@ -70,10 +70,10 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { globPattern := filepath.Join(baseDir, "cpu[0-9]*") cpuDirs, err := filepath.Glob(globPattern) if err != nil { - return fmt.Errorf("Unable to glob files with pattern '%s': %v", globPattern, err) + return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err) } if cpuDirs == nil { - return fmt.Errorf("Unable to find any files with pattern '%s'", globPattern) + return fmt.Errorf("unable to find any files with pattern '%s'", globPattern) } // Initialize CPU topology @@ -82,38 +82,38 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { processor := strings.TrimPrefix(cpuDir, "/sys/devices/system/cpu/cpu") processor_int, err := strconv.ParseInt(processor, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert cpuID '%s' to int64: %v", processor, err) + return fmt.Errorf("unable to convert cpuID '%s' to int64: %v", processor, err) } // Read package ID physicalPackageIDFile := filepath.Join(cpuDir, "topology", "physical_package_id") line, err := ioutil.ReadFile(physicalPackageIDFile) if err != nil { - return fmt.Errorf("Unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err) + return fmt.Errorf("unable to read physical package ID from file '%s': %v", physicalPackageIDFile, err) } physicalPackageID := strings.TrimSpace(string(line)) physicalPackageID_int, err := strconv.ParseInt(physicalPackageID, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert packageID '%s' to int64: %v", physicalPackageID, err) + return fmt.Errorf("unable to convert packageID '%s' to int64: %v", physicalPackageID, err) } // Read core ID coreIDFile := filepath.Join(cpuDir, "topology", "core_id") line, err = ioutil.ReadFile(coreIDFile) if err != nil { - return fmt.Errorf("Unable to read core ID from file '%s': %v", coreIDFile, err) + return fmt.Errorf("unable to read core ID from file '%s': %v", coreIDFile, err) } coreID := strings.TrimSpace(string(line)) coreID_int, err := strconv.ParseInt(coreID, 10, 64) if err != nil { - return fmt.Errorf("Unable to convert coreID '%s' to int64: %v", coreID, err) + return fmt.Errorf("unable to convert coreID '%s' to int64: %v", coreID, err) } // Check access to current frequency file scalingCurFreqFile := filepath.Join(cpuDir, "cpufreq", "scaling_cur_freq") err = unix.Access(scalingCurFreqFile, unix.R_OK) if err != nil { - return fmt.Errorf("Unable to access file '%s': %v", scalingCurFreqFile, err) + return fmt.Errorf("unable to access file '%s': %v", scalingCurFreqFile, err) } t := &m.topology[processor_int] diff --git a/collectors/cpustatMetric.go b/collectors/cpustatMetric.go index 28ae002..556aad4 100644 --- a/collectors/cpustatMetric.go +++ b/collectors/cpustatMetric.go @@ -21,11 +21,10 @@ type CpustatCollectorConfig struct { type CpustatCollector struct { metricCollector - config CpustatCollectorConfig - matches map[string]int - cputags map[string]map[string]string - nodetags map[string]string - num_cpus_metric lp.CCMetric + config CpustatCollectorConfig + matches map[string]int + cputags map[string]map[string]string + nodetags map[string]string } func (m *CpustatCollector) Init(config json.RawMessage) error { diff --git a/collectors/customCmdMetric.go b/collectors/customCmdMetric.go index e978c49..ec2109b 100644 --- a/collectors/customCmdMetric.go +++ b/collectors/customCmdMetric.go @@ -61,7 +61,7 @@ func (m *CustomCmdCollector) Init(config json.RawMessage) error { } } if len(m.files) == 0 && len(m.commands) == 0 { - return errors.New("No metrics to collect") + return errors.New("no metrics to collect") } m.handler = influx.NewMetricHandler() m.parser = influx.NewParser(m.handler) diff --git a/collectors/gpfsMetric.go b/collectors/gpfsMetric.go index adbc7fb..26fc723 100644 --- a/collectors/gpfsMetric.go +++ b/collectors/gpfsMetric.go @@ -17,14 +17,24 @@ import ( lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) +const DEFAULT_GPFS_CMD = "mmpmon" + +type GpfsCollectorLastState struct { + bytesRead int64 + bytesWritten int64 +} + type GpfsCollector struct { metricCollector tags map[string]string config struct { Mmpmon string `json:"mmpmon_path,omitempty"` ExcludeFilesystem []string `json:"exclude_filesystem,omitempty"` + SendBandwidths bool `json:"send_bandwidths"` } - skipFS map[string]struct{} + skipFS map[string]struct{} + lastTimestamp time.Time // Store time stamp of last tick to derive bandwidths + lastState map[string]GpfsCollectorLastState } func (m *GpfsCollector) Init(config json.RawMessage) error { @@ -38,7 +48,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { m.setup() // Set default mmpmon binary - m.config.Mmpmon = "/usr/lpp/mmfs/bin/mmpmon" + m.config.Mmpmon = DEFAULT_GPFS_CMD // Read JSON configuration if len(config) > 0 { @@ -64,17 +74,18 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { // GPFS / IBM Spectrum Scale file system statistics can only be queried by user root user, err := user.Current() if err != nil { - return fmt.Errorf("Failed to get current user: %v", err) + return fmt.Errorf("failed to get current user: %v", err) } if user.Uid != "0" { return fmt.Errorf("GPFS file system statistics can only be queried by user root") } // Check if mmpmon is in executable search path - _, err = exec.LookPath(m.config.Mmpmon) + p, err := exec.LookPath(m.config.Mmpmon) if err != nil { - return fmt.Errorf("Failed to find mmpmon binary '%s': %v", m.config.Mmpmon, err) + return fmt.Errorf("failed to find mmpmon binary '%s': %v", m.config.Mmpmon, err) } + m.config.Mmpmon = p m.init = true return nil @@ -86,6 +97,13 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { return } + // Current time stamp + now := time.Now() + // time difference to last time stamp + timeDiff := now.Sub(m.lastTimestamp).Seconds() + // Save current timestamp + m.lastTimestamp = now + // mmpmon: // -p: generate output that can be parsed // -s: suppress the prompt on input @@ -145,6 +163,12 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { } m.tags["filesystem"] = filesystem + if _, ok := m.lastState[filesystem]; !ok { + m.lastState[filesystem] = GpfsCollectorLastState{ + bytesRead: -1, + bytesWritten: -1, + } + } // return code rc, err := strconv.Atoi(key_value["_rc_"]) @@ -188,6 +212,14 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if y, err := lp.New("gpfs_bytes_read", m.tags, m.meta, map[string]interface{}{"value": bytesRead}, timestamp); err == nil { output <- y } + if m.config.SendBandwidths { + if lastBytesRead := m.lastState[filesystem].bytesRead; lastBytesRead >= 0 { + bwRead := float64(bytesRead-lastBytesRead) / timeDiff + if y, err := lp.New("gpfs_bw_read", m.tags, m.meta, map[string]interface{}{"value": bwRead}, timestamp); err == nil { + output <- y + } + } + } // bytes written bytesWritten, err := strconv.ParseInt(key_value["_bw_"], 10, 64) @@ -200,6 +232,21 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { if y, err := lp.New("gpfs_bytes_written", m.tags, m.meta, map[string]interface{}{"value": bytesWritten}, timestamp); err == nil { output <- y } + if m.config.SendBandwidths { + if lastBytesWritten := m.lastState[filesystem].bytesRead; lastBytesWritten >= 0 { + bwWrite := float64(bytesWritten-lastBytesWritten) / timeDiff + if y, err := lp.New("gpfs_bw_write", m.tags, m.meta, map[string]interface{}{"value": bwWrite}, timestamp); err == nil { + output <- y + } + } + } + + if m.config.SendBandwidths { + m.lastState[filesystem] = GpfsCollectorLastState{ + bytesRead: bytesRead, + bytesWritten: bytesWritten, + } + } // number of opens numOpens, err := strconv.ParseInt(key_value["_oc_"], 10, 64) diff --git a/collectors/gpfsMetric.md b/collectors/gpfsMetric.md index 4a6a058..30a5a40 100644 --- a/collectors/gpfsMetric.md +++ b/collectors/gpfsMetric.md @@ -5,7 +5,8 @@ "mmpmon_path": "/path/to/mmpmon", "exclude_filesystem": [ "fs1" - ] + ], + "send_bandwidths" : true } ``` @@ -16,15 +17,18 @@ The reported filesystems can be filtered with the `exclude_filesystem` option in the configuration. The path to the `mmpmon` command can be configured with the `mmpmon_path` option -in the configuration. +in the configuration. If nothing is set, the collector searches in `$PATH` for `mmpmon`. + Metrics: -* `bytes_read` +* `gpfs_bytes_read` * `gpfs_bytes_written` * `gpfs_num_opens` * `gpfs_num_closes` * `gpfs_num_reads` * `gpfs_num_readdirs` * `gpfs_num_inode_updates` +* `gpfs_bw_read` (if `send_bandwidths == true`) +* `gpfs_bw_write` (if `send_bandwidths == true`) The collector adds a `filesystem` tag to all metrics diff --git a/collectors/infinibandMetric.go b/collectors/infinibandMetric.go index ac79e0a..5be095d 100644 --- a/collectors/infinibandMetric.go +++ b/collectors/infinibandMetric.go @@ -16,7 +16,7 @@ import ( "time" ) -const IB_BASEPATH = `/sys/class/infiniband/` +const IB_BASEPATH = "/sys/class/infiniband/" type InfinibandCollectorInfo struct { LID string // IB local Identifier (LID) @@ -24,14 +24,18 @@ type InfinibandCollectorInfo struct { port string // IB device port portCounterFiles map[string]string // mapping counter name -> sysfs file tagSet map[string]string // corresponding tag list + lastState map[string]int64 // State from last measurement } type InfinibandCollector struct { metricCollector config struct { - ExcludeDevices []string `json:"exclude_devices,omitempty"` // IB device to exclude e.g. mlx5_0 + ExcludeDevices []string `json:"exclude_devices,omitempty"` // IB device to exclude e.g. mlx5_0 + SendAbsoluteValues bool `json:"send_abs_values"` // Send absolut values as read from sys filesystem + SendDerivedValues bool `json:"send_derived_values"` // Send derived values e.g. rates } - info []*InfinibandCollectorInfo + info []*InfinibandCollectorInfo + lastTimestamp time.Time // Store time stamp of last tick to derive bandwidths } // Init initializes the Infiniband collector by walking through files below IB_BASEPATH @@ -49,6 +53,11 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error { "source": m.name, "group": "Network", } + + // Set default configuration, + m.config.SendAbsoluteValues = true + m.config.SendDerivedValues = false + // Read configuration file, allow overwriting default config if len(config) > 0 { err = json.Unmarshal(config, &m.config) if err != nil { @@ -60,10 +69,10 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error { globPattern := filepath.Join(IB_BASEPATH, "*", "ports", "*") ibDirs, err := filepath.Glob(globPattern) if err != nil { - return fmt.Errorf("Unable to glob files with pattern %s: %v", globPattern, err) + return fmt.Errorf("unable to glob files with pattern %s: %v", globPattern, err) } if ibDirs == nil { - return fmt.Errorf("Unable to find any directories with pattern %s", globPattern) + return fmt.Errorf("unable to find any directories with pattern %s", globPattern) } for _, path := range ibDirs { @@ -106,10 +115,16 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error { for _, counterFile := range portCounterFiles { err := unix.Access(counterFile, unix.R_OK) if err != nil { - return fmt.Errorf("Unable to access %s: %v", counterFile, err) + return fmt.Errorf("unable to access %s: %v", counterFile, err) } } + // Initialize last state + lastState := make(map[string]int64) + for counter := range portCounterFiles { + lastState[counter] = -1 + } + m.info = append(m.info, &InfinibandCollectorInfo{ LID: LID, @@ -122,11 +137,12 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error { "port": port, "lid": LID, }, + lastState: lastState, }) } if len(m.info) == 0 { - return fmt.Errorf("Found no IB devices") + return fmt.Errorf("found no IB devices") } m.init = true @@ -141,9 +157,17 @@ func (m *InfinibandCollector) Read(interval time.Duration, output chan lp.CCMetr return } + // Current time stamp now := time.Now() + // time difference to last time stamp + timeDiff := now.Sub(m.lastTimestamp).Seconds() + // Save current timestamp + m.lastTimestamp = now + for _, info := range m.info { for counterName, counterFile := range info.portCounterFiles { + + // Read counter file line, err := ioutil.ReadFile(counterFile) if err != nil { cclog.ComponentError( @@ -152,6 +176,8 @@ func (m *InfinibandCollector) Read(interval time.Duration, output chan lp.CCMetr continue } data := strings.TrimSpace(string(line)) + + // convert counter to int64 v, err := strconv.ParseInt(data, 10, 64) if err != nil { cclog.ComponentError( @@ -159,8 +185,24 @@ func (m *InfinibandCollector) Read(interval time.Duration, output chan lp.CCMetr fmt.Sprintf("Read(): Failed to convert Infininiband metrice %s='%s' to int64: %v", counterName, data, err)) continue } - if y, err := lp.New(counterName, info.tagSet, m.meta, map[string]interface{}{"value": v}, now); err == nil { - output <- y + + // Send absolut values + if m.config.SendAbsoluteValues { + if y, err := lp.New(counterName, info.tagSet, m.meta, map[string]interface{}{"value": v}, now); err == nil { + output <- y + } + } + + // Send derived values + if m.config.SendDerivedValues { + if info.lastState[counterName] >= 0 { + rate := float64((v - info.lastState[counterName])) / timeDiff + if y, err := lp.New(counterName+"_bw", info.tagSet, m.meta, map[string]interface{}{"value": rate}, now); err == nil { + output <- y + } + } + // Save current state + info.lastState[counterName] = v } } diff --git a/collectors/infinibandMetric.md b/collectors/infinibandMetric.md index 579ed77..f129aad 100644 --- a/collectors/infinibandMetric.md +++ b/collectors/infinibandMetric.md @@ -5,7 +5,9 @@ "ibstat": { "exclude_devices": [ "mlx4" - ] + ], + "send_abs_values": true, + "send_derived_values": true } ``` @@ -22,5 +24,9 @@ Metrics: * `ib_xmit` * `ib_recv_pkts` * `ib_xmit_pkts` +* `ib_recv_bw` (if `send_derived_values == true`) +* `ib_xmit_bw` (if `send_derived_values == true`) +* `ib_recv_pkts_bw` (if `send_derived_values == true`) +* `ib_xmit_pkts_bw` (if `send_derived_values == true`) The collector adds a `device` tag to all metrics diff --git a/collectors/ipmiMetric.go b/collectors/ipmiMetric.go index e59f407..16b08ef 100644 --- a/collectors/ipmiMetric.go +++ b/collectors/ipmiMetric.go @@ -54,7 +54,7 @@ func (m *IpmiCollector) Init(config json.RawMessage) error { m.ipmisensors = p } if len(m.ipmitool) == 0 && len(m.ipmisensors) == 0 { - return errors.New("No IPMI reader found") + return errors.New("no IPMI reader found") } m.init = true return nil diff --git a/collectors/likwidMetric.go b/collectors/likwidMetric.go index 8ab42d5..e0b0d7e 100644 --- a/collectors/likwidMetric.go +++ b/collectors/likwidMetric.go @@ -15,7 +15,6 @@ import ( "io/ioutil" "math" "os" - "regexp" "strconv" "strings" "time" @@ -28,48 +27,6 @@ import ( "github.com/NVIDIA/go-nvml/pkg/dl" ) -type MetricScope string - -const ( - METRIC_SCOPE_HWTHREAD = iota - METRIC_SCOPE_CORE - METRIC_SCOPE_LLC - METRIC_SCOPE_NUMA - METRIC_SCOPE_DIE - METRIC_SCOPE_SOCKET - METRIC_SCOPE_NODE -) - -func (ms MetricScope) String() string { - return string(ms) -} - -func (ms MetricScope) Likwid() string { - LikwidDomains := map[string]string{ - "cpu": "", - "core": "", - "llc": "C", - "numadomain": "M", - "die": "D", - "socket": "S", - "node": "N", - } - return LikwidDomains[string(ms)] -} - -func (ms MetricScope) Granularity() int { - for i, g := range GetAllMetricScopes() { - if ms == g { - return i - } - } - return -1 -} - -func GetAllMetricScopes() []MetricScope { - return []MetricScope{"cpu" /*, "core", "llc", "numadomain", "die",*/, "socket", "node"} -} - const ( LIKWID_LIB_NAME = "liblikwid.so" LIKWID_LIB_DL_FLAGS = dl.RTLD_LAZY | dl.RTLD_GLOBAL @@ -77,18 +34,16 @@ const ( ) type LikwidCollectorMetricConfig struct { - Name string `json:"name"` // Name of the metric - Calc string `json:"calc"` // Calculation for the metric using - //Aggr string `json:"aggregation"` // if scope unequal to LIKWID metric scope, the values are combined (sum, min, max, mean or avg, median) - Scope MetricScope `json:"scope"` // scope for calculation. subscopes are aggregated using the 'aggregation' function - Publish bool `json:"publish"` - granulatity MetricScope + Name string `json:"name"` // Name of the metric + Calc string `json:"calc"` // Calculation for the metric using + Type string `json:"type"` // Metric type (aka node, socket, cpu, ...) + Publish bool `json:"publish"` + Unit string `json:"unit"` // Unit of metric if any } type LikwidCollectorEventsetConfig struct { - Events map[string]string `json:"events"` - granulatity map[string]MetricScope - Metrics []LikwidCollectorMetricConfig `json:"metrics"` + Events map[string]string `json:"events"` + Metrics []LikwidCollectorMetricConfig `json:"metrics"` } type LikwidCollectorConfig struct { @@ -98,28 +53,28 @@ type LikwidCollectorConfig struct { InvalidToZero bool `json:"invalid_to_zero,omitempty"` AccessMode string `json:"access_mode,omitempty"` DaemonPath string `json:"accessdaemon_path,omitempty"` + LibraryPath string `json:"liblikwid_path,omitempty"` } type LikwidCollector struct { metricCollector - cpulist []C.int - cpu2tid map[int]int - sock2tid map[int]int - scopeRespTids map[MetricScope]map[int]int - metrics map[C.int]map[string]int - groups []C.int - config LikwidCollectorConfig - results map[int]map[int]map[string]interface{} - mresults map[int]map[int]map[string]float64 - gmresults map[int]map[string]float64 - basefreq float64 - running bool + cpulist []C.int + cpu2tid map[int]int + sock2tid map[int]int + metrics map[C.int]map[string]int + groups []C.int + config LikwidCollectorConfig + results map[int]map[int]map[string]interface{} + mresults map[int]map[int]map[string]float64 + gmresults map[int]map[string]float64 + basefreq float64 + running bool } type LikwidMetric struct { name string search string - scope MetricScope + scope string group_idx int } @@ -131,152 +86,43 @@ func eventsToEventStr(events map[string]string) string { return strings.Join(elist, ",") } -func getGranularity(counter, event string) MetricScope { - if strings.HasPrefix(counter, "PMC") || strings.HasPrefix(counter, "FIXC") { - return "cpu" - } else if strings.Contains(counter, "BOX") || strings.Contains(counter, "DEV") { - return "socket" - } else if strings.HasPrefix(counter, "PWR") { - if event == "RAPL_CORE_ENERGY" { - return "cpu" - } else { - return "socket" - } - } - return "unknown" -} - func getBaseFreq() float64 { var freq float64 = math.NaN() C.power_init(0) info := C.get_powerInfo() if float64(info.baseFrequency) != 0 { - freq = float64(info.baseFrequency) * 1e3 + freq = float64(info.baseFrequency) * 1e6 } else { buffer, err := ioutil.ReadFile("/sys/devices/system/cpu/cpu0/cpufreq/bios_limit") if err == nil { data := strings.Replace(string(buffer), "\n", "", -1) x, err := strconv.ParseInt(data, 0, 64) if err == nil { - freq = float64(x) * 1e3 + freq = float64(x) * 1e6 } } } return freq } -func (m *LikwidCollector) initGranularity() { - splitRegex := regexp.MustCompile("[+-/*()]") - for _, evset := range m.config.Eventsets { - evset.granulatity = make(map[string]MetricScope) - for counter, event := range evset.Events { - gran := getGranularity(counter, event) - if gran.Granularity() >= 0 { - evset.granulatity[counter] = gran - } - } - for i, metric := range evset.Metrics { - s := splitRegex.Split(metric.Calc, -1) - gran := MetricScope("cpu") - evset.Metrics[i].granulatity = gran - for _, x := range s { - if _, ok := evset.Events[x]; ok { - if evset.granulatity[x].Granularity() > gran.Granularity() { - gran = evset.granulatity[x] - } - } - } - evset.Metrics[i].granulatity = gran - } - } - for i, metric := range m.config.Metrics { - s := splitRegex.Split(metric.Calc, -1) - gran := MetricScope("cpu") - m.config.Metrics[i].granulatity = gran - for _, x := range s { - for _, evset := range m.config.Eventsets { - for _, m := range evset.Metrics { - if m.Name == x && m.granulatity.Granularity() > gran.Granularity() { - gran = m.granulatity - } - } - } - } - m.config.Metrics[i].granulatity = gran - } -} - -type TopoResolveFunc func(cpuid int) int - -func (m *LikwidCollector) getResponsiblities() map[MetricScope]map[int]int { - get_cpus := func(scope MetricScope) map[int]int { - var slist []int - var cpu C.int - var input func(index int) string - switch scope { - case "node": - slist = []int{0} - input = func(index int) string { return "N:0" } - case "socket": - input = func(index int) string { return fmt.Sprintf("%s%d:0", scope.Likwid(), index) } - slist = topo.SocketList() - // case "numadomain": - // input = func(index int) string { return fmt.Sprintf("%s%d:0", scope.Likwid(), index) } - // slist = topo.NumaNodeList() - // cclog.Debug(scope, " ", input(0), " ", slist) - // case "die": - // input = func(index int) string { return fmt.Sprintf("%s%d:0", scope.Likwid(), index) } - // slist = topo.DieList() - // case "llc": - // input = fmt.Sprintf("%s%d:0", scope.Likwid(), s) - // slist = topo.LLCacheList() - case "cpu": - input = func(index int) string { return fmt.Sprintf("%d", index) } - slist = topo.CpuList() - case "hwthread": - input = func(index int) string { return fmt.Sprintf("%d", index) } - slist = topo.CpuList() - } - outmap := make(map[int]int) - for _, s := range slist { - t := C.CString(input(s)) - clen := C.cpustr_to_cpulist(t, &cpu, 1) - if int(clen) == 1 { - outmap[s] = m.cpu2tid[int(cpu)] - } else { - cclog.Error(fmt.Sprintf("Cannot determine responsible CPU for %s", input(s))) - outmap[s] = -1 - } - C.free(unsafe.Pointer(t)) - } - return outmap - } - - scopes := GetAllMetricScopes() - complete := make(map[MetricScope]map[int]int) - for _, s := range scopes { - complete[s] = get_cpus(s) - } - return complete -} - func (m *LikwidCollector) Init(config json.RawMessage) error { var ret C.int m.name = "LikwidCollector" m.config.AccessMode = LIKWID_DEF_ACCESSMODE + m.config.LibraryPath = LIKWID_LIB_NAME if len(config) > 0 { err := json.Unmarshal(config, &m.config) if err != nil { return err } } - lib := dl.New(LIKWID_LIB_NAME, LIKWID_LIB_DL_FLAGS) + lib := dl.New(m.config.LibraryPath, LIKWID_LIB_DL_FLAGS) if lib == nil { - return fmt.Errorf("error instantiating DynamicLibrary for %s", LIKWID_LIB_NAME) + return fmt.Errorf("error instantiating DynamicLibrary for %s", m.config.LibraryPath) } err := lib.Open() if err != nil { - return fmt.Errorf("error opening %s: %v", LIKWID_LIB_NAME, err) + return fmt.Errorf("error opening %s: %v", m.config.LibraryPath, err) } if m.config.ForceOverwrite { @@ -306,10 +152,6 @@ func (m *LikwidCollector) Init(config json.RawMessage) error { return err } - // Determine which counter works at which level. PMC*: cpu, *BOX*: socket, ... - m.initGranularity() - // Generate map for MetricScope -> scope_id (like socket id) -> responsible id (offset in cpulist) - m.scopeRespTids = m.getResponsiblities() switch m.config.AccessMode { case "direct": C.HPMmode(0) @@ -336,29 +178,36 @@ func (m *LikwidCollector) Init(config json.RawMessage) error { globalParams["inverseClock"] = float64(1.0) // While adding the events, we test the metrics whether they can be computed at all for i, evset := range m.config.Eventsets { - estr := eventsToEventStr(evset.Events) - // Generate parameter list for the metric computing test - params := make(map[string]interface{}) - params["time"] = float64(1.0) - params["inverseClock"] = float64(1.0) - for counter := range evset.Events { - params[counter] = float64(1.0) - } - for _, metric := range evset.Metrics { - // Try to evaluate the metric - _, err := agg.EvalFloat64Condition(metric.Calc, params) - if err != nil { - cclog.ComponentError(m.name, "Calculation for metric", metric.Name, "failed:", err.Error()) - continue + var gid C.int + var cstr *C.char + if len(evset.Events) > 0 { + estr := eventsToEventStr(evset.Events) + // Generate parameter list for the metric computing test + params := make(map[string]interface{}) + params["time"] = float64(1.0) + params["inverseClock"] = float64(1.0) + for counter := range evset.Events { + params[counter] = float64(1.0) } - // If the metric is not in the parameter list for the global metrics, add it - if _, ok := globalParams[metric.Name]; !ok { - globalParams[metric.Name] = float64(1.0) + for _, metric := range evset.Metrics { + // Try to evaluate the metric + _, err := agg.EvalFloat64Condition(metric.Calc, params) + if err != nil { + cclog.ComponentError(m.name, "Calculation for metric", metric.Name, "failed:", err.Error()) + continue + } + // If the metric is not in the parameter list for the global metrics, add it + if _, ok := globalParams[metric.Name]; !ok { + globalParams[metric.Name] = float64(1.0) + } } + // Now we add the list of events to likwid + cstr = C.CString(estr) + gid = C.perfmon_addEventSet(cstr) + } else { + cclog.ComponentError(m.name, "Invalid Likwid eventset config, no events given") + continue } - // Now we add the list of events to likwid - cstr := C.CString(estr) - gid := C.perfmon_addEventSet(cstr) if gid >= 0 { m.groups = append(m.groups, gid) } @@ -434,15 +283,9 @@ func (m *LikwidCollector) calcEventsetMetrics(group int, interval time.Duration, // Go over events and get the results for eidx = 0; int(eidx) < len(evset.Events); eidx++ { ctr := C.perfmon_getCounterName(gid, eidx) - ev := C.perfmon_getEventName(gid, eidx) gctr := C.GoString(ctr) - gev := C.GoString(ev) - // MetricScope for the counter (and if needed the event) - scope := getGranularity(gctr, gev) - // Get the map scope-id -> tids - // This way we read less counters like only the responsible hardware thread for a socket - scopemap := m.scopeRespTids[scope] - for _, tid := range scopemap { + + for _, tid := range m.cpu2tid { if tid >= 0 { m.results[group][tid]["time"] = interval.Seconds() m.results[group][tid]["inverseClock"] = invClock @@ -456,7 +299,10 @@ func (m *LikwidCollector) calcEventsetMetrics(group int, interval time.Duration, for _, metric := range evset.Metrics { // The metric scope is determined in the Init() function // Get the map scope-id -> tids - scopemap := m.scopeRespTids[metric.Scope] + scopemap := m.cpu2tid + if metric.Type == "socket" { + scopemap = m.sock2tid + } for domain, tid := range scopemap { if tid >= 0 { value, err := agg.EvalFloat64Condition(metric.Calc, m.results[group][tid]) @@ -474,13 +320,15 @@ func (m *LikwidCollector) calcEventsetMetrics(group int, interval time.Duration, // Now we have the result, send it with the proper tags if !math.IsNaN(value) { if metric.Publish { - tags := map[string]string{"type": metric.Scope.String()} - if metric.Scope != "node" { - tags["type-id"] = fmt.Sprintf("%d", domain) - } fields := map[string]interface{}{"value": value} - y, err := lp.New(metric.Name, tags, m.meta, fields, time.Now()) + y, err := lp.New(metric.Name, map[string]string{"type": metric.Type}, m.meta, fields, time.Now()) if err == nil { + if metric.Type != "node" { + y.AddTag("type-id", fmt.Sprintf("%d", domain)) + } + if len(metric.Unit) > 0 { + y.AddMeta("unit", metric.Unit) + } output <- y } } @@ -495,7 +343,10 @@ func (m *LikwidCollector) calcEventsetMetrics(group int, interval time.Duration, // Go over the global metrics, derive the value out of the event sets' metric values and send it func (m *LikwidCollector) calcGlobalMetrics(interval time.Duration, output chan lp.CCMetric) error { for _, metric := range m.config.Metrics { - scopemap := m.scopeRespTids[metric.Scope] + scopemap := m.cpu2tid + if metric.Type == "socket" { + scopemap = m.sock2tid + } for domain, tid := range scopemap { if tid >= 0 { // Here we generate parameter list @@ -521,13 +372,16 @@ func (m *LikwidCollector) calcGlobalMetrics(interval time.Duration, output chan // Now we have the result, send it with the proper tags if !math.IsNaN(value) { if metric.Publish { - tags := map[string]string{"type": metric.Scope.String()} - if metric.Scope != "node" { - tags["type-id"] = fmt.Sprintf("%d", domain) - } + tags := map[string]string{"type": metric.Type} fields := map[string]interface{}{"value": value} y, err := lp.New(metric.Name, tags, m.meta, fields, time.Now()) if err == nil { + if metric.Type != "node" { + y.AddTag("type-id", fmt.Sprintf("%d", domain)) + } + if len(metric.Unit) > 0 { + y.AddMeta("unit", metric.Unit) + } output <- y } } diff --git a/collectors/likwidMetric.md b/collectors/likwidMetric.md index 1aa4242..fe28857 100644 --- a/collectors/likwidMetric.md +++ b/collectors/likwidMetric.md @@ -4,14 +4,17 @@ The `likwid` collector is probably the most complicated collector. The LIKWID library is included as static library with *direct* access mode. The *direct* access mode is suitable if the daemon is executed by a root user. The static library does not contain the performance groups, so all information needs to be provided in the configuration. The `likwid` configuration consists of two parts, the "eventsets" and "globalmetrics": -- An event set list itself has two parts, the "events" and a set of derivable "metrics". Each of the "events" is a counter:event pair in LIKWID's syntax. The "metrics" are a list of formulas to derive the metric value from the measurements of the "events". Each metric has a name, the formula, a scope and a publish flag. Counter names can be used like variables in the formulas, so `PMC0+PMC1` sums the measurements for the both events configured in the counters `PMC0` and `PMC1`. The scope tells the Collector whether it is a metric for each hardware thread (`cpu`) or each CPU socket (`socket`). The last one is the publishing flag. It tells the collector whether a metric should be sent to the router. -- The global metrics are metrics which require data from all event set measurements to be derived. The inputs are the metrics in the event sets. Similar to the metrics in the event sets, the global metrics are defined by a name, a formula, a scope and a publish flag. See event set metrics for details. The only difference is that there is no access to the raw event measurements anymore but only to the metrics. So, the idea is to derive a metric in the "eventsets" section and reuse it in the "globalmetrics" part. If you need a metric only for deriving the global metrics, disable forwarding of the event set metrics. **Be aware** that the combination might be misleading because the "behavior" of a metric changes over time and the multiple measurements might count different computing phases. +- An event set list itself has two parts, the "events" and a set of derivable "metrics". Each of the "events" is a counter:event pair in LIKWID's syntax. The "metrics" are a list of formulas to derive the metric value from the measurements of the "events". Each metric has a name, the formula, a scope and a publish flag. Counter names can be used like variables in the formulas, so `PMC0+PMC1` sums the measurements for the both events configured in the counters `PMC0` and `PMC1`. The scope tells the Collector whether it is a metric for each hardware thread (`cpu`) or each CPU socket (`socket`). You may specify a unit for the metric with `unit`. The last one is the publishing flag. It tells the collector whether a metric should be sent to the router. +- The global metrics are metrics which require data from all event set measurements to be derived. The inputs are the metrics in the event sets. Similar to the metrics in the event sets, the global metrics are defined by a name, a formula, a scope and a publish flag. See event set metrics for details. The only difference is that there is no access to the raw event measurements anymore but only to the metrics. So, the idea is to derive a metric in the "eventsets" section and reuse it in the "globalmetrics" part. If you need a metric only for deriving the global metrics, disable forwarding of the event set metrics (`publish=false`). **Be aware** that the combination might be misleading because the "behavior" of a metric changes over time and the multiple measurements might count different computing phases. Similar to the metrics in the eventset, you can specify a metric unit with the `unit` field. Additional options: - `access_mode` : Method to use for hardware performance monitoring (`direct` access as root user, `accessdaemon` for the daemon mode) - `accessdaemon_path`: Folder with the access daemon `likwid-accessD`, commonly `$LIKWID_INSTALL_LOC/sbin` - `force_overwrite`: Same as setting `LIKWID_FORCE=1`. In case counters are already in-use, LIKWID overwrites their configuration to do its measurements - `invalid_to_zero`: In some cases, the calculations result in `NaN` or `Inf`. With this option, all `NaN` and `Inf` values are replaces with `0.0`. +- `access_mode`: Specify LIKWID access mode: `direct` for direct register access as root user or `accessdaemon` +- `accessdaemon_path`: Folder of the accessDaemon `likwid-accessD` +- `liblikwid_path`: Location of `liblikwid.so` ### Available metric scopes @@ -54,7 +57,8 @@ $ scripts/likwid_perfgroup_to_cc_config.py ICX MEM_DP "calc": "time", "name": "Runtime (RDTSC) [s]", "publish": true, - "scope": "hwthread" + "unit": "seconds" + "scope": "cpu" }, { "..." : "..." @@ -104,25 +108,28 @@ $ chwon $CCUSER /var/run/likwid.lock { "name": "ipc", "calc": "PMC0/PMC1", - "scope": "cpu", + "type": "cpu", "publish": true }, { "name": "flops_any", "calc": "0.000001*PMC2/time", - "scope": "cpu", + "unit": "MFlops/s", + "type": "cpu", "publish": true }, { - "name": "clock_mhz", + "name": "clock", "calc": "0.000001*(FIXC1/FIXC2)/inverseClock", - "scope": "cpu", + "type": "cpu", + "unit": "MHz", "publish": true }, { "name": "mem1", "calc": "0.000001*(DFC0+DFC1+DFC2+DFC3)*64.0/time", - "scope": "socket", + "unit": "Mbyte/s", + "type": "socket", "publish": false } ] @@ -140,19 +147,22 @@ $ chwon $CCUSER /var/run/likwid.lock { "name": "pwr_core", "calc": "PWR0/time", - "scope": "socket", + "unit": "Watt" + "type": "socket", "publish": true }, { "name": "pwr_pkg", "calc": "PWR1/time", - "scope": "socket", + "type": "socket", + "unit": "Watt" "publish": true }, { "name": "mem2", "calc": "0.000001*(DFC0+DFC1+DFC2+DFC3)*64.0/time", - "scope": "socket", + "unit": "Mbyte/s", + "type": "socket", "publish": false } ] @@ -162,7 +172,8 @@ $ chwon $CCUSER /var/run/likwid.lock { "name": "mem_bw", "calc": "mem1+mem2", - "scope": "socket", + "type": "socket", + "unit": "Mbyte/s", "publish": true } ] @@ -198,3 +209,4 @@ IPC PMC0/PMC1 -> { -> ] ``` +The script `scripts/likwid_perfgroup_to_cc_config.py` might help you. \ No newline at end of file diff --git a/collectors/lustreMetric.go b/collectors/lustreMetric.go index 66fd3fd..d5a96e4 100644 --- a/collectors/lustreMetric.go +++ b/collectors/lustreMetric.go @@ -19,20 +19,31 @@ const LCTL_CMD = `lctl` const LCTL_OPTION = `get_param` type LustreCollectorConfig struct { - LCtlCommand string `json:"lctl_command"` - ExcludeMetrics []string `json:"exclude_metrics"` - SendAllMetrics bool `json:"send_all_metrics"` - Sudo bool `json:"use_sudo"` + LCtlCommand string `json:"lctl_command,omitempty"` + ExcludeMetrics []string `json:"exclude_metrics,omitempty"` + Sudo bool `json:"use_sudo,omitempty"` + SendAbsoluteValues bool `json:"send_abs_values,omitempty"` + SendDerivedValues bool `json:"send_derived_values,omitempty"` + SendDiffValues bool `json:"send_diff_values,omitempty"` +} + +type LustreMetricDefinition struct { + name string + lineprefix string + lineoffset int + unit string + calc string } type LustreCollector struct { metricCollector - tags map[string]string - matches map[string]map[string]int - stats map[string]map[string]int64 - config LustreCollectorConfig - lctl string - sudoCmd string + tags map[string]string + config LustreCollectorConfig + lctl string + sudoCmd string + lastTimestamp time.Time // Store time stamp of last tick to derive bandwidths + definitions []LustreMetricDefinition // Combined list without excluded metrics + stats map[string]map[string]int64 // Data for last value per device and metric } func (m *LustreCollector) getDeviceDataCommand(device string) []string { @@ -75,6 +86,16 @@ func (m *LustreCollector) getDevices() []string { return devices } +func getMetricData(lines []string, prefix string, offset int) (int64, error) { + for _, line := range lines { + if strings.HasPrefix(line, prefix) { + lf := strings.Fields(line) + return strconv.ParseInt(lf[offset], 0, 64) + } + } + return 0, errors.New("no such line in data") +} + // //Version reading the stats data of a device from sysfs // func (m *LustreCollector) getDeviceDataSysfs(device string) []string { // llitedir := filepath.Join(LUSTRE_SYSFS, "llite") @@ -87,6 +108,183 @@ func (m *LustreCollector) getDevices() []string { // return strings.Split(string(buffer), "\n") // } +var LustreAbsMetrics = []LustreMetricDefinition{ + { + name: "lustre_read_requests", + lineprefix: "read_bytes", + lineoffset: 1, + unit: "requests", + calc: "none", + }, + { + name: "lustre_write_requests", + lineprefix: "write_bytes", + lineoffset: 1, + unit: "requests", + calc: "none", + }, + { + name: "lustre_read_bytes", + lineprefix: "read_bytes", + lineoffset: 6, + unit: "bytes", + calc: "none", + }, + { + name: "lustre_write_bytes", + lineprefix: "write_bytes", + lineoffset: 6, + unit: "bytes", + calc: "none", + }, + { + name: "lustre_open", + lineprefix: "open", + lineoffset: 1, + unit: "", + calc: "none", + }, + { + name: "lustre_close", + lineprefix: "close", + lineoffset: 1, + unit: "", + calc: "none", + }, + { + name: "lustre_setattr", + lineprefix: "setattr", + lineoffset: 1, + unit: "", + calc: "none", + }, + { + name: "lustre_getattr", + lineprefix: "getattr", + lineoffset: 1, + unit: "", + calc: "none", + }, + { + name: "lustre_statfs", + lineprefix: "statfs", + lineoffset: 1, + unit: "", + calc: "none", + }, + { + name: "lustre_inode_permission", + lineprefix: "inode_permission", + lineoffset: 1, + unit: "", + calc: "none", + }, +} + +var LustreDiffMetrics = []LustreMetricDefinition{ + { + name: "lustre_read_requests_diff", + lineprefix: "read_bytes", + lineoffset: 1, + unit: "requests", + calc: "difference", + }, + { + name: "lustre_write_requests_diff", + lineprefix: "write_bytes", + lineoffset: 1, + unit: "requests", + calc: "difference", + }, + { + name: "lustre_read_bytes_diff", + lineprefix: "read_bytes", + lineoffset: 6, + unit: "bytes", + calc: "difference", + }, + { + name: "lustre_write_bytes_diff", + lineprefix: "write_bytes", + lineoffset: 6, + unit: "bytes", + calc: "difference", + }, + { + name: "lustre_open_diff", + lineprefix: "open", + lineoffset: 1, + unit: "", + calc: "difference", + }, + { + name: "lustre_close_diff", + lineprefix: "close", + lineoffset: 1, + unit: "", + calc: "difference", + }, + { + name: "lustre_setattr_diff", + lineprefix: "setattr", + lineoffset: 1, + unit: "", + calc: "difference", + }, + { + name: "lustre_getattr_diff", + lineprefix: "getattr", + lineoffset: 1, + unit: "", + calc: "difference", + }, + { + name: "lustre_statfs_diff", + lineprefix: "statfs", + lineoffset: 1, + unit: "", + calc: "difference", + }, + { + name: "lustre_inode_permission_diff", + lineprefix: "inode_permission", + lineoffset: 1, + unit: "", + calc: "difference", + }, +} + +var LustreDeriveMetrics = []LustreMetricDefinition{ + { + name: "lustre_read_requests_rate", + lineprefix: "read_bytes", + lineoffset: 1, + unit: "requests/sec", + calc: "derivative", + }, + { + name: "lustre_write_requests_rate", + lineprefix: "write_bytes", + lineoffset: 1, + unit: "requests/sec", + calc: "derivative", + }, + { + name: "lustre_read_bw", + lineprefix: "read_bytes", + lineoffset: 6, + unit: "bytes/sec", + calc: "derivative", + }, + { + name: "lustre_write_bw", + lineprefix: "write_bytes", + lineoffset: 6, + unit: "bytes/sec", + calc: "derivative", + }, +} + func (m *LustreCollector) Init(config json.RawMessage) error { var err error m.name = "LustreCollector" @@ -99,17 +297,9 @@ func (m *LustreCollector) Init(config json.RawMessage) error { m.setup() m.tags = map[string]string{"type": "node"} m.meta = map[string]string{"source": m.name, "group": "Lustre"} - defmatches := map[string]map[string]int{ - "read_bytes": {"lustre_read_bytes": 6, "lustre_read_requests": 1}, - "write_bytes": {"lustre_write_bytes": 6, "lustre_write_requests": 1}, - "open": {"lustre_open": 1}, - "close": {"lustre_close": 1}, - "setattr": {"lustre_setattr": 1}, - "getattr": {"lustre_getattr": 1}, - "statfs": {"lustre_statfs": 1}, - "inode_permission": {"lustre_inode_permission": 1}} // Lustre file system statistics can only be queried by user root + // or with password-less sudo if !m.config.Sudo { user, err := user.Current() if err != nil { @@ -120,23 +310,15 @@ func (m *LustreCollector) Init(config json.RawMessage) error { cclog.ComponentError(m.name, "Lustre file system statistics can only be queried by user root") return err } + } else { + p, err := exec.LookPath("sudo") + if err != nil { + cclog.ComponentError(m.name, "Cannot find 'sudo'") + return err + } + m.sudoCmd = p } - m.matches = make(map[string]map[string]int) - for lineprefix, names := range defmatches { - for metricname, offset := range names { - _, skip := stringArrayContains(m.config.ExcludeMetrics, metricname) - if skip { - continue - } - if _, prefixExist := m.matches[lineprefix]; !prefixExist { - m.matches[lineprefix] = make(map[string]int) - } - if _, metricExist := m.matches[lineprefix][metricname]; !metricExist { - m.matches[lineprefix][metricname] = offset - } - } - } p, err := exec.LookPath(m.config.LCtlCommand) if err != nil { p, err = exec.LookPath(LCTL_CMD) @@ -145,26 +327,51 @@ func (m *LustreCollector) Init(config json.RawMessage) error { } } m.lctl = p - if m.config.Sudo { - p, err := exec.LookPath("sudo") - if err != nil { - m.sudoCmd = p + + m.definitions = []LustreMetricDefinition{} + if m.config.SendAbsoluteValues { + for _, def := range LustreAbsMetrics { + if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + m.definitions = append(m.definitions, def) + } } } + if m.config.SendDiffValues { + for _, def := range LustreDiffMetrics { + if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + m.definitions = append(m.definitions, def) + } + } + } + if m.config.SendDerivedValues { + for _, def := range LustreDeriveMetrics { + if _, skip := stringArrayContains(m.config.ExcludeMetrics, def.name); !skip { + m.definitions = append(m.definitions, def) + } + } + } + if len(m.definitions) == 0 { + return errors.New("no metrics to collect") + } devices := m.getDevices() if len(devices) == 0 { - return errors.New("no metrics to collect") + return errors.New("no Lustre devices found") } m.stats = make(map[string]map[string]int64) for _, d := range devices { m.stats[d] = make(map[string]int64) - for _, names := range m.matches { - for metricname := range names { - m.stats[d][metricname] = 0 + data := m.getDeviceDataCommand(d) + for _, def := range m.definitions { + x, err := getMetricData(data, def.lineprefix, def.lineoffset) + if err == nil { + m.stats[d][def.name] = x + } else { + m.stats[d][def.name] = 0 } } } + m.lastTimestamp = time.Now() m.init = true return nil } @@ -173,54 +380,49 @@ func (m *LustreCollector) Read(interval time.Duration, output chan lp.CCMetric) if !m.init { return } + now := time.Now() + tdiff := now.Sub(m.lastTimestamp) for device, devData := range m.stats { - stats := m.getDeviceDataCommand(device) - processed := []string{} - - for _, line := range stats { - lf := strings.Fields(line) - if len(lf) > 1 { - if fields, ok := m.matches[lf[0]]; ok { - for name, idx := range fields { - x, err := strconv.ParseInt(lf[idx], 0, 64) - if err != nil { - continue - } - value := x - devData[name] - devData[name] = x - if value < 0 { - value = 0 - } - y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": value}, time.Now()) - if err == nil { - y.AddTag("device", device) - if strings.Contains(name, "byte") { - y.AddMeta("unit", "Byte") - } - output <- y - if m.config.SendAllMetrics { - processed = append(processed, name) - } - } - } - } + data := m.getDeviceDataCommand(device) + for _, def := range m.definitions { + var use_x int64 + var err error + var y lp.CCMetric + x, err := getMetricData(data, def.lineprefix, def.lineoffset) + if err == nil { + use_x = x + } else { + use_x = devData[def.name] } - } - if m.config.SendAllMetrics { - for name := range devData { - if _, done := stringArrayContains(processed, name); !done { - y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": 0}, time.Now()) - if err == nil { - y.AddTag("device", device) - if strings.Contains(name, "byte") { - y.AddMeta("unit", "Byte") - } - output <- y - } + var value interface{} + switch def.calc { + case "none": + value = use_x + y, err = lp.New(def.name, m.tags, m.meta, map[string]interface{}{"value": value}, time.Now()) + case "difference": + value = use_x - devData[def.name] + if value.(int64) < 0 { + value = 0 } + y, err = lp.New(def.name, m.tags, m.meta, map[string]interface{}{"value": value}, time.Now()) + case "derivative": + value = float64(use_x-devData[def.name]) / tdiff.Seconds() + if value.(float64) < 0 { + value = 0 + } + y, err = lp.New(def.name, m.tags, m.meta, map[string]interface{}{"value": value}, time.Now()) } + if err == nil { + y.AddTag("device", device) + if len(def.unit) > 0 { + y.AddMeta("unit", def.unit) + } + output <- y + } + devData[def.name] = use_x } } + m.lastTimestamp = now } func (m *LustreCollector) Close() { diff --git a/collectors/lustreMetric.md b/collectors/lustreMetric.md index 0cb9fc8..f11b85f 100644 --- a/collectors/lustreMetric.md +++ b/collectors/lustreMetric.md @@ -3,27 +3,44 @@ ```json "lustrestat": { - "procfiles" : [ - "/proc/fs/lustre/llite/lnec-XXXXXX/stats" - ], + "lctl_command": "/path/to/lctl", "exclude_metrics": [ "setattr", "getattr" - ] + ], + "send_abs_values" : true, + "send_derived_values" : true, + "send_diff_values": true, + "use_sudo": false } ``` -The `lustrestat` collector reads from the procfs stat files for Lustre like `/proc/fs/lustre/llite/lnec-XXXXXX/stats`. +The `lustrestat` collector uses the `lctl` application with the `get_param` option to get all `llite` metrics (Lustre client). The `llite` metrics are only available for root users. If password-less sudo is configured, you can enable `sudo` in the configuration. Metrics: -* `read_bytes` -* `read_requests` -* `write_bytes` -* `write_requests` -* `open` -* `close` -* `getattr` -* `setattr` -* `statfs` -* `inode_permission` +* `lustre_read_bytes` (unit `bytes`) +* `lustre_read_requests` (unit `requests`) +* `lustre_write_bytes` (unit `bytes`) +* `lustre_write_requests` (unit `requests`) +* `lustre_open` +* `lustre_close` +* `lustre_getattr` +* `lustre_setattr` +* `lustre_statfs` +* `lustre_inode_permission` +* `lustre_read_bw` (if `send_derived_values == true`, unit `bytes/sec`) +* `lustre_write_bw` (if `send_derived_values == true`, unit `bytes/sec`) +* `lustre_read_requests_rate` (if `send_derived_values == true`, unit `requests/sec`) +* `lustre_write_requests_rate` (if `send_derived_values == true`, unit `requests/sec`) +* `lustre_read_bytes_diff` (if `send_diff_values == true`, unit `bytes`) +* `lustre_read_requests_diff` (if `send_diff_values == true`, unit `requests`) +* `lustre_write_bytes_diff` (if `send_diff_values == true`, unit `bytes`) +* `lustre_write_requests_diff` (if `send_diff_values == true`, unit `requests`) +* `lustre_open_diff` (if `send_diff_values == true`) +* `lustre_close_diff` (if `send_diff_values == true`) +* `lustre_getattr_diff` (if `send_diff_values == true`) +* `lustre_setattr_diff` (if `send_diff_values == true`) +* `lustre_statfs_diff` (if `send_diff_values == true`) +* `lustre_inode_permission_diff` (if `send_diff_values == true`) +This collector adds an `device` tag. \ No newline at end of file diff --git a/collectors/memstatMetric.go b/collectors/memstatMetric.go index 3998537..bd7af5d 100644 --- a/collectors/memstatMetric.go +++ b/collectors/memstatMetric.go @@ -32,11 +32,12 @@ type MemstatCollectorNode struct { type MemstatCollector struct { metricCollector - stats map[string]int64 - tags map[string]string - matches map[string]string - config MemstatCollectorConfig - nodefiles map[int]MemstatCollectorNode + stats map[string]int64 + tags map[string]string + matches map[string]string + config MemstatCollectorConfig + nodefiles map[int]MemstatCollectorNode + sendMemUsed bool } func getStats(filename string) map[string]float64 { @@ -77,7 +78,7 @@ func (m *MemstatCollector) Init(config json.RawMessage) error { return err } } - m.meta = map[string]string{"source": m.name, "group": "Memory", "unit": "kByte"} + m.meta = map[string]string{"source": m.name, "group": "Memory", "unit": "GByte"} m.stats = make(map[string]int64) m.matches = make(map[string]string) m.tags = map[string]string{"type": "node"} @@ -99,6 +100,10 @@ func (m *MemstatCollector) Init(config json.RawMessage) error { m.matches[k] = v } } + m.sendMemUsed = false + if _, skip := stringArrayContains(m.config.ExcludeMetrics, "mem_used"); !skip { + m.sendMemUsed = true + } if len(m.matches) == 0 { return errors.New("no metrics to collect") } @@ -152,23 +157,26 @@ func (m *MemstatCollector) Read(interval time.Duration, output chan lp.CCMetric) if v, ok := stats[match]; ok { value = v } - y, err := lp.New(name, tags, m.meta, map[string]interface{}{"value": value}, time.Now()) + y, err := lp.New(name, tags, m.meta, map[string]interface{}{"value": value * 1e-6}, time.Now()) if err == nil { output <- y } } - if _, skip := stringArrayContains(m.config.ExcludeMetrics, "mem_used"); !skip { - if freeVal, free := stats["MemFree"]; free { - if bufVal, buffers := stats["Buffers"]; buffers { - if cacheVal, cached := stats["Cached"]; cached { - memUsed := stats["MemTotal"] - (freeVal + bufVal + cacheVal) - y, err := lp.New("mem_used", tags, m.meta, map[string]interface{}{"value": memUsed}, time.Now()) - if err == nil { - output <- y + if m.sendMemUsed { + memUsed := 0.0 + if totalVal, total := stats["MemTotal"]; total { + if freeVal, free := stats["MemFree"]; free { + if bufVal, buffers := stats["Buffers"]; buffers { + if cacheVal, cached := stats["Cached"]; cached { + memUsed = totalVal - (freeVal + bufVal + cacheVal) } } } } + y, err := lp.New("mem_used", tags, m.meta, map[string]interface{}{"value": memUsed * 1e-6}, time.Now()) + if err == nil { + output <- y + } } } diff --git a/collectors/metricCollector.go b/collectors/metricCollector.go index 747772f..7c04e90 100644 --- a/collectors/metricCollector.go +++ b/collectors/metricCollector.go @@ -125,5 +125,5 @@ func RemoveFromStringList(s []string, r string) ([]string, error) { return append(s[:i], s[i+1:]...), nil } } - return s, fmt.Errorf("No such string in list") + return s, fmt.Errorf("no such string in list") } diff --git a/collectors/netstatMetric.go b/collectors/netstatMetric.go index 7eaa3cf..d171d4b 100644 --- a/collectors/netstatMetric.go +++ b/collectors/netstatMetric.go @@ -13,22 +13,27 @@ import ( lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) -const NETSTATFILE = `/proc/net/dev` +const NETSTATFILE = "/proc/net/dev" type NetstatCollectorConfig struct { - IncludeDevices []string `json:"include_devices"` + IncludeDevices []string `json:"include_devices"` + SendAbsoluteValues bool `json:"send_abs_values"` + SendDerivedValues bool `json:"send_derived_values"` } type NetstatCollectorMetric struct { - index int - lastValue float64 + name string + index int + tags map[string]string + meta map[string]string + meta_rates map[string]string + lastValue int64 } type NetstatCollector struct { metricCollector config NetstatCollectorConfig - matches map[string]map[string]NetstatCollectorMetric - devtags map[string]map[string]string + matches map[string][]NetstatCollectorMetric lastTimestamp time.Time } @@ -36,15 +41,33 @@ func (m *NetstatCollector) Init(config json.RawMessage) error { m.name = "NetstatCollector" m.setup() m.lastTimestamp = time.Now() - m.meta = map[string]string{"source": m.name, "group": "Network"} - m.devtags = make(map[string]map[string]string) - nameIndexMap := map[string]int{ - "net_bytes_in": 1, - "net_pkts_in": 2, - "net_bytes_out": 9, - "net_pkts_out": 10, - } - m.matches = make(map[string]map[string]NetstatCollectorMetric) + + const ( + fieldInterface = iota + fieldReceiveBytes + fieldReceivePackets + fieldReceiveErrs + fieldReceiveDrop + fieldReceiveFifo + fieldReceiveFrame + fieldReceiveCompressed + fieldReceiveMulticast + fieldTransmitBytes + fieldTransmitPackets + fieldTransmitErrs + fieldTransmitDrop + fieldTransmitFifo + fieldTransmitColls + fieldTransmitCarrier + fieldTransmitCompressed + ) + + m.matches = make(map[string][]NetstatCollectorMetric) + + // Set default configuration, + m.config.SendAbsoluteValues = true + m.config.SendDerivedValues = false + // Read configuration file, allow overwriting default config if len(config) > 0 { err := json.Unmarshal(config, &m.config) if err != nil { @@ -52,7 +75,9 @@ func (m *NetstatCollector) Init(config json.RawMessage) error { return err } } - file, err := os.Open(string(NETSTATFILE)) + + // Check access to net statistic file + file, err := os.Open(NETSTATFILE) if err != nil { cclog.ComponentError(m.name, err.Error()) return err @@ -62,23 +87,65 @@ func (m *NetstatCollector) Init(config json.RawMessage) error { scanner := bufio.NewScanner(file) for scanner.Scan() { l := scanner.Text() + + // Skip lines with no net device entry if !strings.Contains(l, ":") { continue } + + // Split line into fields f := strings.Fields(l) + + // Get net device entry dev := strings.Trim(f[0], ": ") + + // Check if device is a included device if _, ok := stringArrayContains(m.config.IncludeDevices, dev); ok { - m.matches[dev] = make(map[string]NetstatCollectorMetric) - for name, idx := range nameIndexMap { - m.matches[dev][name] = NetstatCollectorMetric{ - index: idx, - lastValue: 0, - } + tags := map[string]string{"device": dev, "type": "node"} + meta_unit_byte := map[string]string{"source": m.name, "group": "Network", "unit": "bytes"} + meta_unit_byte_per_sec := map[string]string{"source": m.name, "group": "Network", "unit": "bytes/sec"} + meta_unit_pkts := map[string]string{"source": m.name, "group": "Network", "unit": "packets"} + meta_unit_pkts_per_sec := map[string]string{"source": m.name, "group": "Network", "unit": "packets/sec"} + + m.matches[dev] = []NetstatCollectorMetric{ + { + name: "net_bytes_in", + index: fieldReceiveBytes, + lastValue: -1, + tags: tags, + meta: meta_unit_byte, + meta_rates: meta_unit_byte_per_sec, + }, + { + name: "net_pkts_in", + index: fieldReceivePackets, + lastValue: -1, + tags: tags, + meta: meta_unit_pkts, + meta_rates: meta_unit_pkts_per_sec, + }, + { + name: "net_bytes_out", + index: fieldTransmitBytes, + lastValue: -1, + tags: tags, + meta: meta_unit_byte, + meta_rates: meta_unit_byte_per_sec, + }, + { + name: "net_pkts_out", + index: fieldTransmitPackets, + lastValue: -1, + tags: tags, + meta: meta_unit_pkts, + meta_rates: meta_unit_pkts_per_sec, + }, } - m.devtags[dev] = map[string]string{"device": dev, "type": "node"} } + } - if len(m.devtags) == 0 { + + if len(m.matches) == 0 { return errors.New("no devices to collector metrics found") } m.init = true @@ -89,50 +156,62 @@ func (m *NetstatCollector) Read(interval time.Duration, output chan lp.CCMetric) if !m.init { return } + // Current time stamp now := time.Now() + // time difference to last time stamp + timeDiff := now.Sub(m.lastTimestamp).Seconds() + // Save current timestamp + m.lastTimestamp = now + file, err := os.Open(string(NETSTATFILE)) if err != nil { cclog.ComponentError(m.name, err.Error()) return } defer file.Close() - tdiff := now.Sub(m.lastTimestamp) scanner := bufio.NewScanner(file) for scanner.Scan() { l := scanner.Text() + + // Skip lines with no net device entry if !strings.Contains(l, ":") { continue } + + // Split line into fields f := strings.Fields(l) + + // Get net device entry dev := strings.Trim(f[0], ":") + // Check if device is a included device if devmetrics, ok := m.matches[dev]; ok { - for name, data := range devmetrics { - v, err := strconv.ParseFloat(f[data.index], 64) - if err == nil { - vdiff := v - data.lastValue - value := vdiff / tdiff.Seconds() - if data.lastValue == 0 { - value = 0 - } - data.lastValue = v - y, err := lp.New(name, m.devtags[dev], m.meta, map[string]interface{}{"value": value}, now) - if err == nil { - switch { - case strings.Contains(name, "byte"): - y.AddMeta("unit", "bytes/sec") - case strings.Contains(name, "pkt"): - y.AddMeta("unit", "packets/sec") - } + for i := range devmetrics { + metric := &devmetrics[i] + + // Read value + v, err := strconv.ParseInt(f[metric.index], 10, 64) + if err != nil { + continue + } + if m.config.SendAbsoluteValues { + if y, err := lp.New(metric.name, metric.tags, metric.meta, map[string]interface{}{"value": v}, now); err == nil { output <- y } - devmetrics[name] = data + } + if m.config.SendDerivedValues { + if metric.lastValue >= 0 { + rate := float64(v-metric.lastValue) / timeDiff + if y, err := lp.New(metric.name+"_bw", metric.tags, metric.meta_rates, map[string]interface{}{"value": rate}, now); err == nil { + output <- y + } + } + metric.lastValue = v } } } } - m.lastTimestamp = time.Now() } func (m *NetstatCollector) Close() { diff --git a/collectors/netstatMetric.md b/collectors/netstatMetric.md index 90d8600..424cf77 100644 --- a/collectors/netstatMetric.md +++ b/collectors/netstatMetric.md @@ -5,17 +5,23 @@ "netstat": { "include_devices": [ "eth0" - ] + ], + "send_abs_values" : true, + "send_derived_values" : true } ``` The `netstat` collector reads data from `/proc/net/dev` and outputs a handful **node** metrics. With the `include_devices` list you can specify which network devices should be measured. **Note**: Most other collectors use an _exclude_ list instead of an include list. Metrics: -* `net_bytes_in` (`unit=bytes/sec`) -* `net_bytes_out` (`unit=bytes/sec`) -* `net_pkts_in` (`unit=packets/sec`) -* `net_pkts_out` (`unit=packets/sec`) +* `net_bytes_in` (`unit=bytes`) +* `net_bytes_out` (`unit=bytes`) +* `net_pkts_in` (`unit=packets`) +* `net_pkts_out` (`unit=packets`) +* `net_bytes_in_bw` (`unit=bytes/sec` if `send_derived_values == true`) +* `net_bytes_out_bw` (`unit=bytes/sec` if `send_derived_values == true`) +* `net_pkts_in_bw` (`unit=packets/sec` if `send_derived_values == true`) +* `net_pkts_out_bw` (`unit=packets/sec` if `send_derived_values == true`) The device name is added as tag `device`. diff --git a/collectors/tempMetric.go b/collectors/tempMetric.go index bbc5100..7ba8eb1 100644 --- a/collectors/tempMetric.go +++ b/collectors/tempMetric.go @@ -70,10 +70,10 @@ func (m *TempCollector) Init(config json.RawMessage) error { globPattern := filepath.Join("/sys/class/hwmon", "*", "temp*_input") inputFiles, err := filepath.Glob(globPattern) if err != nil { - return fmt.Errorf("Unable to glob files with pattern '%s': %v", globPattern, err) + return fmt.Errorf("unable to glob files with pattern '%s': %v", globPattern, err) } if inputFiles == nil { - return fmt.Errorf("Unable to find any files with pattern '%s'", globPattern) + return fmt.Errorf("unable to find any files with pattern '%s'", globPattern) } // Get sensor name for each temperature sensor file @@ -158,7 +158,7 @@ func (m *TempCollector) Init(config json.RawMessage) error { // Empty sensors map if len(m.sensors) == 0 { - return fmt.Errorf("No temperature sensors found") + return fmt.Errorf("no temperature sensors found") } // Finished initialization diff --git a/collectors/topprocsMetric.go b/collectors/topprocsMetric.go index dd6bff3..408c3cc 100644 --- a/collectors/topprocsMetric.go +++ b/collectors/topprocsMetric.go @@ -39,14 +39,14 @@ func (m *TopProcsCollector) Init(config json.RawMessage) error { m.config.Num_procs = int(DEFAULT_NUM_PROCS) } if m.config.Num_procs <= 0 || m.config.Num_procs > MAX_NUM_PROCS { - return errors.New(fmt.Sprintf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS)) + return fmt.Errorf("num_procs option must be set in 'topprocs' config (range: 1-%d)", MAX_NUM_PROCS) } m.setup() command := exec.Command("ps", "-Ao", "comm", "--sort=-pcpu") command.Wait() _, err = command.Output() if err != nil { - return errors.New("Failed to execute command") + return errors.New("failed to execute command") } m.init = true return nil diff --git a/docs/configuration.md b/docs/configuration.md index 75c7aa5..a13d3a5 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -37,11 +37,12 @@ flowchart LR subgraph Receivers ["Receivers"] direction TB nats["NATS"] + httprecv["HTTP"] miscrecv[...] end subgraph calc["Aggregator"] - direction TB + direction LR cache["Cache"] agg["Calculator"] end @@ -60,6 +61,7 @@ flowchart LR misc --> CollectorManager nats --> ReceiverManager["ReceiverManager"] + httprecv --> ReceiverManager miscrecv --> ReceiverManager CollectorManager --> newrouter["Router"] @@ -184,4 +186,4 @@ The receivers are a special feature of the CC Metric Collector to enable simpler This example configuration creates two receivers with the names `nats_rack0` and `nats_rack1`. While one subscribes to metrics published with the `rack0` subject, the other one subscribes to the `rack0` subject. The NATS server is the same as it manages all subjects in a subnet. (As example, the router could add tags `rack=0` and `rack=1` respectively to the received metrics.) -All types and possible receiver-specific configuration options can be found [here](../receivers/README.md). \ No newline at end of file +All types and possible receiver-specific configuration options can be found [here](../receivers/README.md). diff --git a/internal/ccMetric/ccMetric.go b/internal/ccMetric/ccMetric.go index 1de325a..661b9a4 100644 --- a/internal/ccMetric/ccMetric.go +++ b/internal/ccMetric/ccMetric.go @@ -24,8 +24,8 @@ type ccMetric struct { // ccMetric access functions type CCMetric interface { - ToPoint(metaAsTags bool) *write.Point // Generate influxDB point for data type ccMetric - ToLineProtocol(metaAsTags bool) string // Generate influxDB line protocol for data type ccMetric + ToPoint(metaAsTags map[string]bool) *write.Point // Generate influxDB point for data type ccMetric + ToLineProtocol(metaAsTags map[string]bool) string // Generate influxDB line protocol for data type ccMetric Name() string // Get metric name SetName(name string) // Set metric name @@ -61,25 +61,18 @@ func (m *ccMetric) String() string { } // ToLineProtocol generates influxDB line protocol for data type ccMetric -func (m *ccMetric) ToPoint(metaAsTags bool) (p *write.Point) { - - if !metaAsTags { - p = influxdb2.NewPoint(m.name, m.tags, m.fields, m.tm) - } else { - tags := make(map[string]string, len(m.tags)+len(m.meta)) - for key, value := range m.tags { - tags[key] = value +func (m *ccMetric) ToPoint(metaAsTags map[string]bool) (p *write.Point) { + p = influxdb2.NewPoint(m.name, m.tags, m.fields, m.tm) + for key, ok1 := range metaAsTags { + if val, ok2 := m.GetMeta(key); ok1 && ok2 { + p.AddTag(key, val) } - for key, value := range m.meta { - tags[key] = value - } - p = influxdb2.NewPoint(m.name, tags, m.fields, m.tm) } - return + return p } // ToLineProtocol generates influxDB line protocol for data type ccMetric -func (m *ccMetric) ToLineProtocol(metaAsTags bool) string { +func (m *ccMetric) ToLineProtocol(metaAsTags map[string]bool) string { return write.PointToLineProtocol( m.ToPoint(metaAsTags), diff --git a/internal/metricRouter/metricRouter.go b/internal/metricRouter/metricRouter.go index 90650ea..7ad1e7f 100644 --- a/internal/metricRouter/metricRouter.go +++ b/internal/metricRouter/metricRouter.go @@ -25,6 +25,7 @@ type metricRouterTagConfig struct { // Metric router configuration type metricRouterConfig struct { + HostnameTagName string `json:"hostname_tag"` // Key name used when adding the hostname to a metric (default 'hostname') AddTags []metricRouterTagConfig `json:"add_tags"` // List of tags that are added when the condition is met DelTags []metricRouterTagConfig `json:"delete_tags"` // List of tags that are removed when the condition is met IntervalAgg []agg.MetricAggregatorIntervalConfig `json:"interval_aggregates"` // List of aggregation function processed at the end of an interval @@ -33,6 +34,7 @@ type metricRouterConfig struct { RenameMetrics map[string]string `json:"rename_metrics"` // Map to rename metric name from key to value IntervalStamp bool `json:"interval_timestamp"` // Update timestamp periodically by ticker each interval? NumCacheIntervals int `json:"num_cache_intervals"` // Number of intervals of cached metrics for evaluation + MaxForward int `json:"max_forward"` // Number of maximal forwarded metrics at one select dropMetrics map[string]bool // Internal map for O(1) lookup } @@ -76,7 +78,8 @@ func (r *metricRouter) Init(ticker mct.MultiChanTicker, wg *sync.WaitGroup, rout r.cache_input = make(chan lp.CCMetric) r.wg = wg r.ticker = ticker - r.maxForward = ROUTER_MAX_FORWARD + r.config.MaxForward = ROUTER_MAX_FORWARD + r.config.HostnameTagName = "hostname" // Set hostname hostname, err := os.Hostname() @@ -100,6 +103,7 @@ func (r *metricRouter) Init(ticker mct.MultiChanTicker, wg *sync.WaitGroup, rout cclog.ComponentError("MetricRouter", err.Error()) return err } + r.maxForward = r.config.MaxForward if r.config.NumCacheIntervals > 0 { r.cache, err = NewCache(r.cache_input, r.ticker, &r.cachewg, r.config.NumCacheIntervals) if err != nil { @@ -244,8 +248,10 @@ func (r *metricRouter) Start() { cclog.ComponentDebug("MetricRouter", "FORWARD", point) r.DoAddTags(point) r.DoDelTags(point) - if new, ok := r.config.RenameMetrics[point.Name()]; ok { + name := point.Name() + if new, ok := r.config.RenameMetrics[name]; ok { point.SetName(new) + point.AddMeta("oldname", name) } r.DoAddTags(point) r.DoDelTags(point) @@ -258,7 +264,7 @@ func (r *metricRouter) Start() { // Foward message received from collector channel coll_forward := func(p lp.CCMetric) { // receive from metric collector - p.AddTag("hostname", r.hostname) + p.AddTag(r.config.HostnameTagName, r.hostname) if r.config.IntervalStamp { p.SetTime(r.timestamp) } @@ -287,7 +293,7 @@ func (r *metricRouter) Start() { cache_forward := func(p lp.CCMetric) { // receive from metric collector if !r.dropMetric(p) { - p.AddTag("hostname", r.hostname) + p.AddTag(r.config.HostnameTagName, r.hostname) forward(p) } } @@ -309,19 +315,19 @@ func (r *metricRouter) Start() { case p := <-r.coll_input: coll_forward(p) - for i := 0; len(r.coll_input) > 0 && i < r.maxForward; i++ { + for i := 0; len(r.coll_input) > 0 && i < (r.maxForward-1); i++ { coll_forward(<-r.coll_input) } case p := <-r.recv_input: recv_forward(p) - for i := 0; len(r.recv_input) > 0 && i < r.maxForward; i++ { + for i := 0; len(r.recv_input) > 0 && i < (r.maxForward-1); i++ { recv_forward(<-r.recv_input) } case p := <-r.cache_input: cache_forward(p) - for i := 0; len(r.cache_input) > 0 && i < r.maxForward; i++ { + for i := 0; len(r.cache_input) > 0 && i < (r.maxForward-1); i++ { cache_forward(<-r.cache_input) } } diff --git a/receivers/README.md b/receivers/README.md index 70b209b..3599a93 100644 --- a/receivers/README.md +++ b/receivers/README.md @@ -21,6 +21,7 @@ This allows to specify - [`nats`](./natsReceiver.md): Receive metrics from the NATS network - [`prometheus`](./prometheusReceiver.md): Scrape data from a Prometheus client +- [`http`](./httpReceiver.md): Listen for HTTP Post requests transporting metrics in InfluxDB line protocol # Contributing own receivers A receiver contains a few functions and is derived from the type `Receiver` (in `metricReceiver.go`): diff --git a/receivers/httpReceiver.go b/receivers/httpReceiver.go new file mode 100644 index 0000000..e66ad5e --- /dev/null +++ b/receivers/httpReceiver.go @@ -0,0 +1,118 @@ +package receivers + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "net/http" + "strings" + "sync" + + cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" + lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" + "github.com/gorilla/mux" + influx "github.com/influxdata/line-protocol" +) + +const HTTP_RECEIVER_PORT = "8080" + +type HttpReceiverConfig struct { + Type string `json:"type"` + Addr string `json:"address"` + Port string `json:"port"` + Path string `json:"path"` +} + +type HttpReceiver struct { + receiver + handler *influx.MetricHandler + parser *influx.Parser + meta map[string]string + config HttpReceiverConfig + router *mux.Router + server *http.Server + wg sync.WaitGroup +} + +func (r *HttpReceiver) Init(name string, config json.RawMessage) error { + r.name = fmt.Sprintf("HttpReceiver(%s)", name) + r.config.Port = HTTP_RECEIVER_PORT + if len(config) > 0 { + err := json.Unmarshal(config, &r.config) + if err != nil { + cclog.ComponentError(r.name, "Error reading config:", err.Error()) + return err + } + } + if len(r.config.Port) == 0 { + return errors.New("not all configuration variables set required by HttpReceiver") + } + r.meta = map[string]string{"source": r.name} + p := r.config.Path + if !strings.HasPrefix(p, "/") { + p = "/" + p + } + uri := fmt.Sprintf("%s:%s%s", r.config.Addr, r.config.Port, p) + cclog.ComponentDebug(r.name, "INIT", uri) + r.handler = influx.NewMetricHandler() + r.parser = influx.NewParser(r.handler) + r.parser.SetTimeFunc(DefaultTime) + + r.router = mux.NewRouter() + r.router.Path(p).HandlerFunc(r.ServerHttp) + r.server = &http.Server{Addr: uri, Handler: r.router} + return nil +} + +func (r *HttpReceiver) Start() { + cclog.ComponentDebug(r.name, "START") + r.wg.Add(1) + go func() { + err := r.server.ListenAndServe() + if err != nil && err.Error() != "http: Server closed" { + cclog.ComponentError(r.name, err.Error()) + } + r.wg.Done() + }() +} + +func (r *HttpReceiver) ServerHttp(w http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPost { + http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) + return + } + + body, err := ioutil.ReadAll(req.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + metrics, err := r.parser.Parse(body) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + } + + for _, m := range metrics { + y := lp.FromInfluxMetric(m) + for k, v := range r.meta { + y.AddMeta(k, v) + } + if r.sink != nil { + r.sink <- y + } + } + + w.WriteHeader(http.StatusOK) +} + +func (r *HttpReceiver) Close() { + r.server.Shutdown(context.Background()) +} + +func NewHttpReceiver(name string, config json.RawMessage) (Receiver, error) { + r := new(HttpReceiver) + err := r.Init(name, config) + return r, err +} diff --git a/receivers/httpReceiver.md b/receivers/httpReceiver.md new file mode 100644 index 0000000..ed1e1bf --- /dev/null +++ b/receivers/httpReceiver.md @@ -0,0 +1,23 @@ +## `http` receiver + +The `http` receiver can be used receive metrics through HTTP POST requests. + +### Configuration structure + +```json +{ + "": { + "type": "http", + "address" : "", + "port" : "8080", + "path" : "/write" + } +} +``` + +- `type`: makes the receiver a `http` receiver +- `address`: Listen address +- `port`: Listen port +- `path`: URL path for the write endpoint + +The HTTP endpoint listens to `http://
:/` diff --git a/scripts/cc-metric-collector.deb.control b/scripts/cc-metric-collector.deb.control new file mode 100644 index 0000000..8f752cd --- /dev/null +++ b/scripts/cc-metric-collector.deb.control @@ -0,0 +1,12 @@ +Package: cc-metric-collector +Version: {VERSION} +Installed-Size: {INSTALLED_SIZE} +Architecture: {ARCH} +Maintainer: thomas.gruber@fau.de +Depends: libc6 (>= 2.2.1) +Build-Depends: debhelper-compat (= 13), git, golang-go +Description: Metric collection daemon from the ClusterCockpit suite +Homepage: https://github.com/ClusterCockpit/cc-metric-collector +Source: cc-metric-collector +Rules-Requires-Root: no + diff --git a/scripts/cc-metric-collector.spec b/scripts/cc-metric-collector.spec index 8b23f20..9d55b4f 100644 --- a/scripts/cc-metric-collector.spec +++ b/scripts/cc-metric-collector.spec @@ -8,8 +8,8 @@ Source0: %{name}-%{version}.tar.gz BuildRequires: go-toolset BuildRequires: systemd-rpm-macros -# for internal LIKWID installation -BuildRequires: wget perl-Data-Dumper +# for header downloads +BuildRequires: wget Provides: %{name} = %{version} diff --git a/sinks/gangliaCommon.go b/sinks/gangliaCommon.go index b2a1b2c..f92550b 100644 --- a/sinks/gangliaCommon.go +++ b/sinks/gangliaCommon.go @@ -148,10 +148,14 @@ type GangliaMetricConfig struct { Unit string Group string Value string + Name string } func GetCommonGangliaConfig(point lp.CCMetric) GangliaMetricConfig { mname := GangliaMetricRename(point.Name()) + if oldname, ok := point.GetMeta("oldname"); ok { + mname = GangliaMetricRename(oldname) + } for _, group := range CommonGangliaMetrics { for _, metric := range group.Metrics { if metric.Name == mname { @@ -187,6 +191,7 @@ func GetCommonGangliaConfig(point lp.CCMetric) GangliaMetricConfig { Tmax: metric.Tmax, Unit: metric.Unit, Value: valueStr, + Name: GangliaMetricRename(mname), } } } @@ -198,10 +203,15 @@ func GetCommonGangliaConfig(point lp.CCMetric) GangliaMetricConfig { Tmax: 0, Unit: "", Value: "", + Name: "", } } func GetGangliaConfig(point lp.CCMetric) GangliaMetricConfig { + mname := GangliaMetricRename(point.Name()) + if oldname, ok := point.GetMeta("oldname"); ok { + mname = GangliaMetricRename(oldname) + } group := "" if g, ok := point.GetMeta("group"); ok { group = g @@ -254,5 +264,6 @@ func GetGangliaConfig(point lp.CCMetric) GangliaMetricConfig { Tmax: DEFAULT_GANGLIA_METRIC_TMAX, Unit: unit, Value: valueStr, + Name: GangliaMetricRename(mname), } } diff --git a/sinks/gangliaSink.go b/sinks/gangliaSink.go index 22096af..5324123 100644 --- a/sinks/gangliaSink.go +++ b/sinks/gangliaSink.go @@ -39,16 +39,13 @@ func (s *GangliaSink) Write(point lp.CCMetric) error { //var tagsstr []string var argstr []string - // Get metric name - metricname := GangliaMetricRename(point.Name()) - // Get metric config (type, value, ... in suitable format) conf := GetCommonGangliaConfig(point) if len(conf.Type) == 0 { conf = GetGangliaConfig(point) } if len(conf.Type) == 0 { - return fmt.Errorf("metric %s has no 'value' field", metricname) + return fmt.Errorf("metric %q (Ganglia name %q) has no 'value' field", point.Name(), conf.Name) } if s.config.AddGangliaGroup { @@ -70,7 +67,7 @@ func (s *GangliaSink) Write(point lp.CCMetric) error { if s.config.AddTypeToName { argstr = append(argstr, fmt.Sprintf("--name=%s", GangliaMetricName(point))) } else { - argstr = append(argstr, fmt.Sprintf("--name=%s", metricname)) + argstr = append(argstr, fmt.Sprintf("--name=%s", conf.Name)) } argstr = append(argstr, fmt.Sprintf("--slope=%s", conf.Slope)) argstr = append(argstr, fmt.Sprintf("--value=%s", conf.Value)) diff --git a/sinks/httpSink.go b/sinks/httpSink.go index c2dd2ea..398eaf3 100644 --- a/sinks/httpSink.go +++ b/sinks/httpSink.go @@ -53,7 +53,7 @@ func (s *HttpSink) Write(m lp.CCMetric) error { }) } - p := m.ToPoint(s.config.MetaAsTags) + p := m.ToPoint(s.meta_as_tags) s.lock.Lock() _, err := s.encoder.Encode(p) @@ -159,6 +159,11 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) { s.flushDelay = t } } + // Create lookup map to use meta infos as tags in the output metric + s.meta_as_tags = make(map[string]bool) + for _, k := range s.config.MetaAsTags { + s.meta_as_tags[k] = true + } tr := &http.Transport{ MaxIdleConns: s.maxIdleConns, IdleConnTimeout: s.idleConnTimeout, diff --git a/sinks/influxAsyncSink.go b/sinks/influxAsyncSink.go index 7b38873..213f2d6 100644 --- a/sinks/influxAsyncSink.go +++ b/sinks/influxAsyncSink.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "time" cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" @@ -26,15 +27,21 @@ type InfluxAsyncSinkConfig struct { // Maximum number of points sent to server in single request. Default 5000 BatchSize uint `json:"batch_size,omitempty"` // Interval, in ms, in which is buffer flushed if it has not been already written (by reaching batch size) . Default 1000ms - FlushInterval uint `json:"flush_interval,omitempty"` + FlushInterval uint `json:"flush_interval,omitempty"` + InfluxRetryInterval string `json:"retry_interval"` + InfluxExponentialBase uint `json:"retry_exponential_base"` + InfluxMaxRetries uint `json:"max_retries"` + InfluxMaxRetryTime string `json:"max_retry_time"` } type InfluxAsyncSink struct { sink - client influxdb2.Client - writeApi influxdb2Api.WriteAPI - errors <-chan error - config InfluxAsyncSinkConfig + client influxdb2.Client + writeApi influxdb2Api.WriteAPI + errors <-chan error + config InfluxAsyncSinkConfig + influxRetryInterval uint + influxMaxRetryTime uint } func (s *InfluxAsyncSink) connect() error { @@ -63,6 +70,11 @@ func (s *InfluxAsyncSink) connect() error { InsecureSkipVerify: true, }, ) + clientOptions.SetMaxRetryInterval(s.influxRetryInterval) + clientOptions.SetMaxRetryTime(s.influxMaxRetryTime) + clientOptions.SetExponentialBase(s.config.InfluxExponentialBase) + clientOptions.SetMaxRetries(s.config.InfluxMaxRetries) + s.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions) s.writeApi = s.client.WriteAPI(s.config.Organization, s.config.Database) ok, err := s.client.Ping(context.Background()) @@ -77,7 +89,7 @@ func (s *InfluxAsyncSink) connect() error { func (s *InfluxAsyncSink) Write(m lp.CCMetric) error { s.writeApi.WritePoint( - m.ToPoint(s.config.MetaAsTags), + m.ToPoint(s.meta_as_tags), ) return nil } @@ -99,6 +111,33 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) { // Set default for maximum number of points sent to server in single request. s.config.BatchSize = 100 + s.influxRetryInterval = uint(time.Duration(1) * time.Second) + s.config.InfluxRetryInterval = "1s" + s.influxMaxRetryTime = uint(7 * time.Duration(24) * time.Hour) + s.config.InfluxMaxRetryTime = "168h" + s.config.InfluxMaxRetries = 20 + s.config.InfluxExponentialBase = 2 + + // Default retry intervals (in seconds) + // 1 2 + // 2 4 + // 4 8 + // 8 16 + // 16 32 + // 32 64 + // 64 128 + // 128 256 + // 256 512 + // 512 1024 + // 1024 2048 + // 2048 4096 + // 4096 8192 + // 8192 16384 + // 16384 32768 + // 32768 65536 + // 65536 131072 + // 131072 262144 + // 262144 524288 if len(config) > 0 { err := json.Unmarshal(config, &s.config) @@ -113,6 +152,21 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) { len(s.config.Password) == 0 { return nil, errors.New("not all configuration variables set required by InfluxAsyncSink") } + // Create lookup map to use meta infos as tags in the output metric + s.meta_as_tags = make(map[string]bool) + for _, k := range s.config.MetaAsTags { + s.meta_as_tags[k] = true + } + + toUint := func(duration string, def uint) uint { + t, err := time.ParseDuration(duration) + if err == nil { + return uint(t.Milliseconds()) + } + return def + } + s.influxRetryInterval = toUint(s.config.InfluxRetryInterval, s.influxRetryInterval) + s.influxMaxRetryTime = toUint(s.config.InfluxMaxRetryTime, s.influxMaxRetryTime) // Connect to InfluxDB server if err := s.connect(); err != nil { diff --git a/sinks/influxAsyncSink.md b/sinks/influxAsyncSink.md index 286c93c..951d67d 100644 --- a/sinks/influxAsyncSink.md +++ b/sinks/influxAsyncSink.md @@ -18,6 +18,10 @@ The `influxasync` sink uses the official [InfluxDB golang client](https://pkg.go "organization": "myorg", "ssl": true, "batch_size": 200, + "retry_interval" : "1s", + "retry_exponential_base" : 2, + "max_retries": 20, + "max_retry_time" : "168h" } } ``` @@ -31,4 +35,10 @@ The `influxasync` sink uses the official [InfluxDB golang client](https://pkg.go - `password`: Password for basic authentification - `organization`: Organization in the InfluxDB - `ssl`: Use SSL connection -- `batch_size`: batch up metrics internally, default 100 \ No newline at end of file +- `batch_size`: batch up metrics internally, default 100 +- `retry_interval`: Base retry interval for failed write requests, default 1s +- `retry_exponential_base`: The retry interval is exponentially increased with this base, default 2 +- `max_retries`: Maximal number of retry attempts +- `max_retry_time`: Maximal time to retry failed writes, default 168h (one week) + +For information about the calculation of the retry interval settings, see [offical influxdb-client-go documentation](https://github.com/influxdata/influxdb-client-go#handling-of-failed-async-writes) \ No newline at end of file diff --git a/sinks/influxSink.go b/sinks/influxSink.go index 11859b2..1987342 100644 --- a/sinks/influxSink.go +++ b/sinks/influxSink.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "time" cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" @@ -15,21 +16,29 @@ import ( type InfluxSinkConfig struct { defaultSinkConfig - Host string `json:"host,omitempty"` - Port string `json:"port,omitempty"` - Database string `json:"database,omitempty"` - User string `json:"user,omitempty"` - Password string `json:"password,omitempty"` - Organization string `json:"organization,omitempty"` - SSL bool `json:"ssl,omitempty"` - RetentionPol string `json:"retention_policy,omitempty"` + Host string `json:"host,omitempty"` + Port string `json:"port,omitempty"` + Database string `json:"database,omitempty"` + User string `json:"user,omitempty"` + Password string `json:"password,omitempty"` + Organization string `json:"organization,omitempty"` + SSL bool `json:"ssl,omitempty"` + RetentionPol string `json:"retention_policy,omitempty"` + InfluxRetryInterval string `json:"retry_interval"` + InfluxExponentialBase uint `json:"retry_exponential_base"` + InfluxMaxRetries uint `json:"max_retries"` + InfluxMaxRetryTime string `json:"max_retry_time"` + //InfluxMaxRetryDelay string `json:"max_retry_delay"` // It is mentioned in the docs but there is no way to set it } type InfluxSink struct { sink - client influxdb2.Client - writeApi influxdb2Api.WriteAPIBlocking - config InfluxSinkConfig + client influxdb2.Client + writeApi influxdb2Api.WriteAPIBlocking + config InfluxSinkConfig + influxRetryInterval uint + influxMaxRetryTime uint + //influxMaxRetryDelay uint } func (s *InfluxSink) connect() error { @@ -52,6 +61,12 @@ func (s *InfluxSink) connect() error { InsecureSkipVerify: true, }, ) + + clientOptions.SetMaxRetryInterval(s.influxRetryInterval) + clientOptions.SetMaxRetryTime(s.influxMaxRetryTime) + clientOptions.SetExponentialBase(s.config.InfluxExponentialBase) + clientOptions.SetMaxRetries(s.config.InfluxMaxRetries) + s.client = influxdb2.NewClientWithOptions(uri, auth, clientOptions) s.writeApi = s.client.WriteAPIBlocking(s.config.Organization, s.config.Database) ok, err := s.client.Ping(context.Background()) @@ -68,7 +83,7 @@ func (s *InfluxSink) Write(m lp.CCMetric) error { err := s.writeApi.WritePoint( context.Background(), - m.ToPoint(s.config.MetaAsTags), + m.ToPoint(s.meta_as_tags), ) return err } @@ -91,6 +106,13 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) { return nil, err } } + s.influxRetryInterval = uint(time.Duration(1) * time.Second) + s.config.InfluxRetryInterval = "1s" + s.influxMaxRetryTime = uint(7 * time.Duration(24) * time.Hour) + s.config.InfluxMaxRetryTime = "168h" + s.config.InfluxMaxRetries = 20 + s.config.InfluxExponentialBase = 2 + if len(s.config.Host) == 0 || len(s.config.Port) == 0 || len(s.config.Database) == 0 || @@ -98,6 +120,21 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) { len(s.config.Password) == 0 { return nil, errors.New("not all configuration variables set required by InfluxSink") } + // Create lookup map to use meta infos as tags in the output metric + s.meta_as_tags = make(map[string]bool) + for _, k := range s.config.MetaAsTags { + s.meta_as_tags[k] = true + } + + toUint := func(duration string, def uint) uint { + t, err := time.ParseDuration(duration) + if err == nil { + return uint(t.Milliseconds()) + } + return def + } + s.influxRetryInterval = toUint(s.config.InfluxRetryInterval, s.influxRetryInterval) + s.influxMaxRetryTime = toUint(s.config.InfluxMaxRetryTime, s.influxMaxRetryTime) // Connect to InfluxDB server if err := s.connect(); err != nil { diff --git a/sinks/influxSink.md b/sinks/influxSink.md index bd0f576..a099895 100644 --- a/sinks/influxSink.md +++ b/sinks/influxSink.md @@ -17,6 +17,10 @@ The `influxdb` sink uses the official [InfluxDB golang client](https://pkg.go.de "password" : "examplepw", "organization": "myorg", "ssl": true, + "retry_interval" : "1s", + "retry_exponential_base" : 2, + "max_retries": 20, + "max_retry_time" : "168h" } } ``` @@ -29,4 +33,10 @@ The `influxdb` sink uses the official [InfluxDB golang client](https://pkg.go.de - `user`: Username for basic authentification - `password`: Password for basic authentification - `organization`: Organization in the InfluxDB -- `ssl`: Use SSL connection \ No newline at end of file +- `ssl`: Use SSL connection +- `retry_interval`: Base retry interval for failed write requests, default 1s +- `retry_exponential_base`: The retry interval is exponentially increased with this base, default 2 +- `max_retries`: Maximal number of retry attempts +- `max_retry_time`: Maximal time to retry failed writes, default 168h (one week) + +For information about the calculation of the retry interval settings, see [offical influxdb-client-go documentation](https://github.com/influxdata/influxdb-client-go#handling-of-failed-async-writes) \ No newline at end of file diff --git a/sinks/libgangliaSink.go b/sinks/libgangliaSink.go index 1fc7863..3651584 100644 --- a/sinks/libgangliaSink.go +++ b/sinks/libgangliaSink.go @@ -124,24 +124,21 @@ func (s *LibgangliaSink) Write(point lp.CCMetric) error { return s.cstrCache[key] } - // Get metric name - metricname := GangliaMetricRename(point.Name()) - conf := GetCommonGangliaConfig(point) if len(conf.Type) == 0 { conf = GetGangliaConfig(point) } if len(conf.Type) == 0 { - return fmt.Errorf("metric %s has no 'value' field", metricname) + return fmt.Errorf("metric %q (Ganglia name %q) has no 'value' field", point.Name(), conf.Name) } if s.config.AddTypeToName { - metricname = GangliaMetricName(point) + conf.Name = GangliaMetricName(point) } c_value = C.CString(conf.Value) c_type = lookup(conf.Type) - c_name = lookup(metricname) + c_name = lookup(conf.Name) // Add unit unit := "" diff --git a/sinks/metricSink.go b/sinks/metricSink.go index d5356d0..c6c6860 100644 --- a/sinks/metricSink.go +++ b/sinks/metricSink.go @@ -5,13 +5,13 @@ import ( ) type defaultSinkConfig struct { - MetaAsTags bool `json:"meta_as_tags,omitempty"` - Type string `json:"type"` + MetaAsTags []string `json:"meta_as_tags,omitempty"` + Type string `json:"type"` } type sink struct { - meta_as_tags bool // Use meta data tags as tags - name string // Name of the sink + meta_as_tags map[string]bool // Use meta data tags as tags + name string // Name of the sink } type Sink interface { diff --git a/sinks/natsSink.go b/sinks/natsSink.go index 0d7987e..0597e9b 100644 --- a/sinks/natsSink.go +++ b/sinks/natsSink.go @@ -55,7 +55,7 @@ func (s *NatsSink) connect() error { func (s *NatsSink) Write(m lp.CCMetric) error { if s.client != nil { - _, err := s.encoder.Encode(m.ToPoint(s.config.MetaAsTags)) + _, err := s.encoder.Encode(m.ToPoint(s.meta_as_tags)) if err != nil { cclog.ComponentError(s.name, "Write:", err.Error()) return err @@ -97,6 +97,11 @@ func NewNatsSink(name string, config json.RawMessage) (Sink, error) { len(s.config.Database) == 0 { return nil, errors.New("not all configuration variables set required by NatsSink") } + // Create lookup map to use meta infos as tags in the output metric + s.meta_as_tags = make(map[string]bool) + for _, k := range s.config.MetaAsTags { + s.meta_as_tags[k] = true + } // Setup Influx line protocol s.buffer = &bytes.Buffer{} s.buffer.Grow(1025) @@ -105,7 +110,7 @@ func NewNatsSink(name string, config json.RawMessage) (Sink, error) { s.encoder.SetMaxLineBytes(1024) // Setup infos for connection if err := s.connect(); err != nil { - return nil, fmt.Errorf("Unable to connect: %v", err) + return nil, fmt.Errorf("unable to connect: %v", err) } return s, nil } diff --git a/sinks/sampleSink.go b/sinks/sampleSink.go index 3913a29..2a823e6 100644 --- a/sinks/sampleSink.go +++ b/sinks/sampleSink.go @@ -10,14 +10,14 @@ import ( ) type SampleSinkConfig struct { - // defines JSON tags for 'type' and 'meta_as_tags' + // defines JSON tags for 'type' and 'meta_as_tags' (string list) // See: metricSink.go defaultSinkConfig // Additional config options, for SampleSink } type SampleSink struct { - // declares elements 'name' and 'meta_as_tags' + // declares elements 'name' and 'meta_as_tags' (string to bool map!) sink config SampleSinkConfig // entry point to the SampleSinkConfig } @@ -28,6 +28,7 @@ type SampleSink struct { // Code to submit a single CCMetric to the sink func (s *SampleSink) Write(point lp.CCMetric) error { + // based on s.meta_as_tags use meta infos as tags log.Print(point) return nil } @@ -62,6 +63,12 @@ func NewSampleSink(name string, config json.RawMessage) (Sink, error) { } } + // Create lookup map to use meta infos as tags in the output metric + s.meta_as_tags = make(map[string]bool) + for _, k := range s.config.MetaAsTags { + s.meta_as_tags[k] = true + } + // Check if all required fields in the config are set // E.g. use 'len(s.config.Option) > 0' for string settings diff --git a/sinks/stdoutSink.go b/sinks/stdoutSink.go index acf2621..e091af3 100644 --- a/sinks/stdoutSink.go +++ b/sinks/stdoutSink.go @@ -63,7 +63,11 @@ func NewStdoutSink(name string, config json.RawMessage) (Sink, error) { s.output = f } } - s.meta_as_tags = s.config.MetaAsTags + // Create lookup map to use meta infos as tags in the output metric + s.meta_as_tags = make(map[string]bool) + for _, k := range s.config.MetaAsTags { + s.meta_as_tags[k] = true + } return s, nil } From 8d85bd53f15731a5f4c36722aa3f47d23886522d Mon Sep 17 00:00:00 2001 From: Thomas Gruber Date: Wed, 8 Jun 2022 15:25:40 +0200 Subject: [PATCH 5/7] Merge latest development changes to main branch (#79) * Cleanup: Remove unused code * Use Golang duration parser for 'interval' and 'duration' in main config * Update handling of LIKWID headers. Download only if not already present in the system. Fixes #73 * Units with cc-units (#64) * Add option to normalize units with cc-unit * Add unit conversion to router * Add option to change unit prefix in the router * Add to MetricRouter README * Add order of operations in router to README * Use second add_tags/del_tags only if metric gets renamed * Skip disks in DiskstatCollector that have size=0 * Check readability of sensor files in TempCollector * Fix for --once option * Rename `cpu` type to `hwthread` (#69) * Rename 'cpu' type to 'hwthread' to avoid naming clashes with MetricStore and CC-Webfrontend * Collectors in parallel (#74) * Provide info to CollectorManager whether the collector can be executed in parallel with others * Split serial and parallel collectors. Read in parallel first * Update NvidiaCollector with new metrics, MIG and NvLink support (#75) * CC topology module update (#76) * Rename CPU to hardware thread, write some comments * Do renaming in other parts * Remove CpuList and SocketList function from metricCollector. Available in ccTopology * Option to use MIG UUID as subtype-id in NvidiaCollector * Option to use MIG slice name as subtype-id in NvidiaCollector * MetricRouter: Fix JSON in README * Fix for Github Action to really use the selected version * Remove Ganglia installation in runonce Action and add Go 1.18 * Fix daemon options in init script * Add separate go.mod files to use it with deprecated 1.16 * Minor updates for Makefiles * fix string comparison * AMD ROCm SMI collector (#77) * Add collector for AMD ROCm SMI metrics * Fix import path * Fix imports * Remove Board Number * store GPU index explicitly * Remove board number from description * Use http instead of ftp to download likwid * Fix serial number in rocmCollector * Improved http sink (#78) * automatic flush in NatsSink * tweak default options of HttpSink * shorter cirt. section and retries for HttpSink * fix error handling * Remove file added by mistake. * Use http instead of ftp to download likwid * Fix serial number in rocmCollector Co-authored-by: Thomas Roehl Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Co-authored-by: Lou --- .github/ci-config.json | 4 +- .github/ci-sinks.json | 4 +- .github/workflows/runonce.yml | 42 +- Makefile | 28 +- cc-metric-collector.go | 42 +- collectors/Makefile | 36 +- collectors/README.md | 1 + collectors/beegfsmetaMetric.go | 1 + collectors/beegfsstorageMetric.go | 1 + collectors/collectorManager.go | 92 +- collectors/cpufreqCpuinfoMetric.go | 3 +- collectors/cpufreqCpuinfoMetric.md | 2 +- collectors/cpufreqMetric.go | 3 +- collectors/cpufreqMetric.md | 2 +- collectors/cpustatMetric.go | 3 +- collectors/customCmdMetric.go | 1 + collectors/diskstatMetric.go | 18 +- collectors/gpfsMetric.go | 1 + collectors/infinibandMetric.go | 1 + collectors/iostatMetric.go | 1 + collectors/ipmiMetric.go | 1 + collectors/likwidMetric.go | 3 +- collectors/likwidMetric.md | 50 +- collectors/loadavgMetric.go | 1 + collectors/lustreMetric.go | 1 + collectors/memstatMetric.go | 2 + collectors/metricCollector.go | 75 +- collectors/netstatMetric.go | 1 + collectors/nfsMetric.go | 1 + collectors/numastatsMetric.go | 1 + collectors/nvidiaMetric.go | 1360 +++++++++++++---- collectors/nvidiaMetric.md | 68 +- collectors/rocmsmiMetric.go | 319 ++++ collectors/rocmsmiMetric.md | 47 + collectors/sampleMetric.go | 11 +- collectors/tempMetric.go | 5 + collectors/topprocsMetric.go | 1 + config.json | 12 +- go.mod | 23 +- go.mod.1.16 | 16 + go.mod.1.17+ | 16 + internal/ccTopology/ccTopology.go | 108 +- .../metricAggregatorFunctions.go | 14 +- internal/metricRouter/README.md | 32 + internal/metricRouter/metricRouter.go | 41 +- scripts/cc-metric-collector.init | 2 +- scripts/likwid_perfgroup_to_cc_config.py | 2 +- sinks/httpSink.go | 107 +- sinks/influxAsyncSink.go | 18 +- sinks/influxSink.go | 102 +- sinks/natsSink.go | 76 +- 51 files changed, 2097 insertions(+), 705 deletions(-) create mode 100644 collectors/rocmsmiMetric.go create mode 100644 collectors/rocmsmiMetric.md create mode 100644 go.mod.1.16 create mode 100644 go.mod.1.17+ diff --git a/.github/ci-config.json b/.github/ci-config.json index 15b2e6f..1c4ba97 100644 --- a/.github/ci-config.json +++ b/.github/ci-config.json @@ -3,6 +3,6 @@ "collectors" : ".github/ci-collectors.json", "receivers" : ".github/ci-receivers.json", "router" : ".github/ci-router.json", - "interval": 5, - "duration": 1 + "interval": "5s", + "duration": "1s" } diff --git a/.github/ci-sinks.json b/.github/ci-sinks.json index aa8ae80..2b78305 100644 --- a/.github/ci-sinks.json +++ b/.github/ci-sinks.json @@ -1,6 +1,8 @@ { "testoutput" : { "type" : "stdout", - "meta_as_tags" : true + "meta_as_tags" : [ + "unit" + ] } } diff --git a/.github/workflows/runonce.yml b/.github/workflows/runonce.yml index be161ea..a12e002 100644 --- a/.github/workflows/runonce.yml +++ b/.github/workflows/runonce.yml @@ -7,6 +7,32 @@ name: Run Test on: push jobs: + # + # Job build-1-18 + # Build on latest Ubuntu using golang version 1.18 + # + build-1-18: + runs-on: ubuntu-latest + steps: + # See: https://github.com/marketplace/actions/checkout + # Checkout git repository and submodules + - name: Checkout + uses: actions/checkout@v2 + with: + submodules: recursive + + # See: https://github.com/marketplace/actions/setup-go-environment + - name: Setup Golang + uses: actions/setup-go@v3 + with: + go-version: '1.18.2' + + - name: Build MetricCollector + run: make + + - name: Run MetricCollector once + run: ./cc-metric-collector --once --config .github/ci-config.json + # # Job build-1-17 # Build on latest Ubuntu using golang version 1.17 @@ -23,13 +49,9 @@ jobs: # See: https://github.com/marketplace/actions/setup-go-environment - name: Setup Golang - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: '^1.17.7' - - # Install libganglia - - name: Setup Ganglia - run: sudo apt install ganglia-monitor libganglia1 + go-version: '1.17.7' - name: Build MetricCollector run: make @@ -53,13 +75,9 @@ jobs: # See: https://github.com/marketplace/actions/setup-go-environment - name: Setup Golang - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: '^1.16.7' # The version AlmaLinux 8.5 uses - - # Install libganglia - - name: Setup Ganglia - run: sudo apt install ganglia-monitor libganglia1 + go-version: '1.16.7' # The version AlmaLinux 8.5 uses - name: Build MetricCollector run: make diff --git a/Makefile b/Makefile index 0a7ad04..8a6748a 100644 --- a/Makefile +++ b/Makefile @@ -16,15 +16,23 @@ COMPONENT_DIRS := collectors \ internal/multiChanTicker BINDIR = bin +GOBIN = $(shell which go) .PHONY: all all: $(APP) $(APP): $(GOSRC) + if [ "$(shell $(GOBIN) version | cut -d' ' -f 3 | cut -d '.' -f1-2)" = "go1.16" ]; then \ + echo "1.16"; \ + cp go.mod.1.16 go.mod; \ + else \ + echo "1.17+"; \ + cp go.mod.1.17+ go.mod; \ + fi make -C collectors - go get - go build -o $(APP) $(GOSRC_APP) + $(GOBIN) get + $(GOBIN) build -o $(APP) $(GOSRC_APP) install: $(APP) @WORKSPACE=$(PREFIX) @@ -51,25 +59,25 @@ clean: .PHONY: fmt fmt: - go fmt $(GOSRC_COLLECTORS) - go fmt $(GOSRC_SINKS) - go fmt $(GOSRC_RECEIVERS) - go fmt $(GOSRC_APP) - @for F in $(GOSRC_INTERNAL); do go fmt $$F; done + $(GOBIN) fmt $(GOSRC_COLLECTORS) + $(GOBIN) fmt $(GOSRC_SINKS) + $(GOBIN) fmt $(GOSRC_RECEIVERS) + $(GOBIN) fmt $(GOSRC_APP) + @for F in $(GOSRC_INTERNAL); do $(GOBIN) fmt $$F; done # Examine Go source code and reports suspicious constructs .PHONY: vet vet: - go vet ./... + $(GOBIN) vet ./... # Run linter for the Go programming language. # Using static analysis, it finds bugs and performance issues, offers simplifications, and enforces style rules .PHONY: staticcheck staticcheck: - go install honnef.co/go/tools/cmd/staticcheck@latest - $$(go env GOPATH)/bin/staticcheck ./... + $(GOBIN) install honnef.co/go/tools/cmd/staticcheck@latest + $$($(GOBIN) env GOPATH)/bin/staticcheck ./... .ONESHELL: .PHONY: RPM diff --git a/cc-metric-collector.go b/cc-metric-collector.go index e6388df..42f7843 100644 --- a/cc-metric-collector.go +++ b/cc-metric-collector.go @@ -22,8 +22,8 @@ import ( ) type CentralConfigFile struct { - Interval int `json:"interval"` - Duration int `json:"duration"` + Interval string `json:"interval"` + Duration string `json:"duration"` CollectorConfigFile string `json:"collectors"` RouterConfigFile string `json:"router"` SinkConfigFile string `json:"sinks"` @@ -173,16 +173,36 @@ func mainFunc() int { cclog.Error("Error reading configuration file ", rcfg.CliArgs["configfile"], ": ", err.Error()) return 1 } - if rcfg.ConfigFile.Interval <= 0 || time.Duration(rcfg.ConfigFile.Interval)*time.Second <= 0 { - cclog.Error("Configuration value 'interval' must be greater than zero") + + // Properly use duration parser with inputs like '60s', '5m' or similar + if len(rcfg.ConfigFile.Interval) > 0 { + t, err := time.ParseDuration(rcfg.ConfigFile.Interval) + if err != nil { + cclog.Error("Configuration value 'interval' no valid duration") + } + rcfg.Interval = t + if rcfg.Interval == 0 { + cclog.Error("Configuration value 'interval' must be greater than zero") + return 1 + } + } + + // Properly use duration parser with inputs like '60s', '5m' or similar + if len(rcfg.ConfigFile.Duration) > 0 { + t, err := time.ParseDuration(rcfg.ConfigFile.Duration) + if err != nil { + cclog.Error("Configuration value 'duration' no valid duration") + } + rcfg.Duration = t + if rcfg.Duration == 0 { + cclog.Error("Configuration value 'duration' must be greater than zero") + return 1 + } + } + if rcfg.Duration > rcfg.Interval { + cclog.Error("The interval should be greater than duration") return 1 } - rcfg.Interval = time.Duration(rcfg.ConfigFile.Interval) * time.Second - if rcfg.ConfigFile.Duration <= 0 || time.Duration(rcfg.ConfigFile.Duration)*time.Second <= 0 { - cclog.Error("Configuration value 'duration' must be greater than zero") - return 1 - } - rcfg.Duration = time.Duration(rcfg.ConfigFile.Duration) * time.Second if len(rcfg.ConfigFile.RouterConfigFile) == 0 { cclog.Error("Metric router configuration file must be set") @@ -271,7 +291,7 @@ func mainFunc() int { // Wait until one tick has passed. This is a workaround if rcfg.CliArgs["once"] == "true" { - x := 1.2 * float64(rcfg.ConfigFile.Interval) + x := 1.2 * float64(rcfg.Interval.Seconds()) time.Sleep(time.Duration(int(x)) * time.Second) shutdownSignal <- os.Interrupt } diff --git a/collectors/Makefile b/collectors/Makefile index b07bccd..37e8e67 100644 --- a/collectors/Makefile +++ b/collectors/Makefile @@ -1,22 +1,28 @@ - -all: likwid - - # LIKWID version LIKWID_VERSION = 5.2.1 +LIKWID_INSTALLED_FOLDER=$(shell dirname $(shell which likwid-topology 2>/dev/null) 2>/dev/null) + +LIKWID_FOLDER="$(shell pwd)/likwid" + +all: $(LIKWID_FOLDER)/likwid.h .ONESHELL: -.PHONY: likwid -likwid: - INSTALL_FOLDER="$${PWD}/likwid" - BUILD_FOLDER="$${PWD}/likwidbuild" - if [ -d $${INSTALL_FOLDER} ]; then rm -r $${INSTALL_FOLDER}; fi - mkdir --parents --verbose $${INSTALL_FOLDER} $${BUILD_FOLDER} - wget -P "$${BUILD_FOLDER}" ftp://ftp.rrze.uni-erlangen.de/mirrors/likwid/likwid-$(LIKWID_VERSION).tar.gz - tar -C $${BUILD_FOLDER} -xf $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION).tar.gz - install -Dpm 0644 $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION)/src/includes/likwid*.h $${INSTALL_FOLDER}/ - install -Dpm 0644 $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION)/src/includes/bstrlib.h $${INSTALL_FOLDER}/ - rm -r $${BUILD_FOLDER} +.PHONY: $(LIKWID_FOLDER)/likwid.h +$(LIKWID_FOLDER)/likwid.h: + if [ "$(LIKWID_INSTALLED_FOLDER)" != "" ]; then \ + BASE="$(LIKWID_INSTALLED_FOLDER)/../include"; \ + mkdir -p $(LIKWID_FOLDER); \ + cp $$BASE/*.h $(LIKWID_FOLDER); \ + else \ + BUILD_FOLDER="$${PWD}/likwidbuild"; \ + if [ -d $(LIKWID_FOLDER) ]; then rm -r $(LIKWID_FOLDER); fi; \ + mkdir --parents --verbose $(LIKWID_FOLDER) $${BUILD_FOLDER}; \ + wget -P "$${BUILD_FOLDER}" http://ftp.rrze.uni-erlangen.de/mirrors/likwid/likwid-$(LIKWID_VERSION).tar.gz; \ + tar -C $${BUILD_FOLDER} -xf $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION).tar.gz; \ + install -Dpm 0644 $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION)/src/includes/likwid*.h $(LIKWID_FOLDER)/; \ + install -Dpm 0644 $${BUILD_FOLDER}/likwid-$(LIKWID_VERSION)/src/includes/bstrlib.h $(LIKWID_FOLDER)/; \ + rm -r $${BUILD_FOLDER}; \ + fi clean: diff --git a/collectors/README.md b/collectors/README.md index 3fcdd49..10e5105 100644 --- a/collectors/README.md +++ b/collectors/README.md @@ -39,6 +39,7 @@ In contrast to the configuration files for sinks and receivers, the collectors c * [`gpfs`](./gpfsMetric.md) * [`beegfs_meta`](./beegfsmetaMetric.md) * [`beegfs_storage`](./beegfsstorageMetric.md) +* [`rocm_smi`](./rocmsmiMetric.md) ## Todos diff --git a/collectors/beegfsmetaMetric.go b/collectors/beegfsmetaMetric.go index 57b1e39..a27faf2 100644 --- a/collectors/beegfsmetaMetric.go +++ b/collectors/beegfsmetaMetric.go @@ -55,6 +55,7 @@ func (m *BeegfsMetaCollector) Init(config json.RawMessage) error { m.name = "BeegfsMetaCollector" m.setup() + m.parallel = true // Set default beegfs-ctl binary m.config.Beegfs = DEFAULT_BEEGFS_CMD diff --git a/collectors/beegfsstorageMetric.go b/collectors/beegfsstorageMetric.go index cbc8314..1160664 100644 --- a/collectors/beegfsstorageMetric.go +++ b/collectors/beegfsstorageMetric.go @@ -48,6 +48,7 @@ func (m *BeegfsStorageCollector) Init(config json.RawMessage) error { m.name = "BeegfsStorageCollector" m.setup() + m.parallel = true // Set default beegfs-ctl binary m.config.Beegfs = DEFAULT_BEEGFS_CMD diff --git a/collectors/collectorManager.go b/collectors/collectorManager.go index e9ccfe7..49a9db8 100644 --- a/collectors/collectorManager.go +++ b/collectors/collectorManager.go @@ -14,39 +14,43 @@ import ( // Map of all available metric collectors var AvailableCollectors = map[string]MetricCollector{ - "likwid": new(LikwidCollector), - "loadavg": new(LoadavgCollector), - "memstat": new(MemstatCollector), - "netstat": new(NetstatCollector), - "ibstat": new(InfinibandCollector), - "lustrestat": new(LustreCollector), - "cpustat": new(CpustatCollector), - "topprocs": new(TopProcsCollector), - "nvidia": new(NvidiaCollector), - "customcmd": new(CustomCmdCollector), - "iostat": new(IOstatCollector), - "diskstat": new(DiskstatCollector), - "tempstat": new(TempCollector), - "ipmistat": new(IpmiCollector), - "gpfs": new(GpfsCollector), - "cpufreq": new(CPUFreqCollector), - "cpufreq_cpuinfo": new(CPUFreqCpuInfoCollector), - "nfs3stat": new(Nfs3Collector), - "nfs4stat": new(Nfs4Collector), - "numastats": new(NUMAStatsCollector), - "beegfs_meta": new(BeegfsMetaCollector), - "beegfs_storage": new(BeegfsStorageCollector), + "likwid": new(LikwidCollector), + "loadavg": new(LoadavgCollector), + "memstat": new(MemstatCollector), + "netstat": new(NetstatCollector), + "ibstat": new(InfinibandCollector), + "lustrestat": new(LustreCollector), + "cpustat": new(CpustatCollector), + "topprocs": new(TopProcsCollector), + "nvidia": new(NvidiaCollector), + "customcmd": new(CustomCmdCollector), + "iostat": new(IOstatCollector), + "diskstat": new(DiskstatCollector), + "tempstat": new(TempCollector), + "ipmistat": new(IpmiCollector), + "gpfs": new(GpfsCollector), + "cpufreq": new(CPUFreqCollector), + "cpufreq_cpuinfo": new(CPUFreqCpuInfoCollector), + "nfs3stat": new(Nfs3Collector), + "nfs4stat": new(Nfs4Collector), + "numastats": new(NUMAStatsCollector), + "beegfs_meta": new(BeegfsMetaCollector), + "beegfs_storage": new(BeegfsStorageCollector), + "rocm_smi": new(RocmSmiCollector), } // Metric collector manager data structure type collectorManager struct { - collectors []MetricCollector // List of metric collectors to use - output chan lp.CCMetric // Output channels - done chan bool // channel to finish / stop metric collector manager - ticker mct.MultiChanTicker // periodically ticking once each interval - duration time.Duration // duration (for metrics that measure over a given duration) - wg *sync.WaitGroup // wait group for all goroutines in cc-metric-collector - config map[string]json.RawMessage // json encoded config for collector manager + collectors []MetricCollector // List of metric collectors to read in parallel + serial []MetricCollector // List of metric collectors to read serially + output chan lp.CCMetric // Output channels + done chan bool // channel to finish / stop metric collector manager + ticker mct.MultiChanTicker // periodically ticking once each interval + duration time.Duration // duration (for metrics that measure over a given duration) + wg *sync.WaitGroup // wait group for all goroutines in cc-metric-collector + config map[string]json.RawMessage // json encoded config for collector manager + collector_wg sync.WaitGroup // internally used wait group for the parallel reading of collector + parallel_run bool // Flag whether the collectors are currently read in parallel } // Metric collector manager access functions @@ -66,6 +70,7 @@ type CollectorManager interface { // Initialization is done for all configured collectors func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error { cm.collectors = make([]MetricCollector, 0) + cm.serial = make([]MetricCollector, 0) cm.output = nil cm.done = make(chan bool) cm.wg = wg @@ -100,7 +105,11 @@ func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Durat continue } cclog.ComponentDebug("CollectorManager", "ADD COLLECTOR", collector.Name()) - cm.collectors = append(cm.collectors, collector) + if collector.Parallel() { + cm.collectors = append(cm.collectors, collector) + } else { + cm.serial = append(cm.serial, collector) + } } return nil } @@ -116,6 +125,10 @@ func (cm *collectorManager) Start() { // Collector manager is done done := func() { // close all metric collectors + if cm.parallel_run { + cm.collector_wg.Wait() + cm.parallel_run = false + } for _, c := range cm.collectors { c.Close() } @@ -130,7 +143,26 @@ func (cm *collectorManager) Start() { done() return case t := <-tick: + cm.parallel_run = true for _, c := range cm.collectors { + // Wait for done signal or execute the collector + select { + case <-cm.done: + done() + return + default: + // Read metrics from collector c via goroutine + cclog.ComponentDebug("CollectorManager", c.Name(), t) + cm.collector_wg.Add(1) + go func(myc MetricCollector) { + myc.Read(cm.duration, cm.output) + cm.collector_wg.Done() + }(c) + } + } + cm.collector_wg.Wait() + cm.parallel_run = false + for _, c := range cm.serial { // Wait for done signal or execute the collector select { case <-cm.done: diff --git a/collectors/cpufreqCpuinfoMetric.go b/collectors/cpufreqCpuinfoMetric.go index 6c3de7a..80732ff 100644 --- a/collectors/cpufreqCpuinfoMetric.go +++ b/collectors/cpufreqCpuinfoMetric.go @@ -48,6 +48,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { m.setup() m.name = "CPUFreqCpuInfoCollector" + m.parallel = true m.meta = map[string]string{ "source": m.name, "group": "CPU", @@ -150,7 +151,7 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { t.numNonHT = numNonHT t.numNonHT_int = numNonHT_int t.tagSet = map[string]string{ - "type": "cpu", + "type": "hwthread", "type-id": t.processor, "package_id": t.physicalPackageID, } diff --git a/collectors/cpufreqCpuinfoMetric.md b/collectors/cpufreqCpuinfoMetric.md index 8b0216f..de93176 100644 --- a/collectors/cpufreqCpuinfoMetric.md +++ b/collectors/cpufreqCpuinfoMetric.md @@ -4,7 +4,7 @@ "cpufreq_cpuinfo": {} ``` -The `cpufreq_cpuinfo` collector reads the clock frequency from `/proc/cpuinfo` and outputs a handful **cpu** metrics. +The `cpufreq_cpuinfo` collector reads the clock frequency from `/proc/cpuinfo` and outputs a handful **hwthread** metrics. Metrics: * `cpufreq` diff --git a/collectors/cpufreqMetric.go b/collectors/cpufreqMetric.go index 0bf6d4c..3099900 100644 --- a/collectors/cpufreqMetric.go +++ b/collectors/cpufreqMetric.go @@ -53,6 +53,7 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { m.name = "CPUFreqCollector" m.setup() + m.parallel = true if len(config) > 0 { err := json.Unmarshal(config, &m.config) if err != nil { @@ -161,7 +162,7 @@ func (m *CPUFreqCollector) Init(config json.RawMessage) error { t.numNonHT = numNonHT t.numNonHT_int = numNonHT_int t.tagSet = map[string]string{ - "type": "cpu", + "type": "hwthread", "type-id": t.processor, "package_id": t.physicalPackageID, } diff --git a/collectors/cpufreqMetric.md b/collectors/cpufreqMetric.md index b62d16e..71a6446 100644 --- a/collectors/cpufreqMetric.md +++ b/collectors/cpufreqMetric.md @@ -5,7 +5,7 @@ } ``` -The `cpufreq` collector reads the clock frequency from `/sys/devices/system/cpu/cpu*/cpufreq` and outputs a handful **cpu** metrics. +The `cpufreq` collector reads the clock frequency from `/sys/devices/system/cpu/cpu*/cpufreq` and outputs a handful **hwthread** metrics. Metrics: * `cpufreq` \ No newline at end of file diff --git a/collectors/cpustatMetric.go b/collectors/cpustatMetric.go index 556aad4..c0dcf13 100644 --- a/collectors/cpustatMetric.go +++ b/collectors/cpustatMetric.go @@ -30,6 +30,7 @@ type CpustatCollector struct { func (m *CpustatCollector) Init(config json.RawMessage) error { m.name = "CpustatCollector" m.setup() + m.parallel = true m.meta = map[string]string{"source": m.name, "group": "CPU", "unit": "Percent"} m.nodetags = map[string]string{"type": "node"} if len(config) > 0 { @@ -82,7 +83,7 @@ func (m *CpustatCollector) Init(config json.RawMessage) error { if strings.HasPrefix(linefields[0], "cpu") && strings.Compare(linefields[0], "cpu") != 0 { cpustr := strings.TrimLeft(linefields[0], "cpu") cpu, _ := strconv.Atoi(cpustr) - m.cputags[linefields[0]] = map[string]string{"type": "cpu", "type-id": fmt.Sprintf("%d", cpu)} + m.cputags[linefields[0]] = map[string]string{"type": "hwthread", "type-id": fmt.Sprintf("%d", cpu)} num_cpus++ } } diff --git a/collectors/customCmdMetric.go b/collectors/customCmdMetric.go index ec2109b..492dd48 100644 --- a/collectors/customCmdMetric.go +++ b/collectors/customCmdMetric.go @@ -33,6 +33,7 @@ type CustomCmdCollector struct { func (m *CustomCmdCollector) Init(config json.RawMessage) error { var err error m.name = "CustomCmdCollector" + m.parallel = true m.meta = map[string]string{"source": m.name, "group": "Custom"} if len(config) > 0 { err = json.Unmarshal(config, &m.config) diff --git a/collectors/diskstatMetric.go b/collectors/diskstatMetric.go index 4910c83..69ffe07 100644 --- a/collectors/diskstatMetric.go +++ b/collectors/diskstatMetric.go @@ -29,6 +29,7 @@ type DiskstatCollector struct { func (m *DiskstatCollector) Init(config json.RawMessage) error { m.name = "DiskstatCollector" + m.parallel = true m.meta = map[string]string{"source": m.name, "group": "Disk"} m.setup() if len(config) > 0 { @@ -77,11 +78,18 @@ func (m *DiskstatCollector) Read(interval time.Duration, output chan lp.CCMetric continue } path := strings.Replace(linefields[1], `\040`, " ", -1) - stat := syscall.Statfs_t{} + stat := syscall.Statfs_t{ + Blocks: 0, + Bsize: 0, + Bfree: 0, + } err := syscall.Statfs(path, &stat) if err != nil { continue } + if stat.Blocks == 0 || stat.Bsize == 0 { + continue + } tags := map[string]string{"type": "node", "device": linefields[0]} total := (stat.Blocks * uint64(stat.Bsize)) / uint64(1000000000) y, err := lp.New("disk_total", tags, m.meta, map[string]interface{}{"value": total}, time.Now()) @@ -95,9 +103,11 @@ func (m *DiskstatCollector) Read(interval time.Duration, output chan lp.CCMetric y.AddMeta("unit", "GBytes") output <- y } - perc := (100 * (total - free)) / total - if perc > part_max_used { - part_max_used = perc + if total > 0 { + perc := (100 * (total - free)) / total + if perc > part_max_used { + part_max_used = perc + } } } y, err := lp.New("part_max_used", map[string]string{"type": "node"}, m.meta, map[string]interface{}{"value": int(part_max_used)}, time.Now()) diff --git a/collectors/gpfsMetric.go b/collectors/gpfsMetric.go index ed63201..ca9affe 100644 --- a/collectors/gpfsMetric.go +++ b/collectors/gpfsMetric.go @@ -46,6 +46,7 @@ func (m *GpfsCollector) Init(config json.RawMessage) error { var err error m.name = "GpfsCollector" m.setup() + m.parallel = true // Set default mmpmon binary m.config.Mmpmon = DEFAULT_GPFS_CMD diff --git a/collectors/infinibandMetric.go b/collectors/infinibandMetric.go index 274e669..92ea911 100644 --- a/collectors/infinibandMetric.go +++ b/collectors/infinibandMetric.go @@ -54,6 +54,7 @@ func (m *InfinibandCollector) Init(config json.RawMessage) error { var err error m.name = "InfinibandCollector" m.setup() + m.parallel = true m.meta = map[string]string{ "source": m.name, "group": "Network", diff --git a/collectors/iostatMetric.go b/collectors/iostatMetric.go index ca7f33c..19b4157 100644 --- a/collectors/iostatMetric.go +++ b/collectors/iostatMetric.go @@ -37,6 +37,7 @@ type IOstatCollector struct { func (m *IOstatCollector) Init(config json.RawMessage) error { var err error m.name = "IOstatCollector" + m.parallel = true m.meta = map[string]string{"source": m.name, "group": "Disk"} m.setup() if len(config) > 0 { diff --git a/collectors/ipmiMetric.go b/collectors/ipmiMetric.go index 16b08ef..50605ac 100644 --- a/collectors/ipmiMetric.go +++ b/collectors/ipmiMetric.go @@ -34,6 +34,7 @@ type IpmiCollector struct { func (m *IpmiCollector) Init(config json.RawMessage) error { m.name = "IpmiCollector" m.setup() + m.parallel = true m.meta = map[string]string{"source": m.name, "group": "IPMI"} m.config.IpmitoolPath = string(IPMITOOL_PATH) m.config.IpmisensorsPath = string(IPMISENSORS_PATH) diff --git a/collectors/likwidMetric.go b/collectors/likwidMetric.go index f2229d1..c036415 100644 --- a/collectors/likwidMetric.go +++ b/collectors/likwidMetric.go @@ -177,6 +177,7 @@ func getBaseFreq() float64 { func (m *LikwidCollector) Init(config json.RawMessage) error { m.name = "LikwidCollector" + m.parallel = false m.initialized = false m.running = false m.config.AccessMode = LIKWID_DEF_ACCESSMODE @@ -204,7 +205,7 @@ func (m *LikwidCollector) Init(config json.RawMessage) error { m.meta = map[string]string{"group": "PerfCounter"} cclog.ComponentDebug(m.name, "Get cpulist and init maps and lists") - cpulist := topo.CpuList() + cpulist := topo.HwthreadList() m.cpulist = make([]C.int, len(cpulist)) m.cpu2tid = make(map[int]int) for i, c := range cpulist { diff --git a/collectors/likwidMetric.md b/collectors/likwidMetric.md index 2d622d1..1bb211f 100644 --- a/collectors/likwidMetric.md +++ b/collectors/likwidMetric.md @@ -19,7 +19,7 @@ The `likwid` collector is probably the most complicated collector. The LIKWID li "calc": "COUNTER0 + COUNTER1", "publish": false, "unit": "myunit", - "type": "cpu" + "type": "hwthread" } ] } @@ -30,7 +30,7 @@ The `likwid` collector is probably the most complicated collector. The LIKWID li "calc": "sum_01", "publish": true, "unit": "myunit", - "type": "cpu" + "type": "hwthread" } ] } @@ -51,15 +51,15 @@ Additional options: Hardware performance counters are scattered all over the system nowadays. A counter coveres a specific part of the system. While there are hardware thread specific counter for CPU cycles, instructions and so on, some others are specific for a whole CPU socket/package. To address that, the LikwidCollector provides the specification of a `type` for each metric. -- `cpu` : One metric per CPU hardware thread with the tags `"type" : "cpu"` and `"type-id" : "$cpu_id"` +- `hwthread` : One metric per CPU hardware thread with the tags `"type" : "hwthread"` and `"type-id" : "$hwthread_id"` - `socket` : One metric per CPU socket/package with the tags `"type" : "socket"` and `"type-id" : "$socket_id"` -**Note:** You should not specify the `socket` type for a metric that is measured at `cpu` scope and vice versa, so some kind of expert knowledge or lookup work in the [Likwid Wiki](https://github.com/RRZE-HPC/likwid/wiki) is required. Get the scope of each counter from the *Architecture* pages and as soon as one counter in a metric is socket-specific, the whole metric is socket-specific. +**Note:** You cannot specify `socket` scope for a metric that is measured at `hwthread` scope, so some kind of expert knowledge or lookup work in the [Likwid Wiki](https://github.com/RRZE-HPC/likwid/wiki) is required. Get the scope of each counter from the *Architecture* pages and as soon as one counter in a metric is socket-specific, the whole metric is socket-specific. As a guideline: -- All counters `FIXCx`, `PMCy` and `TMAz` have the scope `cpu` +- All counters `FIXCx`, `PMCy` and `TMAz` have the scope `hwthread` - All counters names containing `BOX` have the scope `socket` -- All `PWRx` counters have scope `socket`, except `"PWR1" : "RAPL_CORE_ENERGY"` has `cpu` scope (AMD Zen) +- All `PWRx` counters have scope `socket`, except `"PWR1" : "RAPL_CORE_ENERGY"` has `hwthread` scope - All `DFCx` counters have scope `socket` ### Help with the configuration @@ -90,7 +90,7 @@ $ scripts/likwid_perfgroup_to_cc_config.py ICX MEM_DP "name": "Runtime (RDTSC) [s]", "publish": true, "unit": "seconds" - "scope": "cpu" + "scope": "hwthread" }, { "..." : "..." @@ -147,20 +147,20 @@ One might think this does not happen often but often used metrics in the world o { "name": "ipc", "calc": "PMC0/PMC1", - "type": "cpu", + "type": "hwthread", "publish": true }, { "name": "flops_any", "calc": "0.000001*PMC2/time", "unit": "MFlops/s", - "type": "cpu", + "type": "hwthread", "publish": true }, { "name": "clock", "calc": "0.000001*(FIXC1/FIXC2)/inverseClock", - "type": "cpu", + "type": "hwthread", "unit": "MHz", "publish": true }, @@ -219,3 +219,33 @@ One might think this does not happen often but often used metrics in the world o } ``` +### How to get the eventsets and metrics from LIKWID + +The `likwid` collector reads hardware performance counters at a **hwthread** and **socket** level. The configuration looks quite complicated but it is basically copy&paste from [LIKWID's performance groups](https://github.com/RRZE-HPC/likwid/tree/master/groups). The collector made multiple iterations and tried to use the performance groups but it lacked flexibility. The current way of configuration provides most flexibility. + +The logic is as following: There are multiple eventsets, each consisting of a list of counters+events and a list of metrics. If you compare a common performance group with the example setting above, there is not much difference: +``` +EVENTSET -> "events": { +FIXC1 ACTUAL_CPU_CLOCK -> "FIXC1": "ACTUAL_CPU_CLOCK", +FIXC2 MAX_CPU_CLOCK -> "FIXC2": "MAX_CPU_CLOCK", +PMC0 RETIRED_INSTRUCTIONS -> "PMC0" : "RETIRED_INSTRUCTIONS", +PMC1 CPU_CLOCKS_UNHALTED -> "PMC1" : "CPU_CLOCKS_UNHALTED", +PMC2 RETIRED_SSE_AVX_FLOPS_ALL -> "PMC2": "RETIRED_SSE_AVX_FLOPS_ALL", +PMC3 MERGE -> "PMC3": "MERGE", + -> } +``` + +The metrics are following the same procedure: + +``` +METRICS -> "metrics": [ +IPC PMC0/PMC1 -> { + -> "name" : "IPC", + -> "calc" : "PMC0/PMC1", + -> "scope": "hwthread", + -> "publish": true + -> } + -> ] +``` + +The script `scripts/likwid_perfgroup_to_cc_config.py` might help you. diff --git a/collectors/loadavgMetric.go b/collectors/loadavgMetric.go index 3859721..58fb102 100644 --- a/collectors/loadavgMetric.go +++ b/collectors/loadavgMetric.go @@ -36,6 +36,7 @@ type LoadavgCollector struct { func (m *LoadavgCollector) Init(config json.RawMessage) error { m.name = "LoadavgCollector" + m.parallel = true m.setup() if len(config) > 0 { err := json.Unmarshal(config, &m.config) diff --git a/collectors/lustreMetric.go b/collectors/lustreMetric.go index d5a96e4..eade2ca 100644 --- a/collectors/lustreMetric.go +++ b/collectors/lustreMetric.go @@ -288,6 +288,7 @@ var LustreDeriveMetrics = []LustreMetricDefinition{ func (m *LustreCollector) Init(config json.RawMessage) error { var err error m.name = "LustreCollector" + m.parallel = true if len(config) > 0 { err = json.Unmarshal(config, &m.config) if err != nil { diff --git a/collectors/memstatMetric.go b/collectors/memstatMetric.go index c6c7f34..9841a01 100644 --- a/collectors/memstatMetric.go +++ b/collectors/memstatMetric.go @@ -81,6 +81,7 @@ func getStats(filename string) map[string]MemstatStats { func (m *MemstatCollector) Init(config json.RawMessage) error { var err error m.name = "MemstatCollector" + m.parallel = true m.config.NodeStats = true m.config.NumaStats = false if len(config) > 0 { @@ -159,6 +160,7 @@ func (m *MemstatCollector) Init(config json.RawMessage) error { func (m *MemstatCollector) Read(interval time.Duration, output chan lp.CCMetric) { if !m.init { + cclog.ComponentPrint(m.name, "Here") return } diff --git a/collectors/metricCollector.go b/collectors/metricCollector.go index 7c04e90..4d52571 100644 --- a/collectors/metricCollector.go +++ b/collectors/metricCollector.go @@ -3,27 +3,25 @@ package collectors import ( "encoding/json" "fmt" - "io/ioutil" - "log" - "strconv" - "strings" "time" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) type MetricCollector interface { - Name() string // Name of the metric collector - Init(config json.RawMessage) error // Initialize metric collector - Initialized() bool // Is metric collector initialized? + Name() string // Name of the metric collector + Init(config json.RawMessage) error // Initialize metric collector + Initialized() bool // Is metric collector initialized? + Parallel() bool Read(duration time.Duration, output chan lp.CCMetric) // Read metrics from metric collector Close() // Close / finish metric collector } type metricCollector struct { - name string // name of the metric - init bool // is metric collector initialized? - meta map[string]string // static meta data tags + name string // name of the metric + init bool // is metric collector initialized? + parallel bool // can the metric collector be executed in parallel with others + meta map[string]string // static meta data tags } // Name returns the name of the metric collector @@ -31,6 +29,11 @@ func (c *metricCollector) Name() string { return c.name } +// Name returns the name of the metric collector +func (c *metricCollector) Parallel() bool { + return c.parallel +} + // Setup is for future use func (c *metricCollector) setup() error { return nil @@ -65,58 +68,6 @@ func stringArrayContains(array []string, str string) (int, bool) { return -1, false } -// SocketList returns the list of physical sockets as read from /proc/cpuinfo -func SocketList() []int { - buffer, err := ioutil.ReadFile("/proc/cpuinfo") - if err != nil { - log.Print(err) - return nil - } - ll := strings.Split(string(buffer), "\n") - var packs []int - for _, line := range ll { - if strings.HasPrefix(line, "physical id") { - lv := strings.Fields(line) - id, err := strconv.ParseInt(lv[3], 10, 32) - if err != nil { - log.Print(err) - return packs - } - _, found := intArrayContains(packs, int(id)) - if !found { - packs = append(packs, int(id)) - } - } - } - return packs -} - -// CpuList returns the list of physical CPUs (in contrast to logical CPUs) as read from /proc/cpuinfo -func CpuList() []int { - buffer, err := ioutil.ReadFile("/proc/cpuinfo") - if err != nil { - log.Print(err) - return nil - } - ll := strings.Split(string(buffer), "\n") - var cpulist []int - for _, line := range ll { - if strings.HasPrefix(line, "processor") { - lv := strings.Fields(line) - id, err := strconv.ParseInt(lv[2], 10, 32) - if err != nil { - log.Print(err) - return cpulist - } - _, found := intArrayContains(cpulist, int(id)) - if !found { - cpulist = append(cpulist, int(id)) - } - } - } - return cpulist -} - // RemoveFromStringList removes the string r from the array of strings s // If r is not contained in the array an error is returned func RemoveFromStringList(s []string, r string) ([]string, error) { diff --git a/collectors/netstatMetric.go b/collectors/netstatMetric.go index d171d4b..8cfb34e 100644 --- a/collectors/netstatMetric.go +++ b/collectors/netstatMetric.go @@ -39,6 +39,7 @@ type NetstatCollector struct { func (m *NetstatCollector) Init(config json.RawMessage) error { m.name = "NetstatCollector" + m.parallel = true m.setup() m.lastTimestamp = time.Now() diff --git a/collectors/nfsMetric.go b/collectors/nfsMetric.go index c511b0d..6b15784 100644 --- a/collectors/nfsMetric.go +++ b/collectors/nfsMetric.go @@ -114,6 +114,7 @@ func (m *nfsCollector) MainInit(config json.RawMessage) error { m.data = make(map[string]NfsCollectorData) m.initStats() m.init = true + m.parallel = true return nil } diff --git a/collectors/numastatsMetric.go b/collectors/numastatsMetric.go index 52a2638..f65a019 100644 --- a/collectors/numastatsMetric.go +++ b/collectors/numastatsMetric.go @@ -54,6 +54,7 @@ func (m *NUMAStatsCollector) Init(config json.RawMessage) error { } m.name = "NUMAStatsCollector" + m.parallel = true m.setup() m.meta = map[string]string{ "source": m.name, diff --git a/collectors/nvidiaMetric.go b/collectors/nvidiaMetric.go index 24f0855..458ecd4 100644 --- a/collectors/nvidiaMetric.go +++ b/collectors/nvidiaMetric.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "strings" "time" cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" @@ -13,22 +14,30 @@ import ( ) type NvidiaCollectorConfig struct { - ExcludeMetrics []string `json:"exclude_metrics,omitempty"` - ExcludeDevices []string `json:"exclude_devices,omitempty"` - AddPciInfoTag bool `json:"add_pci_info_tag,omitempty"` + ExcludeMetrics []string `json:"exclude_metrics,omitempty"` + ExcludeDevices []string `json:"exclude_devices,omitempty"` + AddPciInfoTag bool `json:"add_pci_info_tag,omitempty"` + UsePciInfoAsTypeId bool `json:"use_pci_info_as_type_id,omitempty"` + AddUuidMeta bool `json:"add_uuid_meta,omitempty"` + AddBoardNumberMeta bool `json:"add_board_number_meta,omitempty"` + AddSerialMeta bool `json:"add_serial_meta,omitempty"` + ProcessMigDevices bool `json:"process_mig_devices,omitempty"` + UseUuidForMigDevices bool `json:"use_uuid_for_mig_device,omitempty"` + UseSliceForMigDevices bool `json:"use_slice_for_mig_device,omitempty"` } type NvidiaCollectorDevice struct { device nvml.Device excludeMetrics map[string]bool tags map[string]string + meta map[string]string } type NvidiaCollector struct { metricCollector - num_gpus int config NvidiaCollectorConfig gpus []NvidiaCollectorDevice + num_gpus int } func (m *NvidiaCollector) CatchPanic() { @@ -42,6 +51,10 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { var err error m.name = "NvidiaCollector" m.config.AddPciInfoTag = false + m.config.UsePciInfoAsTypeId = false + m.config.ProcessMigDevices = false + m.config.UseUuidForMigDevices = false + m.config.UseSliceForMigDevices = false m.setup() if len(config) > 0 { err = json.Unmarshal(config, &m.config) @@ -54,7 +67,6 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { "group": "Nvidia", } - m.num_gpus = 0 defer m.CatchPanic() // Initialize NVIDIA Management Library (NVML) @@ -74,13 +86,14 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { } // For all GPUs + idx := 0 m.gpus = make([]NvidiaCollectorDevice, num_gpus) for i := 0; i < num_gpus; i++ { - g := &m.gpus[i] - // Skip excluded devices + // Skip excluded devices by ID str_i := fmt.Sprintf("%d", i) if _, skip := stringArrayContains(m.config.ExcludeDevices, str_i); skip { + cclog.ComponentDebug(m.name, "Skipping excluded device", str_i) continue } @@ -89,14 +102,85 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { if ret != nvml.SUCCESS { err = errors.New(nvml.ErrorString(ret)) cclog.ComponentError(m.name, "Unable to get device at index", i, ":", err.Error()) - return err + continue } + + // Get device's PCI info + pciInfo, ret := nvml.DeviceGetPciInfo(device) + if ret != nvml.SUCCESS { + err = errors.New(nvml.ErrorString(ret)) + cclog.ComponentError(m.name, "Unable to get PCI info for device at index", i, ":", err.Error()) + continue + } + // Create PCI ID in the common format used by the NVML. + pci_id := fmt.Sprintf( + nvml.DEVICE_PCI_BUS_ID_FMT, + pciInfo.Domain, + pciInfo.Bus, + pciInfo.Device) + + // Skip excluded devices specified by PCI ID + if _, skip := stringArrayContains(m.config.ExcludeDevices, pci_id); skip { + cclog.ComponentDebug(m.name, "Skipping excluded device", pci_id) + continue + } + + // Select which value to use as 'type-id'. + // The PCI ID is commonly required in SLURM environments because the + // numberic IDs used by SLURM and the ones used by NVML might differ + // depending on the job type. The PCI ID is more reliable but is commonly + // not recorded for a job, so it must be added manually in prologue or epilogue + // e.g. to the comment field + tid := str_i + if m.config.UsePciInfoAsTypeId { + tid = pci_id + } + + // Now we got all infos together, populate the device list + g := &m.gpus[idx] + + // Add device handle g.device = device // Add tags g.tags = map[string]string{ "type": "accelerator", - "type-id": str_i, + "type-id": tid, + } + + // Add PCI info as tag if not already used as 'type-id' + if m.config.AddPciInfoTag && !m.config.UsePciInfoAsTypeId { + g.tags["pci_identifier"] = pci_id + } + + g.meta = map[string]string{ + "source": m.name, + "group": "Nvidia", + } + + if m.config.AddBoardNumberMeta { + board, ret := nvml.DeviceGetBoardPartNumber(device) + if ret != nvml.SUCCESS { + cclog.ComponentError(m.name, "Unable to get boart part number for device at index", i, ":", err.Error()) + } else { + g.meta["board_number"] = board + } + } + if m.config.AddSerialMeta { + serial, ret := nvml.DeviceGetSerial(device) + if ret != nvml.SUCCESS { + cclog.ComponentError(m.name, "Unable to get serial number for device at index", i, ":", err.Error()) + } else { + g.meta["serial"] = serial + } + } + if m.config.AddUuidMeta { + uuid, ret := nvml.DeviceGetUUID(device) + if ret != nvml.SUCCESS { + cclog.ComponentError(m.name, "Unable to get UUID for device at index", i, ":", err.Error()) + } else { + g.meta["uuid"] = uuid + } } // Add excluded metrics @@ -105,363 +189,1003 @@ func (m *NvidiaCollector) Init(config json.RawMessage) error { g.excludeMetrics[e] = true } - // Add PCI info as tag - if m.config.AddPciInfoTag { - pciInfo, ret := nvml.DeviceGetPciInfo(g.device) - if ret != nvml.SUCCESS { - err = errors.New(nvml.ErrorString(ret)) - cclog.ComponentError(m.name, "Unable to get PCI info for device at index", i, ":", err.Error()) - return err - } - g.tags["pci_identifier"] = fmt.Sprintf( - "%08X:%02X:%02X.0", - pciInfo.Domain, - pciInfo.Bus, - pciInfo.Device) - } + // Increment the index for the next device + idx++ } + m.num_gpus = idx m.init = true return nil } +func readMemoryInfo(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_fb_mem_total"] || !device.excludeMetrics["nv_fb_mem_used"] || !device.excludeMetrics["nv_fb_mem_reserved"] { + var total uint64 + var used uint64 + var reserved uint64 = 0 + var v2 bool = false + meminfo, ret := nvml.DeviceGetMemoryInfo(device.device) + if ret != nvml.SUCCESS { + err := errors.New(nvml.ErrorString(ret)) + return err + } + total = meminfo.Total + used = meminfo.Used + + if !device.excludeMetrics["nv_fb_mem_total"] { + t := float64(total) / (1024 * 1024) + y, err := lp.New("nv_fb_mem_total", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "MByte") + output <- y + } + } + + if !device.excludeMetrics["nv_fb_mem_used"] { + f := float64(used) / (1024 * 1024) + y, err := lp.New("nv_fb_mem_used", device.tags, device.meta, map[string]interface{}{"value": f}, time.Now()) + if err == nil { + y.AddMeta("unit", "MByte") + output <- y + } + } + + if v2 && !device.excludeMetrics["nv_fb_mem_reserved"] { + r := float64(reserved) / (1024 * 1024) + y, err := lp.New("nv_fb_mem_reserved", device.tags, device.meta, map[string]interface{}{"value": r}, time.Now()) + if err == nil { + y.AddMeta("unit", "MByte") + output <- y + } + } + } + return nil +} + +func readBarMemoryInfo(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_bar1_mem_total"] || !device.excludeMetrics["nv_bar1_mem_used"] { + meminfo, ret := nvml.DeviceGetBAR1MemoryInfo(device.device) + if ret != nvml.SUCCESS { + err := errors.New(nvml.ErrorString(ret)) + return err + } + if !device.excludeMetrics["nv_bar1_mem_total"] { + t := float64(meminfo.Bar1Total) / (1024 * 1024) + y, err := lp.New("nv_bar1_mem_total", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "MByte") + output <- y + } + } + if !device.excludeMetrics["nv_bar1_mem_used"] { + t := float64(meminfo.Bar1Used) / (1024 * 1024) + y, err := lp.New("nv_bar1_mem_used", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "MByte") + output <- y + } + } + } + return nil +} + +func readUtilization(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + isMig, ret := nvml.DeviceIsMigDeviceHandle(device.device) + if ret != nvml.SUCCESS { + err := errors.New(nvml.ErrorString(ret)) + return err + } + if !isMig { + return nil + } + + if !device.excludeMetrics["nv_util"] || !device.excludeMetrics["nv_mem_util"] { + // Retrieves the current utilization rates for the device's major subsystems. + // + // Available utilization rates + // * Gpu: Percent of time over the past sample period during which one or more kernels was executing on the GPU. + // * Memory: Percent of time over the past sample period during which global (device) memory was being read or written + // + // Note: + // * During driver initialization when ECC is enabled one can see high GPU and Memory Utilization readings. + // This is caused by ECC Memory Scrubbing mechanism that is performed during driver initialization. + // * On MIG-enabled GPUs, querying device utilization rates is not currently supported. + util, ret := nvml.DeviceGetUtilizationRates(device.device) + if ret == nvml.SUCCESS { + if !device.excludeMetrics["nv_util"] { + y, err := lp.New("nv_util", device.tags, device.meta, map[string]interface{}{"value": float64(util.Gpu)}, time.Now()) + if err == nil { + y.AddMeta("unit", "%") + output <- y + } + } + if !device.excludeMetrics["nv_mem_util"] { + y, err := lp.New("nv_mem_util", device.tags, device.meta, map[string]interface{}{"value": float64(util.Memory)}, time.Now()) + if err == nil { + y.AddMeta("unit", "%") + output <- y + } + } + } + } + return nil +} + +func readTemp(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_temp"] { + // Retrieves the current temperature readings for the device, in degrees C. + // + // Available temperature sensors: + // * TEMPERATURE_GPU: Temperature sensor for the GPU die. + // * NVML_TEMPERATURE_COUNT + temp, ret := nvml.DeviceGetTemperature(device.device, nvml.TEMPERATURE_GPU) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_temp", device.tags, device.meta, map[string]interface{}{"value": float64(temp)}, time.Now()) + if err == nil { + y.AddMeta("unit", "degC") + output <- y + } + } + } + return nil +} + +func readFan(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_fan"] { + // Retrieves the intended operating speed of the device's fan. + // + // Note: The reported speed is the intended fan speed. + // If the fan is physically blocked and unable to spin, the output will not match the actual fan speed. + // + // For all discrete products with dedicated fans. + // + // The fan speed is expressed as a percentage of the product's maximum noise tolerance fan speed. + // This value may exceed 100% in certain cases. + fan, ret := nvml.DeviceGetFanSpeed(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_fan", device.tags, device.meta, map[string]interface{}{"value": float64(fan)}, time.Now()) + if err == nil { + y.AddMeta("unit", "%") + output <- y + } + } + } + return nil +} + +// func readFans(device NvidiaCollectorDevice, output chan lp.CCMetric) error { +// if !device.excludeMetrics["nv_fan"] { +// numFans, ret := nvml.DeviceGetNumFans(device.device) +// if ret == nvml.SUCCESS { +// for i := 0; i < numFans; i++ { +// fan, ret := nvml.DeviceGetFanSpeed_v2(device.device, i) +// if ret == nvml.SUCCESS { +// y, err := lp.New("nv_fan", device.tags, device.meta, map[string]interface{}{"value": float64(fan)}, time.Now()) +// if err == nil { +// y.AddMeta("unit", "%") +// y.AddTag("stype", "fan") +// y.AddTag("stype-id", fmt.Sprintf("%d", i)) +// output <- y +// } +// } +// } +// } +// } +// return nil +// } + +func readEccMode(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_ecc_mode"] { + // Retrieves the current and pending ECC modes for the device. + // + // For Fermi or newer fully supported devices. Only applicable to devices with ECC. + // Requires NVML_INFOROM_ECC version 1.0 or higher. + // + // Changing ECC modes requires a reboot. + // The "pending" ECC mode refers to the target mode following the next reboot. + _, ecc_pend, ret := nvml.DeviceGetEccMode(device.device) + if ret == nvml.SUCCESS { + var y lp.CCMetric + var err error + switch ecc_pend { + case nvml.FEATURE_DISABLED: + y, err = lp.New("nv_ecc_mode", device.tags, device.meta, map[string]interface{}{"value": "OFF"}, time.Now()) + case nvml.FEATURE_ENABLED: + y, err = lp.New("nv_ecc_mode", device.tags, device.meta, map[string]interface{}{"value": "ON"}, time.Now()) + default: + y, err = lp.New("nv_ecc_mode", device.tags, device.meta, map[string]interface{}{"value": "UNKNOWN"}, time.Now()) + } + if err == nil { + output <- y + } + } else if ret == nvml.ERROR_NOT_SUPPORTED { + y, err := lp.New("nv_ecc_mode", device.tags, device.meta, map[string]interface{}{"value": "N/A"}, time.Now()) + if err == nil { + output <- y + } + } + } + return nil +} + +func readPerfState(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_perf_state"] { + // Retrieves the current performance state for the device. + // + // Allowed PStates: + // 0: Maximum Performance. + // .. + // 15: Minimum Performance. + // 32: Unknown performance state. + pState, ret := nvml.DeviceGetPerformanceState(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_perf_state", device.tags, device.meta, map[string]interface{}{"value": fmt.Sprintf("P%d", int(pState))}, time.Now()) + if err == nil { + output <- y + } + } + } + return nil +} + +func readPowerUsage(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_power_usage"] { + // Retrieves power usage for this GPU in milliwatts and its associated circuitry (e.g. memory) + // + // On Fermi and Kepler GPUs the reading is accurate to within +/- 5% of current power draw. + // + // It is only available if power management mode is supported + mode, ret := nvml.DeviceGetPowerManagementMode(device.device) + if ret != nvml.SUCCESS { + return nil + } + if mode == nvml.FEATURE_ENABLED { + power, ret := nvml.DeviceGetPowerUsage(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_power_usage", device.tags, device.meta, map[string]interface{}{"value": float64(power) / 1000}, time.Now()) + if err == nil { + y.AddMeta("unit", "watts") + output <- y + } + } + } + } + return nil +} + +func readClocks(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + // Retrieves the current clock speeds for the device. + // + // Available clock information: + // * CLOCK_GRAPHICS: Graphics clock domain. + // * CLOCK_SM: Streaming Multiprocessor clock domain. + // * CLOCK_MEM: Memory clock domain. + if !device.excludeMetrics["nv_graphics_clock"] { + graphicsClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_GRAPHICS) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_graphics_clock", device.tags, device.meta, map[string]interface{}{"value": float64(graphicsClock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + + if !device.excludeMetrics["nv_sm_clock"] { + smCock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_SM) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_sm_clock", device.tags, device.meta, map[string]interface{}{"value": float64(smCock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + + if !device.excludeMetrics["nv_mem_clock"] { + memClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_MEM) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_mem_clock", device.tags, device.meta, map[string]interface{}{"value": float64(memClock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + if !device.excludeMetrics["nv_video_clock"] { + memClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_VIDEO) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_video_clock", device.tags, device.meta, map[string]interface{}{"value": float64(memClock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + return nil +} + +func readMaxClocks(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + // Retrieves the maximum clock speeds for the device. + // + // Available clock information: + // * CLOCK_GRAPHICS: Graphics clock domain. + // * CLOCK_SM: Streaming multiprocessor clock domain. + // * CLOCK_MEM: Memory clock domain. + // * CLOCK_VIDEO: Video encoder/decoder clock domain. + // * CLOCK_COUNT: Count of clock types. + // + // Note: + /// On GPUs from Fermi family current P0 clocks (reported by nvmlDeviceGetClockInfo) can differ from max clocks by few MHz. + if !device.excludeMetrics["nv_max_graphics_clock"] { + max_gclk, ret := nvml.DeviceGetMaxClockInfo(device.device, nvml.CLOCK_GRAPHICS) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_max_graphics_clock", device.tags, device.meta, map[string]interface{}{"value": float64(max_gclk)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + + if !device.excludeMetrics["nv_max_sm_clock"] { + maxSmClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_SM) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_max_sm_clock", device.tags, device.meta, map[string]interface{}{"value": float64(maxSmClock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + + if !device.excludeMetrics["nv_max_mem_clock"] { + maxMemClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_MEM) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_max_mem_clock", device.tags, device.meta, map[string]interface{}{"value": float64(maxMemClock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + + if !device.excludeMetrics["nv_max_video_clock"] { + maxMemClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_VIDEO) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_max_video_clock", device.tags, device.meta, map[string]interface{}{"value": float64(maxMemClock)}, time.Now()) + if err == nil { + y.AddMeta("unit", "MHz") + output <- y + } + } + } + return nil +} + +func readEccErrors(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_ecc_uncorrected_error"] { + // Retrieves the total ECC error counts for the device. + // + // For Fermi or newer fully supported devices. + // Only applicable to devices with ECC. + // Requires NVML_INFOROM_ECC version 1.0 or higher. + // Requires ECC Mode to be enabled. + // + // The total error count is the sum of errors across each of the separate memory systems, + // i.e. the total set of errors across the entire device. + ecc_db, ret := nvml.DeviceGetTotalEccErrors(device.device, nvml.MEMORY_ERROR_TYPE_UNCORRECTED, nvml.AGGREGATE_ECC) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_ecc_uncorrected_error", device.tags, device.meta, map[string]interface{}{"value": float64(ecc_db)}, time.Now()) + if err == nil { + output <- y + } + } + } + if !device.excludeMetrics["nv_ecc_corrected_error"] { + ecc_sb, ret := nvml.DeviceGetTotalEccErrors(device.device, nvml.MEMORY_ERROR_TYPE_CORRECTED, nvml.AGGREGATE_ECC) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_ecc_corrected_error", device.tags, device.meta, map[string]interface{}{"value": float64(ecc_sb)}, time.Now()) + if err == nil { + output <- y + } + } + } + return nil +} + +func readPowerLimit(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_power_max_limit"] { + // Retrieves the power management limit associated with this device. + // + // For Fermi or newer fully supported devices. + // + // The power limit defines the upper boundary for the card's power draw. + // If the card's total power draw reaches this limit the power management algorithm kicks in. + pwr_limit, ret := nvml.DeviceGetPowerManagementLimit(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_power_max_limit", device.tags, device.meta, map[string]interface{}{"value": float64(pwr_limit) / 1000}, time.Now()) + if err == nil { + y.AddMeta("unit", "watts") + output <- y + } + } + } + return nil +} + +func readEncUtilization(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + isMig, ret := nvml.DeviceIsMigDeviceHandle(device.device) + if ret != nvml.SUCCESS { + err := errors.New(nvml.ErrorString(ret)) + return err + } + if !isMig { + return nil + } + if !device.excludeMetrics["nv_encoder_util"] { + // Retrieves the current utilization and sampling size in microseconds for the Encoder + // + // For Kepler or newer fully supported devices. + // + // Note: On MIG-enabled GPUs, querying encoder utilization is not currently supported. + enc_util, _, ret := nvml.DeviceGetEncoderUtilization(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_encoder_util", device.tags, device.meta, map[string]interface{}{"value": float64(enc_util)}, time.Now()) + if err == nil { + y.AddMeta("unit", "%") + output <- y + } + } + } + return nil +} + +func readDecUtilization(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + isMig, ret := nvml.DeviceIsMigDeviceHandle(device.device) + if ret != nvml.SUCCESS { + err := errors.New(nvml.ErrorString(ret)) + return err + } + if !isMig { + return nil + } + if !device.excludeMetrics["nv_decoder_util"] { + // Retrieves the current utilization and sampling size in microseconds for the Encoder + // + // For Kepler or newer fully supported devices. + // + // Note: On MIG-enabled GPUs, querying encoder utilization is not currently supported. + dec_util, _, ret := nvml.DeviceGetDecoderUtilization(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_decoder_util", device.tags, device.meta, map[string]interface{}{"value": float64(dec_util)}, time.Now()) + if err == nil { + y.AddMeta("unit", "%") + output <- y + } + } + } + return nil +} + +func readRemappedRows(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_remapped_rows_corrected"] || + !device.excludeMetrics["nv_remapped_rows_uncorrected"] || + !device.excludeMetrics["nv_remapped_rows_pending"] || + !device.excludeMetrics["nv_remapped_rows_failure"] { + // Get number of remapped rows. The number of rows reported will be based on the cause of the remapping. + // isPending indicates whether or not there are pending remappings. + // A reset will be required to actually remap the row. + // failureOccurred will be set if a row remapping ever failed in the past. + // A pending remapping won't affect future work on the GPU since error-containment and dynamic page blacklisting will take care of that. + // + // For Ampere or newer fully supported devices. + // + // Note: On MIG-enabled GPUs with active instances, querying the number of remapped rows is not supported + corrected, uncorrected, pending, failure, ret := nvml.DeviceGetRemappedRows(device.device) + if ret == nvml.SUCCESS { + if !device.excludeMetrics["nv_remapped_rows_corrected"] { + y, err := lp.New("nv_remapped_rows_corrected", device.tags, device.meta, map[string]interface{}{"value": float64(corrected)}, time.Now()) + if err == nil { + output <- y + } + } + if !device.excludeMetrics["nv_remapped_rows_uncorrected"] { + y, err := lp.New("nv_remapped_rows_corrected", device.tags, device.meta, map[string]interface{}{"value": float64(uncorrected)}, time.Now()) + if err == nil { + output <- y + } + } + if !device.excludeMetrics["nv_remapped_rows_pending"] { + var p int = 0 + if pending { + p = 1 + } + y, err := lp.New("nv_remapped_rows_pending", device.tags, device.meta, map[string]interface{}{"value": p}, time.Now()) + if err == nil { + output <- y + } + } + if !device.excludeMetrics["nv_remapped_rows_failure"] { + var f int = 0 + if failure { + f = 1 + } + y, err := lp.New("nv_remapped_rows_failure", device.tags, device.meta, map[string]interface{}{"value": f}, time.Now()) + if err == nil { + output <- y + } + } + } + } + return nil +} + +func readProcessCounts(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + if !device.excludeMetrics["nv_compute_processes"] { + // Get information about processes with a compute context on a device + // + // For Fermi &tm; or newer fully supported devices. + // + // This function returns information only about compute running processes (e.g. CUDA application which have + // active context). Any graphics applications (e.g. using OpenGL, DirectX) won't be listed by this function. + // + // To query the current number of running compute processes, call this function with *infoCount = 0. The + // return code will be NVML_ERROR_INSUFFICIENT_SIZE, or NVML_SUCCESS if none are running. For this call + // \a infos is allowed to be NULL. + // + // The usedGpuMemory field returned is all of the memory used by the application. + // + // Keep in mind that information returned by this call is dynamic and the number of elements might change in + // time. Allocate more space for \a infos table in case new compute processes are spawned. + // + // @note In MIG mode, if device handle is provided, the API returns aggregate information, only if + // the caller has appropriate privileges. Per-instance information can be queried by using + // specific MIG device handles. + // Querying per-instance information using MIG device handles is not supported if the device is in vGPU Host virtualization mode. + procList, ret := nvml.DeviceGetComputeRunningProcesses(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_compute_processes", device.tags, device.meta, map[string]interface{}{"value": len(procList)}, time.Now()) + if err == nil { + output <- y + } + } + } + if !device.excludeMetrics["nv_graphics_processes"] { + // Get information about processes with a graphics context on a device + // + // For Kepler &tm; or newer fully supported devices. + // + // This function returns information only about graphics based processes + // (eg. applications using OpenGL, DirectX) + // + // To query the current number of running graphics processes, call this function with *infoCount = 0. The + // return code will be NVML_ERROR_INSUFFICIENT_SIZE, or NVML_SUCCESS if none are running. For this call + // \a infos is allowed to be NULL. + // + // The usedGpuMemory field returned is all of the memory used by the application. + // + // Keep in mind that information returned by this call is dynamic and the number of elements might change in + // time. Allocate more space for \a infos table in case new graphics processes are spawned. + // + // @note In MIG mode, if device handle is provided, the API returns aggregate information, only if + // the caller has appropriate privileges. Per-instance information can be queried by using + // specific MIG device handles. + // Querying per-instance information using MIG device handles is not supported if the device is in vGPU Host virtualization mode. + procList, ret := nvml.DeviceGetGraphicsRunningProcesses(device.device) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_graphics_processes", device.tags, device.meta, map[string]interface{}{"value": len(procList)}, time.Now()) + if err == nil { + output <- y + } + } + } + // if !device.excludeMetrics["nv_mps_compute_processes"] { + // // Get information about processes with a MPS compute context on a device + // // + // // For Volta &tm; or newer fully supported devices. + // // + // // This function returns information only about compute running processes (e.g. CUDA application which have + // // active context) utilizing MPS. Any graphics applications (e.g. using OpenGL, DirectX) won't be listed by + // // this function. + // // + // // To query the current number of running compute processes, call this function with *infoCount = 0. The + // // return code will be NVML_ERROR_INSUFFICIENT_SIZE, or NVML_SUCCESS if none are running. For this call + // // \a infos is allowed to be NULL. + // // + // // The usedGpuMemory field returned is all of the memory used by the application. + // // + // // Keep in mind that information returned by this call is dynamic and the number of elements might change in + // // time. Allocate more space for \a infos table in case new compute processes are spawned. + // // + // // @note In MIG mode, if device handle is provided, the API returns aggregate information, only if + // // the caller has appropriate privileges. Per-instance information can be queried by using + // // specific MIG device handles. + // // Querying per-instance information using MIG device handles is not supported if the device is in vGPU Host virtualization mode. + // procList, ret := nvml.DeviceGetMPSComputeRunningProcesses(device.device) + // if ret == nvml.SUCCESS { + // y, err := lp.New("nv_mps_compute_processes", device.tags, device.meta, map[string]interface{}{"value": len(procList)}, time.Now()) + // if err == nil { + // output <- y + // } + // } + // } + return nil +} + +func readViolationStats(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + var violTime nvml.ViolationTime + var ret nvml.Return + + // Gets the duration of time during which the device was throttled (lower than requested clocks) due to power + // or thermal constraints. + // + // The method is important to users who are tying to understand if their GPUs throttle at any point during their applications. The + // difference in violation times at two different reference times gives the indication of GPU throttling event. + // + // Violation for thermal capping is not supported at this time. + // + // For Kepler or newer fully supported devices. + + if !device.excludeMetrics["nv_violation_power"] { + // How long did power violations cause the GPU to be below application clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_POWER) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_power", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_thermal"] { + // How long did thermal violations cause the GPU to be below application clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_THERMAL) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_thermal", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_sync_boost"] { + // How long did sync boost cause the GPU to be below application clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_SYNC_BOOST) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_sync_boost", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_board_limit"] { + // How long did the board limit cause the GPU to be below application clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_BOARD_LIMIT) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_board_limit", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_low_util"] { + // How long did low utilization cause the GPU to be below application clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_LOW_UTILIZATION) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_low_util", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_reliability"] { + // How long did the board reliability limit cause the GPU to be below application clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_RELIABILITY) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_reliability", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_below_app_clock"] { + // Total time the GPU was held below application clocks by any limiter (all of above) + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_TOTAL_APP_CLOCKS) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_below_app_clock", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + if !device.excludeMetrics["nv_violation_below_base_clock"] { + // Total time the GPU was held below base clocks + violTime, ret = nvml.DeviceGetViolationStatus(device.device, nvml.PERF_POLICY_TOTAL_BASE_CLOCKS) + if ret == nvml.SUCCESS { + t := float64(violTime.ViolationTime) * 1e-9 + y, err := lp.New("nv_violation_below_base_clock", device.tags, device.meta, map[string]interface{}{"value": t}, time.Now()) + if err == nil { + y.AddMeta("unit", "sec") + output <- y + } + } + } + + return nil +} + +func readNVLinkStats(device NvidiaCollectorDevice, output chan lp.CCMetric) error { + // Retrieves the specified error counter value + // Please refer to \a nvmlNvLinkErrorCounter_t for error counters that are available + // + // For Pascal &tm; or newer fully supported devices. + + for i := 0; i < nvml.NVLINK_MAX_LINKS; i++ { + state, ret := nvml.DeviceGetNvLinkState(device.device, i) + if ret == nvml.SUCCESS { + if state == nvml.FEATURE_ENABLED { + if !device.excludeMetrics["nv_nvlink_crc_errors"] { + // Data link receive data CRC error counter + count, ret := nvml.DeviceGetNvLinkErrorCounter(device.device, i, nvml.NVLINK_ERROR_DL_CRC_DATA) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_nvlink_crc_errors", device.tags, device.meta, map[string]interface{}{"value": count}, time.Now()) + if err == nil { + y.AddTag("stype", "nvlink") + y.AddTag("stype-id", fmt.Sprintf("%d", i)) + output <- y + } + } + } + if !device.excludeMetrics["nv_nvlink_ecc_errors"] { + // Data link receive data ECC error counter + count, ret := nvml.DeviceGetNvLinkErrorCounter(device.device, i, nvml.NVLINK_ERROR_DL_ECC_DATA) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_nvlink_ecc_errors", device.tags, device.meta, map[string]interface{}{"value": count}, time.Now()) + if err == nil { + y.AddTag("stype", "nvlink") + y.AddTag("stype-id", fmt.Sprintf("%d", i)) + output <- y + } + } + } + if !device.excludeMetrics["nv_nvlink_replay_errors"] { + // Data link transmit replay error counter + count, ret := nvml.DeviceGetNvLinkErrorCounter(device.device, i, nvml.NVLINK_ERROR_DL_REPLAY) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_nvlink_replay_errors", device.tags, device.meta, map[string]interface{}{"value": count}, time.Now()) + if err == nil { + y.AddTag("stype", "nvlink") + y.AddTag("stype-id", fmt.Sprintf("%d", i)) + output <- y + } + } + } + if !device.excludeMetrics["nv_nvlink_recovery_errors"] { + // Data link transmit recovery error counter + count, ret := nvml.DeviceGetNvLinkErrorCounter(device.device, i, nvml.NVLINK_ERROR_DL_RECOVERY) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_nvlink_recovery_errors", device.tags, device.meta, map[string]interface{}{"value": count}, time.Now()) + if err == nil { + y.AddTag("stype", "nvlink") + y.AddTag("stype-id", fmt.Sprintf("%d", i)) + output <- y + } + } + } + if !device.excludeMetrics["nv_nvlink_crc_flit_errors"] { + // Data link receive flow control digit CRC error counter + count, ret := nvml.DeviceGetNvLinkErrorCounter(device.device, i, nvml.NVLINK_ERROR_DL_CRC_FLIT) + if ret == nvml.SUCCESS { + y, err := lp.New("nv_nvlink_crc_flit_errors", device.tags, device.meta, map[string]interface{}{"value": count}, time.Now()) + if err == nil { + y.AddTag("stype", "nvlink") + y.AddTag("stype-id", fmt.Sprintf("%d", i)) + output <- y + } + } + } + } + } + } + return nil +} + func (m *NvidiaCollector) Read(interval time.Duration, output chan lp.CCMetric) { + var err error if !m.init { return } - for i := range m.gpus { - device := &m.gpus[i] - - if !device.excludeMetrics["nv_util"] || !device.excludeMetrics["nv_mem_util"] { - // Retrieves the current utilization rates for the device's major subsystems. - // - // Available utilization rates - // * Gpu: Percent of time over the past sample period during which one or more kernels was executing on the GPU. - // * Memory: Percent of time over the past sample period during which global (device) memory was being read or written - // - // Note: - // * During driver initialization when ECC is enabled one can see high GPU and Memory Utilization readings. - // This is caused by ECC Memory Scrubbing mechanism that is performed during driver initialization. - // * On MIG-enabled GPUs, querying device utilization rates is not currently supported. - util, ret := nvml.DeviceGetUtilizationRates(device.device) - if ret == nvml.SUCCESS { - if !device.excludeMetrics["nv_util"] { - y, err := lp.New("nv_util", device.tags, m.meta, map[string]interface{}{"value": float64(util.Gpu)}, time.Now()) - if err == nil { - y.AddMeta("unit", "%") - output <- y - } - } - if !device.excludeMetrics["nv_mem_util"] { - y, err := lp.New("nv_mem_util", device.tags, m.meta, map[string]interface{}{"value": float64(util.Memory)}, time.Now()) - if err == nil { - y.AddMeta("unit", "%") - output <- y - } - } - } + readAll := func(device NvidiaCollectorDevice, output chan lp.CCMetric) { + name, ret := nvml.DeviceGetName(device.device) + if ret != nvml.SUCCESS { + name = "NoName" + } + err = readMemoryInfo(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readMemoryInfo for device", name, "failed") } - if !device.excludeMetrics["nv_mem_total"] || !device.excludeMetrics["nv_fb_memory"] { - // Retrieves the amount of used, free and total memory available on the device, in bytes. - // - // Enabling ECC reduces the amount of total available memory, due to the extra required parity bits. - // - // The reported amount of used memory is equal to the sum of memory allocated by all active channels on the device. - // - // Available memory info: - // * Free: Unallocated FB memory (in bytes). - // * Total: Total installed FB memory (in bytes). - // * Used: Allocated FB memory (in bytes). Note that the driver/GPU always sets aside a small amount of memory for bookkeeping. - // - // Note: - // In MIG mode, if device handle is provided, the API returns aggregate information, only if the caller has appropriate privileges. - // Per-instance information can be queried by using specific MIG device handles. - meminfo, ret := nvml.DeviceGetMemoryInfo(device.device) - if ret == nvml.SUCCESS { - if !device.excludeMetrics["nv_mem_total"] { - t := float64(meminfo.Total) / (1024 * 1024) - y, err := lp.New("nv_mem_total", device.tags, m.meta, map[string]interface{}{"value": t}, time.Now()) - if err == nil { - y.AddMeta("unit", "MByte") - output <- y - } - } - - if !device.excludeMetrics["nv_fb_memory"] { - f := float64(meminfo.Used) / (1024 * 1024) - y, err := lp.New("nv_fb_memory", device.tags, m.meta, map[string]interface{}{"value": f}, time.Now()) - if err == nil { - y.AddMeta("unit", "MByte") - output <- y - } - } - } + err = readUtilization(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readUtilization for device", name, "failed") } - if !device.excludeMetrics["nv_temp"] { - // Retrieves the current temperature readings for the device, in degrees C. - // - // Available temperature sensors: - // * TEMPERATURE_GPU: Temperature sensor for the GPU die. - // * NVML_TEMPERATURE_COUNT - temp, ret := nvml.DeviceGetTemperature(device.device, nvml.TEMPERATURE_GPU) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_temp", device.tags, m.meta, map[string]interface{}{"value": float64(temp)}, time.Now()) - if err == nil { - y.AddMeta("unit", "degC") - output <- y - } - } + err = readTemp(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readTemp for device", name, "failed") } - if !device.excludeMetrics["nv_fan"] { - // Retrieves the intended operating speed of the device's fan. - // - // Note: The reported speed is the intended fan speed. - // If the fan is physically blocked and unable to spin, the output will not match the actual fan speed. - // - // For all discrete products with dedicated fans. - // - // The fan speed is expressed as a percentage of the product's maximum noise tolerance fan speed. - // This value may exceed 100% in certain cases. - fan, ret := nvml.DeviceGetFanSpeed(device.device) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_fan", device.tags, m.meta, map[string]interface{}{"value": float64(fan)}, time.Now()) - if err == nil { - y.AddMeta("unit", "%") - output <- y - } - } + err = readFan(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readFan for device", name, "failed") } - if !device.excludeMetrics["nv_ecc_mode"] { - // Retrieves the current and pending ECC modes for the device. - // - // For Fermi or newer fully supported devices. Only applicable to devices with ECC. - // Requires NVML_INFOROM_ECC version 1.0 or higher. - // - // Changing ECC modes requires a reboot. - // The "pending" ECC mode refers to the target mode following the next reboot. - _, ecc_pend, ret := nvml.DeviceGetEccMode(device.device) - if ret == nvml.SUCCESS { - var y lp.CCMetric - var err error - switch ecc_pend { - case nvml.FEATURE_DISABLED: - y, err = lp.New("nv_ecc_mode", device.tags, m.meta, map[string]interface{}{"value": "OFF"}, time.Now()) - case nvml.FEATURE_ENABLED: - y, err = lp.New("nv_ecc_mode", device.tags, m.meta, map[string]interface{}{"value": "ON"}, time.Now()) - default: - y, err = lp.New("nv_ecc_mode", device.tags, m.meta, map[string]interface{}{"value": "UNKNOWN"}, time.Now()) - } - if err == nil { - output <- y - } - } else if ret == nvml.ERROR_NOT_SUPPORTED { - y, err := lp.New("nv_ecc_mode", device.tags, m.meta, map[string]interface{}{"value": "N/A"}, time.Now()) - if err == nil { - output <- y - } - } + err = readEccMode(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readEccMode for device", name, "failed") } - if !device.excludeMetrics["nv_perf_state"] { - // Retrieves the current performance state for the device. - // - // Allowed PStates: - // 0: Maximum Performance. - // .. - // 15: Minimum Performance. - // 32: Unknown performance state. - pState, ret := nvml.DeviceGetPerformanceState(device.device) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_perf_state", device.tags, m.meta, map[string]interface{}{"value": fmt.Sprintf("P%d", int(pState))}, time.Now()) - if err == nil { - output <- y - } - } + err = readPerfState(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readPerfState for device", name, "failed") } - if !device.excludeMetrics["nv_power_usage_report"] { - // Retrieves power usage for this GPU in milliwatts and its associated circuitry (e.g. memory) - // - // On Fermi and Kepler GPUs the reading is accurate to within +/- 5% of current power draw. - // - // It is only available if power management mode is supported - power, ret := nvml.DeviceGetPowerUsage(device.device) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_power_usage_report", device.tags, m.meta, map[string]interface{}{"value": float64(power) / 1000}, time.Now()) - if err == nil { - y.AddMeta("unit", "watts") - output <- y - } - } + err = readPowerUsage(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readPowerUsage for device", name, "failed") } - // Retrieves the current clock speeds for the device. - // - // Available clock information: - // * CLOCK_GRAPHICS: Graphics clock domain. - // * CLOCK_SM: Streaming Multiprocessor clock domain. - // * CLOCK_MEM: Memory clock domain. - if !device.excludeMetrics["nv_graphics_clock_report"] { - graphicsClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_GRAPHICS) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_graphics_clock_report", device.tags, m.meta, map[string]interface{}{"value": float64(graphicsClock)}, time.Now()) - if err == nil { - y.AddMeta("unit", "MHz") - output <- y - } - } + err = readClocks(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readClocks for device", name, "failed") } - if !device.excludeMetrics["nv_sm_clock_report"] { - smCock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_SM) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_sm_clock_report", device.tags, m.meta, map[string]interface{}{"value": float64(smCock)}, time.Now()) - if err == nil { - y.AddMeta("unit", "MHz") - output <- y - } - } + err = readMaxClocks(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readMaxClocks for device", name, "failed") } - if !device.excludeMetrics["nv_mem_clock_report"] { - memClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_MEM) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_mem_clock_report", device.tags, m.meta, map[string]interface{}{"value": float64(memClock)}, time.Now()) - if err == nil { - y.AddMeta("unit", "MHz") - output <- y - } - } + err = readEccErrors(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readEccErrors for device", name, "failed") } - // Retrieves the maximum clock speeds for the device. - // - // Available clock information: - // * CLOCK_GRAPHICS: Graphics clock domain. - // * CLOCK_SM: Streaming multiprocessor clock domain. - // * CLOCK_MEM: Memory clock domain. - // * CLOCK_VIDEO: Video encoder/decoder clock domain. - // * CLOCK_COUNT: Count of clock types. - // - // Note: - /// On GPUs from Fermi family current P0 clocks (reported by nvmlDeviceGetClockInfo) can differ from max clocks by few MHz. - if !device.excludeMetrics["nv_max_graphics_clock"] { - max_gclk, ret := nvml.DeviceGetMaxClockInfo(device.device, nvml.CLOCK_GRAPHICS) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_max_graphics_clock", device.tags, m.meta, map[string]interface{}{"value": float64(max_gclk)}, time.Now()) - if err == nil { - y.AddMeta("unit", "MHz") - output <- y - } - } + err = readPowerLimit(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readPowerLimit for device", name, "failed") } - if !device.excludeMetrics["nv_max_sm_clock"] { - maxSmClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_SM) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_max_sm_clock", device.tags, m.meta, map[string]interface{}{"value": float64(maxSmClock)}, time.Now()) - if err == nil { - y.AddMeta("unit", "MHz") - output <- y - } - } + err = readEncUtilization(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readEncUtilization for device", name, "failed") } - if !device.excludeMetrics["nv_max_mem_clock"] { - maxMemClock, ret := nvml.DeviceGetClockInfo(device.device, nvml.CLOCK_MEM) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_max_mem_clock", device.tags, m.meta, map[string]interface{}{"value": float64(maxMemClock)}, time.Now()) - if err == nil { - y.AddMeta("unit", "MHz") - output <- y - } - } + err = readDecUtilization(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readDecUtilization for device", name, "failed") } - if !device.excludeMetrics["nv_ecc_db_error"] { - // Retrieves the total ECC error counts for the device. - // - // For Fermi or newer fully supported devices. - // Only applicable to devices with ECC. - // Requires NVML_INFOROM_ECC version 1.0 or higher. - // Requires ECC Mode to be enabled. - // - // The total error count is the sum of errors across each of the separate memory systems, - // i.e. the total set of errors across the entire device. - ecc_db, ret := nvml.DeviceGetTotalEccErrors(device.device, nvml.MEMORY_ERROR_TYPE_UNCORRECTED, nvml.AGGREGATE_ECC) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_ecc_db_error", device.tags, m.meta, map[string]interface{}{"value": float64(ecc_db)}, time.Now()) - if err == nil { - output <- y - } - } + err = readRemappedRows(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readRemappedRows for device", name, "failed") } - if !device.excludeMetrics["nv_ecc_sb_error"] { - ecc_sb, ret := nvml.DeviceGetTotalEccErrors(device.device, nvml.MEMORY_ERROR_TYPE_CORRECTED, nvml.AGGREGATE_ECC) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_ecc_sb_error", device.tags, m.meta, map[string]interface{}{"value": float64(ecc_sb)}, time.Now()) - if err == nil { - output <- y - } - } + err = readBarMemoryInfo(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readBarMemoryInfo for device", name, "failed") } - if !device.excludeMetrics["nv_power_man_limit"] { - // Retrieves the power management limit associated with this device. - // - // For Fermi or newer fully supported devices. - // - // The power limit defines the upper boundary for the card's power draw. - // If the card's total power draw reaches this limit the power management algorithm kicks in. - pwr_limit, ret := nvml.DeviceGetPowerManagementLimit(device.device) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_power_man_limit", device.tags, m.meta, map[string]interface{}{"value": float64(pwr_limit) / 1000}, time.Now()) - if err == nil { - y.AddMeta("unit", "watts") - output <- y - } - } + err = readProcessCounts(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readProcessCounts for device", name, "failed") } - if !device.excludeMetrics["nv_encoder_util"] { - // Retrieves the current utilization and sampling size in microseconds for the Encoder - // - // For Kepler or newer fully supported devices. - // - // Note: On MIG-enabled GPUs, querying encoder utilization is not currently supported. - enc_util, _, ret := nvml.DeviceGetEncoderUtilization(device.device) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_encoder_util", device.tags, m.meta, map[string]interface{}{"value": float64(enc_util)}, time.Now()) - if err == nil { - y.AddMeta("unit", "%") - output <- y - } - } + err = readViolationStats(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readViolationStats for device", name, "failed") } - if !device.excludeMetrics["nv_decoder_util"] { - // Retrieves the current utilization and sampling size in microseconds for the Decoder - // - // For Kepler or newer fully supported devices. - // - // Note: On MIG-enabled GPUs, querying decoder utilization is not currently supported. - dec_util, _, ret := nvml.DeviceGetDecoderUtilization(device.device) - if ret == nvml.SUCCESS { - y, err := lp.New("nv_decoder_util", device.tags, m.meta, map[string]interface{}{"value": float64(dec_util)}, time.Now()) - if err == nil { - y.AddMeta("unit", "%") - output <- y - } - } + err = readNVLinkStats(device, output) + if err != nil { + cclog.ComponentDebug(m.name, "readNVLinkStats for device", name, "failed") } } + // Actual read loop over all attached Nvidia GPUs + for i := 0; i < m.num_gpus; i++ { + + readAll(m.gpus[i], output) + + // Iterate over all MIG devices if any + if m.config.ProcessMigDevices { + current, _, ret := nvml.DeviceGetMigMode(m.gpus[i].device) + if ret != nvml.SUCCESS { + continue + } + if current == nvml.DEVICE_MIG_DISABLE { + continue + } + + maxMig, ret := nvml.DeviceGetMaxMigDeviceCount(m.gpus[i].device) + if ret != nvml.SUCCESS { + continue + } + if maxMig == 0 { + continue + } + cclog.ComponentDebug(m.name, "Reading MIG devices for GPU", i) + + for j := 0; j < maxMig; j++ { + mdev, ret := nvml.DeviceGetMigDeviceHandleByIndex(m.gpus[i].device, j) + if ret != nvml.SUCCESS { + continue + } + + excludeMetrics := make(map[string]bool) + for _, metric := range m.config.ExcludeMetrics { + excludeMetrics[metric] = true + } + + migDevice := NvidiaCollectorDevice{ + device: mdev, + tags: map[string]string{}, + meta: map[string]string{}, + excludeMetrics: excludeMetrics, + } + for k, v := range m.gpus[i].tags { + migDevice.tags[k] = v + } + migDevice.tags["stype"] = "mig" + if m.config.UseUuidForMigDevices { + uuid, ret := nvml.DeviceGetUUID(mdev) + if ret != nvml.SUCCESS { + cclog.ComponentError(m.name, "Unable to get UUID for mig device at index", j, ":", err.Error()) + } else { + migDevice.tags["stype-id"] = uuid + } + } else if m.config.UseSliceForMigDevices { + name, ret := nvml.DeviceGetName(m.gpus[i].device) + if ret == nvml.SUCCESS { + mname, ret := nvml.DeviceGetName(mdev) + if ret == nvml.SUCCESS { + x := strings.Replace(mname, name, "", -1) + x = strings.Replace(x, "MIG", "", -1) + x = strings.TrimSpace(x) + migDevice.tags["stype-id"] = x + } + } + } + if _, ok := migDevice.tags["stype-id"]; !ok { + migDevice.tags["stype-id"] = fmt.Sprintf("%d", j) + } + for k, v := range m.gpus[i].meta { + migDevice.meta[k] = v + } + if _, ok := migDevice.meta["uuid"]; ok && !m.config.UseUuidForMigDevices { + uuid, ret := nvml.DeviceGetUUID(mdev) + if ret == nvml.SUCCESS { + migDevice.meta["uuid"] = uuid + } + } + + readAll(migDevice, output) + } + } + } } func (m *NvidiaCollector) Close() { diff --git a/collectors/nvidiaMetric.md b/collectors/nvidiaMetric.md index afe8b9e..7f0c416 100644 --- a/collectors/nvidiaMetric.md +++ b/collectors/nvidiaMetric.md @@ -3,38 +3,74 @@ ```json "nvidia": { - "exclude_devices" : [ - "0","1" + "exclude_devices": [ + "0","1", "0000000:ff:01.0" ], "exclude_metrics": [ - "nv_fb_memory", + "nv_fb_mem_used", "nv_fan" - ] + ], + "process_mig_devices": false, + "use_pci_info_as_type_id": true, + "add_pci_info_tag": false, + "add_uuid_meta": false, + "add_board_number_meta": false, + "add_serial_meta": false, + "use_uuid_for_mig_device": false, + "use_slice_for_mig_device": false } ``` +The `nvidia` collector can be configured to leave out specific devices with the `exclude_devices` option. It takes IDs as supplied to the NVML with `nvmlDeviceGetHandleByIndex()` or the PCI address in NVML format (`%08X:%02X:%02X.0`). Metrics (listed below) that should not be sent to the MetricRouter can be excluded with the `exclude_metrics` option. Commonly only the physical GPUs are monitored. If MIG devices should be analyzed as well, set `process_mig_devices` (adds `stype=mig,stype-id=`). With the options `use_uuid_for_mig_device` and `use_slice_for_mig_device`, the `` can be replaced with the UUID (e.g. `MIG-6a9f7cc8-6d5b-5ce0-92de-750edc4d8849`) or the MIG slice name (e.g. `1g.5gb`). + +The metrics sent by the `nvidia` collector use `accelerator` as `type` tag. For the `type-id`, it uses the device handle index by default. With the `use_pci_info_as_type_id` option, the PCI ID is used instead. If both values should be added as tags, activate the `add_pci_info_tag` option. It uses the device handle index as `type-id` and adds the PCI ID as separate `pci_identifier` tag. + +Optionally, it is possible to add the UUID, the board part number and the serial to the meta informations. They are not sent to the sinks (if not configured otherwise). + + Metrics: * `nv_util` * `nv_mem_util` -* `nv_mem_total` -* `nv_fb_memory` +* `nv_fb_mem_total` +* `nv_fb_mem_used` +* `nv_bar1_mem_total` +* `nv_bar1_mem_used` * `nv_temp` * `nv_fan` * `nv_ecc_mode` * `nv_perf_state` -* `nv_power_usage_report` -* `nv_graphics_clock_report` -* `nv_sm_clock_report` -* `nv_mem_clock_report` +* `nv_power_usage` +* `nv_graphics_clock` +* `nv_sm_clock` +* `nv_mem_clock` +* `nv_video_clock` * `nv_max_graphics_clock` * `nv_max_sm_clock` * `nv_max_mem_clock` -* `nv_ecc_db_error` -* `nv_ecc_sb_error` -* `nv_power_man_limit` +* `nv_max_video_clock` +* `nv_ecc_uncorrected_error` +* `nv_ecc_corrected_error` +* `nv_power_max_limit` * `nv_encoder_util` * `nv_decoder_util` +* `nv_remapped_rows_corrected` +* `nv_remapped_rows_uncorrected` +* `nv_remapped_rows_pending` +* `nv_remapped_rows_failure` +* `nv_compute_processes` +* `nv_graphics_processes` +* `nv_violation_power` +* `nv_violation_thermal` +* `nv_violation_sync_boost` +* `nv_violation_board_limit` +* `nv_violation_low_util` +* `nv_violation_reliability` +* `nv_violation_below_app_clock` +* `nv_violation_below_base_clock` +* `nv_nvlink_crc_flit_errors` +* `nv_nvlink_crc_errors` +* `nv_nvlink_ecc_errors` +* `nv_nvlink_replay_errors` +* `nv_nvlink_recovery_errors` -It uses a separate `type` in the metrics. The output metric looks like this: -`,type=accelerator,type-id= value= ` - +Some metrics add the additional sub type tag (`stype`) like the `nv_nvlink_*` metrics set `stype=nvlink,stype-id=`. \ No newline at end of file diff --git a/collectors/rocmsmiMetric.go b/collectors/rocmsmiMetric.go new file mode 100644 index 0000000..c717a5d --- /dev/null +++ b/collectors/rocmsmiMetric.go @@ -0,0 +1,319 @@ +package collectors + +import ( + "encoding/json" + "errors" + "fmt" + "time" + + cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" + lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" + "github.com/ClusterCockpit/go-rocm-smi/pkg/rocm_smi" +) + +type RocmSmiCollectorConfig struct { + ExcludeMetrics []string `json:"exclude_metrics,omitempty"` + ExcludeDevices []string `json:"exclude_devices,omitempty"` + AddPciInfoTag bool `json:"add_pci_info_tag,omitempty"` + UsePciInfoAsTypeId bool `json:"use_pci_info_as_type_id,omitempty"` + AddSerialMeta bool `json:"add_serial_meta,omitempty"` +} + +type RocmSmiCollectorDevice struct { + device rocm_smi.DeviceHandle + index int + tags map[string]string // default tags + meta map[string]string // default meta information + excludeMetrics map[string]bool // copy of exclude metrics from config +} + +type RocmSmiCollector struct { + metricCollector + config RocmSmiCollectorConfig // the configuration structure + devices []RocmSmiCollectorDevice +} + +// Functions to implement MetricCollector interface +// Init(...), Read(...), Close() +// See: metricCollector.go + +// Init initializes the sample collector +// Called once by the collector manager +// All tags, meta data tags and metrics that do not change over the runtime should be set here +func (m *RocmSmiCollector) Init(config json.RawMessage) error { + var err error = nil + // Always set the name early in Init() to use it in cclog.Component* functions + m.name = "RocmSmiCollector" + // This is for later use, also call it early + m.setup() + // Define meta information sent with each metric + // (Can also be dynamic or this is the basic set with extension through AddMeta()) + //m.meta = map[string]string{"source": m.name, "group": "AMD"} + // Define tags sent with each metric + // The 'type' tag is always needed, it defines the granulatity of the metric + // node -> whole system + // socket -> CPU socket (requires socket ID as 'type-id' tag) + // cpu -> single CPU hardware thread (requires cpu ID as 'type-id' tag) + //m.tags = map[string]string{"type": "node"} + // Read in the JSON configuration + if len(config) > 0 { + err = json.Unmarshal(config, &m.config) + if err != nil { + cclog.ComponentError(m.name, "Error reading config:", err.Error()) + return err + } + } + + ret := rocm_smi.Init() + if ret != rocm_smi.STATUS_SUCCESS { + err = errors.New("Failed to initialize ROCm SMI library") + cclog.ComponentError(m.name, err.Error()) + return err + } + + numDevs, ret := rocm_smi.NumMonitorDevices() + if ret != rocm_smi.STATUS_SUCCESS { + err = errors.New("Failed to get number of GPUs from ROCm SMI library") + cclog.ComponentError(m.name, err.Error()) + return err + } + + exclDev := func(s string) bool { + skip_device := false + for _, excl := range m.config.ExcludeDevices { + if excl == s { + skip_device = true + break + } + } + return skip_device + } + + m.devices = make([]RocmSmiCollectorDevice, 0) + + for i := 0; i < numDevs; i++ { + str_i := fmt.Sprintf("%d", i) + if exclDev(str_i) { + continue + } + device, ret := rocm_smi.DeviceGetHandleByIndex(i) + if ret != rocm_smi.STATUS_SUCCESS { + err = fmt.Errorf("Failed to get handle for GPU %d", i) + cclog.ComponentError(m.name, err.Error()) + return err + } + + pciInfo, ret := rocm_smi.DeviceGetPciInfo(device) + if ret != rocm_smi.STATUS_SUCCESS { + err = fmt.Errorf("Failed to get PCI information for GPU %d", i) + cclog.ComponentError(m.name, err.Error()) + return err + } + + pciId := fmt.Sprintf( + "%08X:%02X:%02X.%X", + pciInfo.Domain, + pciInfo.Bus, + pciInfo.Device, + pciInfo.Function) + + if exclDev(pciId) { + continue + } + + dev := RocmSmiCollectorDevice{ + device: device, + tags: map[string]string{ + "type": "accelerator", + "type-id": str_i, + }, + meta: map[string]string{ + "source": m.name, + "group": "AMD", + }, + } + if m.config.UsePciInfoAsTypeId { + dev.tags["type-id"] = pciId + } else if m.config.AddPciInfoTag { + dev.tags["pci_identifier"] = pciId + } + + if m.config.AddSerialMeta { + serial, ret := rocm_smi.DeviceGetSerialNumber(device) + if ret != rocm_smi.STATUS_SUCCESS { + cclog.ComponentError(m.name, "Unable to get serial number for device at index", i, ":", rocm_smi.StatusStringNoError(ret)) + } else { + dev.meta["serial"] = serial + } + } + // Add excluded metrics + dev.excludeMetrics = map[string]bool{} + for _, e := range m.config.ExcludeMetrics { + dev.excludeMetrics[e] = true + } + dev.index = i + m.devices = append(m.devices, dev) + } + + // Set this flag only if everything is initialized properly, all required files exist, ... + m.init = true + return err +} + +// Read collects all metrics belonging to the sample collector +// and sends them through the output channel to the collector manager +func (m *RocmSmiCollector) Read(interval time.Duration, output chan lp.CCMetric) { + // Create a sample metric + timestamp := time.Now() + + for _, dev := range m.devices { + metrics, ret := rocm_smi.DeviceGetMetrics(dev.device) + if ret != rocm_smi.STATUS_SUCCESS { + cclog.ComponentError(m.name, "Unable to get metrics for device at index", dev.index, ":", rocm_smi.StatusStringNoError(ret)) + continue + } + + if !dev.excludeMetrics["rocm_gfx_util"] { + value := metrics.Average_gfx_activity + y, err := lp.New("rocm_gfx_util", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_umc_util"] { + value := metrics.Average_umc_activity + y, err := lp.New("rocm_umc_util", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_mm_util"] { + value := metrics.Average_mm_activity + y, err := lp.New("rocm_mm_util", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_avg_power"] { + value := metrics.Average_socket_power + y, err := lp.New("rocm_avg_power", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_mem"] { + value := metrics.Temperature_mem + y, err := lp.New("rocm_temp_mem", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_hotspot"] { + value := metrics.Temperature_hotspot + y, err := lp.New("rocm_temp_hotspot", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_edge"] { + value := metrics.Temperature_edge + y, err := lp.New("rocm_temp_edge", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_vrgfx"] { + value := metrics.Temperature_vrgfx + y, err := lp.New("rocm_temp_vrgfx", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_vrsoc"] { + value := metrics.Temperature_vrsoc + y, err := lp.New("rocm_temp_vrsoc", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_vrmem"] { + value := metrics.Temperature_vrmem + y, err := lp.New("rocm_temp_vrmem", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_gfx_clock"] { + value := metrics.Average_gfxclk_frequency + y, err := lp.New("rocm_gfx_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_soc_clock"] { + value := metrics.Average_socclk_frequency + y, err := lp.New("rocm_soc_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_u_clock"] { + value := metrics.Average_uclk_frequency + y, err := lp.New("rocm_u_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_v0_clock"] { + value := metrics.Average_vclk0_frequency + y, err := lp.New("rocm_v0_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_v1_clock"] { + value := metrics.Average_vclk1_frequency + y, err := lp.New("rocm_v1_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_d0_clock"] { + value := metrics.Average_dclk0_frequency + y, err := lp.New("rocm_d0_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_d1_clock"] { + value := metrics.Average_dclk1_frequency + y, err := lp.New("rocm_d1_clock", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + output <- y + } + } + if !dev.excludeMetrics["rocm_temp_hbm"] { + for i := 0; i < rocm_smi.NUM_HBM_INSTANCES; i++ { + value := metrics.Temperature_hbm[i] + y, err := lp.New("rocm_temp_hbm", dev.tags, dev.meta, map[string]interface{}{"value": value}, timestamp) + if err == nil { + y.AddTag("stype", "device") + y.AddTag("stype-id", fmt.Sprintf("%d", i)) + output <- y + } + } + } + } + +} + +// Close metric collector: close network connection, close files, close libraries, ... +// Called once by the collector manager +func (m *RocmSmiCollector) Close() { + // Unset flag + ret := rocm_smi.Shutdown() + if ret != rocm_smi.STATUS_SUCCESS { + cclog.ComponentError(m.name, "Failed to shutdown ROCm SMI library") + } + m.init = false +} diff --git a/collectors/rocmsmiMetric.md b/collectors/rocmsmiMetric.md new file mode 100644 index 0000000..9c4da5e --- /dev/null +++ b/collectors/rocmsmiMetric.md @@ -0,0 +1,47 @@ + +## `rocm_smi` collector + +```json + "rocm_smi": { + "exclude_devices": [ + "0","1", "0000000:ff:01.0" + ], + "exclude_metrics": [ + "rocm_mm_util", + "rocm_temp_vrsoc" + ], + "use_pci_info_as_type_id": true, + "add_pci_info_tag": false, + "add_serial_meta": false, + } +``` + +The `rocm_smi` collector can be configured to leave out specific devices with the `exclude_devices` option. It takes logical IDs in the list of available devices or the PCI address similar to NVML format (`%08X:%02X:%02X.0`). Metrics (listed below) that should not be sent to the MetricRouter can be excluded with the `exclude_metrics` option. + +The metrics sent by the `rocm_smi` collector use `accelerator` as `type` tag. For the `type-id`, it uses the device handle index by default. With the `use_pci_info_as_type_id` option, the PCI ID is used instead. If both values should be added as tags, activate the `add_pci_info_tag` option. It uses the device handle index as `type-id` and adds the PCI ID as separate `pci_identifier` tag. + +Optionally, it is possible to add the serial to the meta informations. They are not sent to the sinks (if not configured otherwise). + + +Metrics: +* `rocm_gfx_util` +* `rocm_umc_util` +* `rocm_mm_util` +* `rocm_avg_power` +* `rocm_temp_mem` +* `rocm_temp_hotspot` +* `rocm_temp_edge` +* `rocm_temp_vrgfx` +* `rocm_temp_vrsoc` +* `rocm_temp_vrmem` +* `rocm_gfx_clock` +* `rocm_soc_clock` +* `rocm_u_clock` +* `rocm_v0_clock` +* `rocm_v1_clock` +* `rocm_d0_clock` +* `rocm_d1_clock` +* `rocm_temp_hbm` + + +Some metrics add the additional sub type tag (`stype`) like the `rocm_temp_hbm` metrics set `stype=device,stype-id=`. diff --git a/collectors/sampleMetric.go b/collectors/sampleMetric.go index 47078a6..47ec296 100644 --- a/collectors/sampleMetric.go +++ b/collectors/sampleMetric.go @@ -35,6 +35,10 @@ func (m *SampleCollector) Init(config json.RawMessage) error { m.name = "InternalCollector" // This is for later use, also call it early m.setup() + // Tell whether the collector should be run in parallel with others (reading files, ...) + // or it should be run serially, mostly for collectors acutally doing measurements + // because they should not measure the execution of the other collectors + m.parallel = true // Define meta information sent with each metric // (Can also be dynamic or this is the basic set with extension through AddMeta()) m.meta = map[string]string{"source": m.name, "group": "SAMPLE"} @@ -42,7 +46,12 @@ func (m *SampleCollector) Init(config json.RawMessage) error { // The 'type' tag is always needed, it defines the granulatity of the metric // node -> whole system // socket -> CPU socket (requires socket ID as 'type-id' tag) - // cpu -> single CPU hardware thread (requires cpu ID as 'type-id' tag) + // die -> CPU die (requires CPU die ID as 'type-id' tag) + // memoryDomain -> NUMA domain (requires NUMA domain ID as 'type-id' tag) + // llc -> Last level cache (requires last level cache ID as 'type-id' tag) + // core -> single CPU core that may consist of multiple hardware threads (SMT) (requires core ID as 'type-id' tag) + // hwthtread -> single CPU hardware thread (requires hardware thread ID as 'type-id' tag) + // accelerator -> A accelerator device like GPU or FPGA (requires an accelerator ID as 'type-id' tag) m.tags = map[string]string{"type": "node"} // Read in the JSON configuration if len(config) > 0 { diff --git a/collectors/tempMetric.go b/collectors/tempMetric.go index 7ba8eb1..af9d7fd 100644 --- a/collectors/tempMetric.go +++ b/collectors/tempMetric.go @@ -50,6 +50,7 @@ func (m *TempCollector) Init(config json.RawMessage) error { } m.name = "TempCollector" + m.parallel = true m.setup() if len(config) > 0 { err := json.Unmarshal(config, &m.config) @@ -116,6 +117,10 @@ func (m *TempCollector) Init(config json.RawMessage) error { } // Sensor file + _, err = ioutil.ReadFile(file) + if err != nil { + continue + } sensor.file = file // Sensor tags diff --git a/collectors/topprocsMetric.go b/collectors/topprocsMetric.go index 408c3cc..1f4aaca 100644 --- a/collectors/topprocsMetric.go +++ b/collectors/topprocsMetric.go @@ -28,6 +28,7 @@ type TopProcsCollector struct { func (m *TopProcsCollector) Init(config json.RawMessage) error { var err error m.name = "TopProcsCollector" + m.parallel = true m.tags = map[string]string{"type": "node"} m.meta = map[string]string{"source": m.name, "group": "TopProcs"} if len(config) > 0 { diff --git a/config.json b/config.json index 52f9df1..924bec7 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,8 @@ { - "sinks": "sinks.json", - "collectors" : "collectors.json", - "receivers" : "receivers.json", - "router" : "router.json", - "interval": 10, - "duration": 1 + "sinks": "./sinks.json", + "collectors" : "./collectors.json", + "receivers" : "./receivers.json", + "router" : "./router.json", + "interval": "10s", + "duration": "1s" } diff --git a/go.mod b/go.mod index 07d46f6..a4aacb8 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,35 @@ module github.com/ClusterCockpit/cc-metric-collector -go 1.16 +go 1.17 require ( + github.com/ClusterCockpit/cc-units v0.0.0-20220318130935-92a0c6442220 github.com/NVIDIA/go-nvml v0.11.6-0 github.com/PaesslerAG/gval v1.1.2 github.com/gorilla/mux v1.8.0 github.com/influxdata/influxdb-client-go/v2 v2.8.1 github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf - github.com/nats-io/nats-server/v2 v2.8.0 // indirect github.com/nats-io/nats.go v1.14.0 github.com/prometheus/client_golang v1.12.1 github.com/stmcginnis/gofish v0.13.0 golang.org/x/sys v0.0.0-20220412211240-33da011f77ad ) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/deepmap/oapi-codegen v1.8.2 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/nats-io/nats-server/v2 v2.8.0 // indirect + github.com/nats-io/nkeys v0.3.0 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/common v0.32.1 // indirect + github.com/prometheus/procfs v0.7.3 // indirect + golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect + golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect + google.golang.org/protobuf v1.26.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.mod.1.16 b/go.mod.1.16 new file mode 100644 index 0000000..2a72cb3 --- /dev/null +++ b/go.mod.1.16 @@ -0,0 +1,16 @@ +module github.com/ClusterCockpit/cc-metric-collector + +go 1.16 + +require ( + github.com/NVIDIA/go-nvml v0.11.6-0 + github.com/PaesslerAG/gval v1.1.2 + github.com/gorilla/mux v1.8.0 + github.com/influxdata/influxdb-client-go/v2 v2.7.0 + github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf + github.com/nats-io/nats-server/v2 v2.8.0 // indirect + github.com/nats-io/nats.go v1.14.0 + github.com/prometheus/client_golang v1.12.1 + github.com/stmcginnis/gofish v0.13.0 + golang.org/x/sys v0.0.0-20220412211240-33da011f77ad +) diff --git a/go.mod.1.17+ b/go.mod.1.17+ new file mode 100644 index 0000000..b3dc6ca --- /dev/null +++ b/go.mod.1.17+ @@ -0,0 +1,16 @@ +module github.com/ClusterCockpit/cc-metric-collector + +go 1.17 + +require ( + github.com/NVIDIA/go-nvml v0.11.6-0 + github.com/PaesslerAG/gval v1.1.2 + github.com/gorilla/mux v1.8.0 + github.com/influxdata/influxdb-client-go/v2 v2.8.1 + github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf + github.com/nats-io/nats-server/v2 v2.8.0 // indirect + github.com/nats-io/nats.go v1.14.0 + github.com/prometheus/client_golang v1.12.1 + github.com/stmcginnis/gofish v0.13.0 + golang.org/x/sys v0.0.0-20220412211240-33da011f77ad +) diff --git a/internal/ccTopology/ccTopology.go b/internal/ccTopology/ccTopology.go index f68c3f4..0ed8883 100644 --- a/internal/ccTopology/ccTopology.go +++ b/internal/ccTopology/ccTopology.go @@ -29,6 +29,7 @@ func intArrayContains(array []int, str int) (int, bool) { return -1, false } +// Used internally for sysfs file reads func fileToInt(path string) int { buffer, err := ioutil.ReadFile(path) if err != nil { @@ -47,6 +48,7 @@ func fileToInt(path string) int { return int(id) } +// Get list of CPU socket IDs func SocketList() []int { buffer, err := ioutil.ReadFile(string(PROCFS_CPUINFO)) if err != nil { @@ -54,7 +56,7 @@ func SocketList() []int { return nil } ll := strings.Split(string(buffer), "\n") - var packs []int + packs := make([]int, 0) for _, line := range ll { if strings.HasPrefix(line, "physical id") { lv := strings.Fields(line) @@ -72,7 +74,8 @@ func SocketList() []int { return packs } -func CpuList() []int { +// Get list of hardware thread IDs in the order of listing in /proc/cpuinfo +func HwthreadList() []int { buffer, err := ioutil.ReadFile(string(PROCFS_CPUINFO)) if err != nil { log.Print(err) @@ -97,6 +100,13 @@ func CpuList() []int { return cpulist } +// Get list of hardware thread IDs in the order of listing in /proc/cpuinfo +// Deprecated! Use HwthreadList() +func CpuList() []int { + return HwthreadList() +} + +// Get list of CPU core IDs in the order of listing in /proc/cpuinfo func CoreList() []int { buffer, err := ioutil.ReadFile(string(PROCFS_CPUINFO)) if err != nil { @@ -122,6 +132,7 @@ func CoreList() []int { return corelist } +// Get list of NUMA node IDs func NumaNodeList() []int { numaList := make([]int, 0) globPath := filepath.Join(string(SYSFS_NUMABASE), "node*") @@ -156,8 +167,9 @@ func NumaNodeList() []int { return numaList } +// Get list of CPU die IDs func DieList() []int { - cpulist := CpuList() + cpulist := HwthreadList() dielist := make([]int, 0) for _, c := range cpulist { diepath := filepath.Join(string(SYSFS_CPUBASE), fmt.Sprintf("cpu%d", c), "topology/die_id") @@ -175,7 +187,27 @@ func DieList() []int { return SocketList() } -type CpuEntry struct { +// Get list of specified type using the naming format inside ClusterCockpit +func GetTypeList(topology_type string) []int { + switch topology_type { + case "node": + return []int{0} + case "socket": + return SocketList() + case "die": + return DieList() + case "memoryDomain": + return NumaNodeList() + case "core": + return CoreList() + case "hwthread": + return HwthreadList() + } + return []int{} +} + +// Structure holding all information about a hardware thread +type HwthreadEntry struct { Cpuid int SMT int Core int @@ -184,25 +216,25 @@ type CpuEntry struct { Die int } -func CpuData() []CpuEntry { +func CpuData() []HwthreadEntry { - fileToInt := func(path string) int { - buffer, err := ioutil.ReadFile(path) - if err != nil { - log.Print(err) - //cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error()) - return -1 - } - sbuffer := strings.Replace(string(buffer), "\n", "", -1) - var id int64 - //_, err = fmt.Scanf("%d", sbuffer, &id) - id, err = strconv.ParseInt(sbuffer, 10, 32) - if err != nil { - cclogger.ComponentError("ccTopology", "Parsing", path, ":", sbuffer, err.Error()) - return -1 - } - return int(id) - } + // fileToInt := func(path string) int { + // buffer, err := ioutil.ReadFile(path) + // if err != nil { + // log.Print(err) + // //cclogger.ComponentError("ccTopology", "Reading", path, ":", err.Error()) + // return -1 + // } + // sbuffer := strings.Replace(string(buffer), "\n", "", -1) + // var id int64 + // //_, err = fmt.Scanf("%d", sbuffer, &id) + // id, err = strconv.ParseInt(sbuffer, 10, 32) + // if err != nil { + // cclogger.ComponentError("ccTopology", "Parsing", path, ":", sbuffer, err.Error()) + // return -1 + // } + // return int(id) + // } getCore := func(basepath string) int { return fileToInt(fmt.Sprintf("%s/core_id", basepath)) } @@ -260,9 +292,9 @@ func CpuData() []CpuEntry { return 0 } - clist := make([]CpuEntry, 0) - for _, c := range CpuList() { - clist = append(clist, CpuEntry{Cpuid: c}) + clist := make([]HwthreadEntry, 0) + for _, c := range HwthreadList() { + clist = append(clist, HwthreadEntry{Cpuid: c}) } for i, centry := range clist { centry.Socket = -1 @@ -298,6 +330,7 @@ func CpuData() []CpuEntry { return clist } +// Structure holding basic information about a CPU type CpuInformation struct { NumHWthreads int SMTWidth int @@ -307,6 +340,7 @@ type CpuInformation struct { NumNumaDomains int } +// Get basic information about the CPU func CpuInfo() CpuInformation { var c CpuInformation @@ -342,7 +376,8 @@ func CpuInfo() CpuInformation { return c } -func GetCpuSocket(cpuid int) int { +// Get the CPU socket ID for a given hardware thread ID +func GetHwthreadSocket(cpuid int) int { cdata := CpuData() for _, d := range cdata { if d.Cpuid == cpuid { @@ -352,7 +387,8 @@ func GetCpuSocket(cpuid int) int { return -1 } -func GetCpuNumaDomain(cpuid int) int { +// Get the NUMA node ID for a given hardware thread ID +func GetHwthreadNumaDomain(cpuid int) int { cdata := CpuData() for _, d := range cdata { if d.Cpuid == cpuid { @@ -362,7 +398,8 @@ func GetCpuNumaDomain(cpuid int) int { return -1 } -func GetCpuDie(cpuid int) int { +// Get the CPU die ID for a given hardware thread ID +func GetHwthreadDie(cpuid int) int { cdata := CpuData() for _, d := range cdata { if d.Cpuid == cpuid { @@ -372,7 +409,8 @@ func GetCpuDie(cpuid int) int { return -1 } -func GetCpuCore(cpuid int) int { +// Get the CPU core ID for a given hardware thread ID +func GetHwthreadCore(cpuid int) int { cdata := CpuData() for _, d := range cdata { if d.Cpuid == cpuid { @@ -382,7 +420,8 @@ func GetCpuCore(cpuid int) int { return -1 } -func GetSocketCpus(socket int) []int { +// Get the all hardware thread ID associated with a CPU socket +func GetSocketHwthreads(socket int) []int { all := CpuData() cpulist := make([]int, 0) for _, d := range all { @@ -393,7 +432,8 @@ func GetSocketCpus(socket int) []int { return cpulist } -func GetNumaDomainCpus(domain int) []int { +// Get the all hardware thread ID associated with a NUMA node +func GetNumaDomainHwthreads(domain int) []int { all := CpuData() cpulist := make([]int, 0) for _, d := range all { @@ -404,7 +444,8 @@ func GetNumaDomainCpus(domain int) []int { return cpulist } -func GetDieCpus(die int) []int { +// Get the all hardware thread ID associated with a CPU die +func GetDieHwthreads(die int) []int { all := CpuData() cpulist := make([]int, 0) for _, d := range all { @@ -415,7 +456,8 @@ func GetDieCpus(die int) []int { return cpulist } -func GetCoreCpus(core int) []int { +// Get the all hardware thread ID associated with a CPU core +func GetCoreHwthreads(core int) []int { all := CpuData() cpulist := make([]int, 0) for _, d := range all { diff --git a/internal/metricAggregator/metricAggregatorFunctions.go b/internal/metricAggregator/metricAggregatorFunctions.go index 1fbef65..945dc6d 100644 --- a/internal/metricAggregator/metricAggregatorFunctions.go +++ b/internal/metricAggregator/metricAggregatorFunctions.go @@ -246,7 +246,7 @@ func matchfunc(args ...interface{}) (interface{}, error) { func getCpuCoreFunc(args ...interface{}) (interface{}, error) { switch cpuid := args[0].(type) { case int: - return topo.GetCpuCore(cpuid), nil + return topo.GetHwthreadCore(cpuid), nil } return -1, errors.New("function 'getCpuCore' accepts only an 'int' cpuid") } @@ -255,7 +255,7 @@ func getCpuCoreFunc(args ...interface{}) (interface{}, error) { func getCpuSocketFunc(args ...interface{}) (interface{}, error) { switch cpuid := args[0].(type) { case int: - return topo.GetCpuSocket(cpuid), nil + return topo.GetHwthreadSocket(cpuid), nil } return -1, errors.New("function 'getCpuCore' accepts only an 'int' cpuid") } @@ -264,7 +264,7 @@ func getCpuSocketFunc(args ...interface{}) (interface{}, error) { func getCpuNumaDomainFunc(args ...interface{}) (interface{}, error) { switch cpuid := args[0].(type) { case int: - return topo.GetCpuNumaDomain(cpuid), nil + return topo.GetHwthreadNumaDomain(cpuid), nil } return -1, errors.New("function 'getCpuNuma' accepts only an 'int' cpuid") } @@ -273,7 +273,7 @@ func getCpuNumaDomainFunc(args ...interface{}) (interface{}, error) { func getCpuDieFunc(args ...interface{}) (interface{}, error) { switch cpuid := args[0].(type) { case int: - return topo.GetCpuDie(cpuid), nil + return topo.GetHwthreadDie(cpuid), nil } return -1, errors.New("function 'getCpuDie' accepts only an 'int' cpuid") } @@ -336,7 +336,7 @@ func getCpuListOfDieFunc(args ...interface{}) (interface{}, error) { // wrapper function to get a list of all cpuids of the node func getCpuListOfNode(args ...interface{}) (interface{}, error) { - return topo.CpuList(), nil + return topo.HwthreadList(), nil } // helper function to get the cpuid list for a CCMetric type tag set (type and type-id) @@ -348,14 +348,14 @@ func getCpuListOfType(args ...interface{}) (interface{}, error) { case string: switch typ { case "node": - return topo.CpuList(), nil + return topo.HwthreadList(), nil case "socket": return getCpuListOfSocketFunc(args[1]) case "numadomain": return getCpuListOfNumaDomainFunc(args[1]) case "core": return getCpuListOfCoreFunc(args[1]) - case "cpu": + case "hwthread": var cpu int switch id := args[1].(type) { diff --git a/internal/metricRouter/README.md b/internal/metricRouter/README.md index fe2d64f..ed99b51 100644 --- a/internal/metricRouter/README.md +++ b/internal/metricRouter/README.md @@ -52,6 +52,11 @@ The CCMetric router sits in between the collectors and the sinks and can be used ], "rename_metrics" : { "metric_12345" : "mymetric" + }, + "normalize_units" : true, + "change_unit_prefix" : { + "mem_used" : "G", + "mem_total" : "G" } } ``` @@ -192,6 +197,14 @@ This option takes a list of evaluable conditions and performs them one after the ``` The first line is comparable with the example in `drop_metrics`, it drops all metrics starting with `drop_metric_` and ending with a number. The second line drops all metrics of the first hardware thread (**not** recommended) +# Manipulating the metric units + +## The `normalize_units` option +The cc-metric-collector tries to read the data from the system as it is reported. If available, it tries to read the metric unit from the system as well (e.g. from `/proc/meminfo`). The problem is that, depending on the source, the metric units are named differently. Just think about `byte`, `Byte`, `B`, `bytes`, ... +The [cc-units](https://github.com/ClusterCockpit/cc-units) package provides us a normalization option to use the same metric unit name for all metrics. It this option is set to true, all `unit` meta tags are normalized. + +## The `change_unit_prefix` section +It is often the case that metrics are reported by the system using a rather outdated unit prefix (like `/proc/meminfo` still uses kByte despite current memory sizes are in the GByte range). If you want to change the prefix of a unit, you can do that with the help of [cc-units](https://github.com/ClusterCockpit/cc-units). The setting works on the metric name and requires the new prefix for the metric. The cc-units package determines the scaling factor. # Aggregate metric values of the current interval with the `interval_aggregates` option @@ -239,3 +252,22 @@ Use cases for `interval_aggregates`: } } ``` + +# Order of operations + +The router performs the above mentioned options in a specific order. In order to get the logic you want for a specific metric, it is crucial to know the processing order: + +- Add the `hostname` tag (c) +- Manipulate the timestamp to the interval timestamp (c,r) +- Drop metrics based on `drop_metrics` and `drop_metrics_if` (c,r) +- Add tags based on `add_tags` (c,r) +- Delete tags based on `del_tags` (c,r) +- Rename metric based on `rename_metric` (c,r) + - Add tags based on `add_tags` to still work if the configuration uses the new name (c,r) + - Delete tags based on `del_tags` to still work if the configuration uses the new name (c,r) +- Normalize units when `normalize_units` is set (c,r) +- Convert unit prefix based on `change_unit_prefix` (c,r) + +Legend: +- 'c' if metric is coming from a collector +- 'r' if metric is coming from a receiver diff --git a/internal/metricRouter/metricRouter.go b/internal/metricRouter/metricRouter.go index 8875d0e..2614ced 100644 --- a/internal/metricRouter/metricRouter.go +++ b/internal/metricRouter/metricRouter.go @@ -12,6 +12,7 @@ import ( lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" agg "github.com/ClusterCockpit/cc-metric-collector/internal/metricAggregator" mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker" + units "github.com/ClusterCockpit/cc-units" ) const ROUTER_MAX_FORWARD = 50 @@ -35,6 +36,8 @@ type metricRouterConfig struct { IntervalStamp bool `json:"interval_timestamp"` // Update timestamp periodically by ticker each interval? NumCacheIntervals int `json:"num_cache_intervals"` // Number of intervals of cached metrics for evaluation MaxForward int `json:"max_forward"` // Number of maximal forwarded metrics at one select + NormalizeUnits bool `json:"normalize_units"` // Check unit meta flag and normalize it using cc-units + ChangeUnitPrefix map[string]string `json:"change_unit_prefix"` // Add prefix that should be applied to the metrics dropMetrics map[string]bool // Internal map for O(1) lookup } @@ -207,6 +210,38 @@ func (r *metricRouter) dropMetric(point lp.CCMetric) bool { return false } +func (r *metricRouter) prepareUnit(point lp.CCMetric) bool { + if r.config.NormalizeUnits { + if in_unit, ok := point.GetMeta("unit"); ok { + u := units.NewUnit(in_unit) + if u.Valid() { + point.AddMeta("unit", u.Short()) + } + } + } + if newP, ok := r.config.ChangeUnitPrefix[point.Name()]; ok { + + newPrefix := units.NewPrefix(newP) + + if in_unit, ok := point.GetMeta("unit"); ok && newPrefix != units.InvalidPrefix { + u := units.NewUnit(in_unit) + if u.Valid() { + cclog.ComponentDebug("MetricRouter", "Change prefix to", newP, "for metric", point.Name()) + conv, out_unit := units.GetUnitPrefixFactor(u, newPrefix) + if conv != nil && out_unit.Valid() { + if val, ok := point.GetField("value"); ok { + point.AddField("value", conv(val)) + point.AddMeta("unit", out_unit.Short()) + } + } + } + + } + } + + return true +} + // Start starts the metric router func (r *metricRouter) Start() { // start timer if configured @@ -232,9 +267,11 @@ func (r *metricRouter) Start() { if new, ok := r.config.RenameMetrics[name]; ok { point.SetName(new) point.AddMeta("oldname", name) + r.DoAddTags(point) + r.DoDelTags(point) } - r.DoAddTags(point) - r.DoDelTags(point) + + r.prepareUnit(point) for _, o := range r.outputs { o <- point diff --git a/scripts/cc-metric-collector.init b/scripts/cc-metric-collector.init index 35fa965..acb82eb 100755 --- a/scripts/cc-metric-collector.init +++ b/scripts/cc-metric-collector.init @@ -75,7 +75,7 @@ case "$1" in fi # Start Daemon - start-stop-daemon --start -b --chdir "$WORK_DIR" --user "$CC_USER" -c "$CC_USER" --pidfile "$PID_FILE" --exec $DAEMON -- $DAEMON_OPTS + start-stop-daemon --start -b --chdir "$WORK_DIR" --user "$CC_USER" -c "$CC_USER" --pidfile "$PID_FILE" --exec $DAEMON -- $CC_OPTS return=$? if [ $return -eq 0 ] then diff --git a/scripts/likwid_perfgroup_to_cc_config.py b/scripts/likwid_perfgroup_to_cc_config.py index f1c3451..52959ed 100755 --- a/scripts/likwid_perfgroup_to_cc_config.py +++ b/scripts/likwid_perfgroup_to_cc_config.py @@ -39,7 +39,7 @@ def group_to_json(groupfile): llist = re.split("\s+", line) calc = llist[-1] metric = " ".join(llist[:-1]) - scope = "cpu" + scope = "hwthread" if "BOX" in calc: scope = "socket" if "PWR" in calc: diff --git a/sinks/httpSink.go b/sinks/httpSink.go index 7713638..466915d 100644 --- a/sinks/httpSink.go +++ b/sinks/httpSink.go @@ -19,9 +19,9 @@ type HttpSinkConfig struct { URL string `json:"url,omitempty"` JWT string `json:"jwt,omitempty"` Timeout string `json:"timeout,omitempty"` - MaxIdleConns int `json:"max_idle_connections,omitempty"` IdleConnTimeout string `json:"idle_connection_timeout,omitempty"` FlushDelay string `json:"flush_delay,omitempty"` + MaxRetries int `json:"max_retries,omitempty"` } type HttpSink struct { @@ -32,83 +32,85 @@ type HttpSink struct { buffer *bytes.Buffer flushTimer *time.Timer config HttpSinkConfig - maxIdleConns int idleConnTimeout time.Duration timeout time.Duration flushDelay time.Duration } func (s *HttpSink) Write(m lp.CCMetric) error { - if s.buffer.Len() == 0 && s.flushDelay != 0 { - // This is the first write since the last flush, start the flushTimer! - if s.flushTimer != nil && s.flushTimer.Stop() { - cclog.ComponentDebug(s.name, "unexpected: the flushTimer was already running?") - } - - // Run a batched flush for all lines that have arrived in the last second - s.flushTimer = time.AfterFunc(s.flushDelay, func() { - if err := s.Flush(); err != nil { - cclog.ComponentError(s.name, "flush failed:", err.Error()) - } - }) - } - p := m.ToPoint(s.meta_as_tags) - s.lock.Lock() + firstWriteOfBatch := s.buffer.Len() == 0 _, err := s.encoder.Encode(p) - s.lock.Unlock() // defer does not work here as Flush() takes the lock as well - + s.lock.Unlock() if err != nil { cclog.ComponentError(s.name, "encoding failed:", err.Error()) return err } - // Flush synchronously if "flush_delay" is zero if s.flushDelay == 0 { return s.Flush() } - return err + if firstWriteOfBatch { + if s.flushTimer == nil { + s.flushTimer = time.AfterFunc(s.flushDelay, func() { + if err := s.Flush(); err != nil { + cclog.ComponentError(s.name, "flush failed:", err.Error()) + } + }) + } else { + s.flushTimer.Reset(s.flushDelay) + } + } + + return nil } func (s *HttpSink) Flush() error { - // buffer is read by client.Do, prevent concurrent modifications + // Own lock for as short as possible: the time it takes to copy the buffer. s.lock.Lock() - defer s.lock.Unlock() - - // Do not flush empty buffer - if s.buffer.Len() == 0 { + buf := make([]byte, s.buffer.Len()) + copy(buf, s.buffer.Bytes()) + s.buffer.Reset() + s.lock.Unlock() + if len(buf) == 0 { return nil } - // Create new request to send buffer - req, err := http.NewRequest(http.MethodPost, s.config.URL, s.buffer) - if err != nil { - cclog.ComponentError(s.name, "failed to create request:", err.Error()) - return err + var res *http.Response + for i := 0; i < s.config.MaxRetries; i++ { + // Create new request to send buffer + req, err := http.NewRequest(http.MethodPost, s.config.URL, bytes.NewReader(buf)) + if err != nil { + cclog.ComponentError(s.name, "failed to create request:", err.Error()) + return err + } + + // Set authorization header + if len(s.config.JWT) != 0 { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.config.JWT)) + } + + // Do request + res, err = s.client.Do(req) + if err != nil { + cclog.ComponentError(s.name, "transport/tcp error:", err.Error()) + // Wait between retries + time.Sleep(time.Duration(i+1) * (time.Second / 2)) + continue + } + + break } - // Set authorization header - if len(s.config.JWT) != 0 { - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s.config.JWT)) - } - - // Send - res, err := s.client.Do(req) - - // Clear buffer - s.buffer.Reset() - - // Handle transport/tcp errors - if err != nil { - cclog.ComponentError(s.name, "transport/tcp error:", err.Error()) - return err + if res == nil { + return errors.New("flush failed due to repeated errors") } // Handle application errors if res.StatusCode != http.StatusOK { - err = errors.New(res.Status) + err := errors.New(res.Status) cclog.ComponentError(s.name, "application error:", err.Error()) return err } @@ -128,10 +130,10 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) { s := new(HttpSink) // Set default values s.name = fmt.Sprintf("HttpSink(%s)", name) - s.config.MaxIdleConns = 10 - s.config.IdleConnTimeout = "5s" + s.config.IdleConnTimeout = "120s" // should be larger than the measurement interval. s.config.Timeout = "5s" - s.config.FlushDelay = "1s" + s.config.FlushDelay = "5s" + s.config.MaxRetries = 3 // Read config if len(config) > 0 { @@ -143,9 +145,6 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) { if len(s.config.URL) == 0 { return nil, errors.New("`url` config option is required for HTTP sink") } - if s.config.MaxIdleConns > 0 { - s.maxIdleConns = s.config.MaxIdleConns - } if len(s.config.IdleConnTimeout) > 0 { t, err := time.ParseDuration(s.config.IdleConnTimeout) if err == nil { @@ -170,7 +169,7 @@ func NewHttpSink(name string, config json.RawMessage) (Sink, error) { s.meta_as_tags[k] = true } tr := &http.Transport{ - MaxIdleConns: s.maxIdleConns, + MaxIdleConns: 1, // We will only ever talk to one host. IdleConnTimeout: s.idleConnTimeout, } s.client = &http.Client{Transport: tr, Timeout: s.timeout} diff --git a/sinks/influxAsyncSink.go b/sinks/influxAsyncSink.go index 31d127d..bf88079 100644 --- a/sinks/influxAsyncSink.go +++ b/sinks/influxAsyncSink.go @@ -25,7 +25,6 @@ type InfluxAsyncSinkConfig struct { Password string `json:"password,omitempty"` Organization string `json:"organization,omitempty"` SSL bool `json:"ssl,omitempty"` - RetentionPol string `json:"retention_policy,omitempty"` // Maximum number of points sent to server in single request. Default 5000 BatchSize uint `json:"batch_size,omitempty"` // Interval, in ms, in which is buffer flushed if it has not been already written (by reaching batch size) . Default 1000ms @@ -186,12 +185,17 @@ func NewInfluxAsyncSink(name string, config json.RawMessage) (Sink, error) { return nil, err } } - if len(s.config.Host) == 0 || - len(s.config.Port) == 0 || - len(s.config.Database) == 0 || - len(s.config.Organization) == 0 || - len(s.config.Password) == 0 { - return nil, errors.New("not all configuration variables set required by InfluxAsyncSink") + if len(s.config.Port) == 0 { + return nil, errors.New("Missing port configuration required by InfluxSink") + } + if len(s.config.Database) == 0 { + return nil, errors.New("Missing database configuration required by InfluxSink") + } + if len(s.config.Organization) == 0 { + return nil, errors.New("Missing organization configuration required by InfluxSink") + } + if len(s.config.Password) == 0 { + return nil, errors.New("Missing password configuration required by InfluxSink") } // Create lookup map to use meta infos as tags in the output metric s.meta_as_tags = make(map[string]bool) diff --git a/sinks/influxSink.go b/sinks/influxSink.go index b382c38..212647d 100644 --- a/sinks/influxSink.go +++ b/sinks/influxSink.go @@ -16,37 +16,28 @@ import ( "github.com/influxdata/influxdb-client-go/v2/api/write" ) -type InfluxSinkConfig struct { - defaultSinkConfig - Host string `json:"host,omitempty"` - Port string `json:"port,omitempty"` - Database string `json:"database,omitempty"` - User string `json:"user,omitempty"` - Password string `json:"password,omitempty"` - Organization string `json:"organization,omitempty"` - SSL bool `json:"ssl,omitempty"` - FlushDelay string `json:"flush_delay,omitempty"` - BatchSize int `json:"batch_size,omitempty"` - RetentionPol string `json:"retention_policy,omitempty"` - // InfluxRetryInterval string `json:"retry_interval"` - // InfluxExponentialBase uint `json:"retry_exponential_base"` - // InfluxMaxRetries uint `json:"max_retries"` - // InfluxMaxRetryTime string `json:"max_retry_time"` - //InfluxMaxRetryDelay string `json:"max_retry_delay"` // It is mentioned in the docs but there is no way to set it -} - type InfluxSink struct { sink - client influxdb2.Client - writeApi influxdb2Api.WriteAPIBlocking - config InfluxSinkConfig - influxRetryInterval uint - influxMaxRetryTime uint - batch []*write.Point - flushTimer *time.Timer - flushDelay time.Duration - lock sync.Mutex // Flush() runs in another goroutine, so this lock has to protect the buffer - //influxMaxRetryDelay uint + client influxdb2.Client + writeApi influxdb2Api.WriteAPIBlocking + config struct { + defaultSinkConfig + Host string `json:"host,omitempty"` + Port string `json:"port,omitempty"` + Database string `json:"database,omitempty"` + User string `json:"user,omitempty"` + Password string `json:"password,omitempty"` + Organization string `json:"organization,omitempty"` + SSL bool `json:"ssl,omitempty"` + // Maximum number of points sent to server in single request. Default 100 + BatchSize int `json:"batch_size,omitempty"` + // Interval, in which is buffer flushed if it has not been already written (by reaching batch size). Default 1s + FlushInterval string `json:"flush_delay,omitempty"` + } + batch []*write.Point + flushTimer *time.Timer + flushDelay time.Duration + lock sync.Mutex // Flush() runs in another goroutine, so this lock has to protect the buffer } // connect connects to the InfluxDB server @@ -76,23 +67,6 @@ func (s *InfluxSink) connect() error { // Set influxDB client options clientOptions := influxdb2.DefaultOptions() - // if s.influxRetryInterval != 0 { - // cclog.ComponentDebug(s.name, "MaxRetryInterval", s.influxRetryInterval) - // clientOptions.SetMaxRetryInterval(s.influxRetryInterval) - // } - // if s.influxMaxRetryTime != 0 { - // cclog.ComponentDebug(s.name, "MaxRetryTime", s.influxMaxRetryTime) - // clientOptions.SetMaxRetryTime(s.influxMaxRetryTime) - // } - // if s.config.InfluxExponentialBase != 0 { - // cclog.ComponentDebug(s.name, "Exponential Base", s.config.InfluxExponentialBase) - // clientOptions.SetExponentialBase(s.config.InfluxExponentialBase) - // } - // if s.config.InfluxMaxRetries != 0 { - // cclog.ComponentDebug(s.name, "Max Retries", s.config.InfluxMaxRetries) - // clientOptions.SetMaxRetries(s.config.InfluxMaxRetries) - // } - // Do not check InfluxDB certificate clientOptions.SetTLSConfig( &tls.Config{ @@ -126,11 +100,13 @@ func (s *InfluxSink) Write(m lp.CCMetric) error { } // Run a batched flush for all lines that have arrived in the last flush delay interval - s.flushTimer = time.AfterFunc(s.flushDelay, func() { - if err := s.Flush(); err != nil { - cclog.ComponentError(s.name, "flush failed:", err.Error()) - } - }) + s.flushTimer = time.AfterFunc( + s.flushDelay, + func() { + if err := s.Flush(); err != nil { + cclog.ComponentError(s.name, "flush failed:", err.Error()) + } + }) } // Append metric to batch slice @@ -194,7 +170,7 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) { // Set config default values s.config.BatchSize = 100 - s.config.FlushDelay = "1s" + s.config.FlushInterval = "1s" // Read config if len(config) > 0 { @@ -203,12 +179,6 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) { return nil, err } } - s.influxRetryInterval = 0 - s.influxMaxRetryTime = 0 - // s.config.InfluxRetryInterval = "" - // s.config.InfluxMaxRetryTime = "" - // s.config.InfluxMaxRetries = 0 - // s.config.InfluxExponentialBase = 0 if len(s.config.Host) == 0 { return nil, errors.New("Missing host configuration required by InfluxSink") @@ -232,21 +202,9 @@ func NewInfluxSink(name string, config json.RawMessage) (Sink, error) { s.meta_as_tags[k] = true } - // toUint := func(duration string, def uint) uint { - // if len(duration) > 0 { - // t, err := time.ParseDuration(duration) - // if err == nil { - // return uint(t.Milliseconds()) - // } - // } - // return def - // } - // s.influxRetryInterval = toUint(s.config.InfluxRetryInterval, s.influxRetryInterval) - // s.influxMaxRetryTime = toUint(s.config.InfluxMaxRetryTime, s.influxMaxRetryTime) - // Configure flush delay duration - if len(s.config.FlushDelay) > 0 { - t, err := time.ParseDuration(s.config.FlushDelay) + if len(s.config.FlushInterval) > 0 { + t, err := time.ParseDuration(s.config.FlushInterval) if err == nil { s.flushDelay = t } diff --git a/sinks/natsSink.go b/sinks/natsSink.go index 0597e9b..4d43454 100644 --- a/sinks/natsSink.go +++ b/sinks/natsSink.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "sync" "time" cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" @@ -15,11 +16,12 @@ import ( type NatsSinkConfig struct { defaultSinkConfig - Host string `json:"host,omitempty"` - Port string `json:"port,omitempty"` - Database string `json:"database,omitempty"` - User string `json:"user,omitempty"` - Password string `json:"password,omitempty"` + Host string `json:"host,omitempty"` + Port string `json:"port,omitempty"` + Subject string `json:"subject,omitempty"` + User string `json:"user,omitempty"` + Password string `json:"password,omitempty"` + FlushDelay string `json:"flush_delay,omitempty"` } type NatsSink struct { @@ -28,6 +30,10 @@ type NatsSink struct { encoder *influx.Encoder buffer *bytes.Buffer config NatsSinkConfig + + lock sync.Mutex + flushDelay time.Duration + flushTimer *time.Timer } func (s *NatsSink) connect() error { @@ -54,37 +60,53 @@ func (s *NatsSink) connect() error { } func (s *NatsSink) Write(m lp.CCMetric) error { - if s.client != nil { - _, err := s.encoder.Encode(m.ToPoint(s.meta_as_tags)) - if err != nil { - cclog.ComponentError(s.name, "Write:", err.Error()) - return err - } + s.lock.Lock() + _, err := s.encoder.Encode(m.ToPoint(s.meta_as_tags)) + s.lock.Unlock() + if err != nil { + cclog.ComponentError(s.name, "Write:", err.Error()) + return err } + + if s.flushDelay == 0 { + s.Flush() + } else if s.flushTimer == nil { + s.flushTimer = time.AfterFunc(s.flushDelay, func() { + s.Flush() + }) + } else { + s.flushTimer.Reset(s.flushDelay) + } + return nil } func (s *NatsSink) Flush() error { - if s.client != nil { - if err := s.client.Publish(s.config.Database, s.buffer.Bytes()); err != nil { - cclog.ComponentError(s.name, "Flush:", err.Error()) - return err - } - s.buffer.Reset() + s.lock.Lock() + buf := append([]byte{}, s.buffer.Bytes()...) // copy bytes + s.buffer.Reset() + s.lock.Unlock() + + if len(buf) == 0 { + return nil + } + + if err := s.client.Publish(s.config.Subject, buf); err != nil { + cclog.ComponentError(s.name, "Flush:", err.Error()) + return err } return nil } func (s *NatsSink) Close() { - if s.client != nil { - cclog.ComponentDebug(s.name, "Close") - s.client.Close() - } + cclog.ComponentDebug(s.name, "Close") + s.client.Close() } func NewNatsSink(name string, config json.RawMessage) (Sink, error) { s := new(NatsSink) s.name = fmt.Sprintf("NatsSink(%s)", name) + s.flushDelay = 10 * time.Second if len(config) > 0 { err := json.Unmarshal(config, &s.config) if err != nil { @@ -94,7 +116,7 @@ func NewNatsSink(name string, config json.RawMessage) (Sink, error) { } if len(s.config.Host) == 0 || len(s.config.Port) == 0 || - len(s.config.Database) == 0 { + len(s.config.Subject) == 0 { return nil, errors.New("not all configuration variables set required by NatsSink") } // Create lookup map to use meta infos as tags in the output metric @@ -112,5 +134,15 @@ func NewNatsSink(name string, config json.RawMessage) (Sink, error) { if err := s.connect(); err != nil { return nil, fmt.Errorf("unable to connect: %v", err) } + + s.flushTimer = nil + if len(s.config.FlushDelay) != 0 { + var err error + s.flushDelay, err = time.ParseDuration(s.config.FlushDelay) + if err != nil { + return nil, err + } + } + return s, nil } From 0d31ec481b4a9439ea17d0decc71bf7c1c46b0a3 Mon Sep 17 00:00:00 2001 From: Thomas Gruber Date: Thu, 9 Jun 2022 12:42:11 +0200 Subject: [PATCH 6/7] Update Release.yml --- .github/workflows/Release.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 9ce5615..191f6d6 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -40,7 +40,9 @@ jobs: # Use dnf to install build dependencies - name: Install build dependencies - run: dnf --assumeyes builddep scripts/cc-metric-collector.spec + run: | + dnf --assumeyes install 'dnf-command(builddep)' + dnf --assumeyes builddep scripts/cc-metric-collector.spec - name: RPM build MetricCollector id: rpmbuild @@ -181,4 +183,4 @@ jobs: ${{ steps.files.outputs.ALMA_85_RPM }} ${{ steps.files.outputs.ALMA_85_SRPM }} ${{ steps.files.outputs.UBI_8_RPM }} - ${{ steps.files.outputs.UBI_8_SRPM }} \ No newline at end of file + ${{ steps.files.outputs.UBI_8_SRPM }} From 31a38bc17d51d0f8f0f686b829cc4f96e1b87a01 Mon Sep 17 00:00:00 2001 From: Thomas Roehl Date: Thu, 9 Jun 2022 14:36:25 +0200 Subject: [PATCH 7/7] Update release action --- .github/workflows/Release.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 191f6d6..f75a167 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -42,12 +42,13 @@ jobs: - name: Install build dependencies run: | dnf --assumeyes install 'dnf-command(builddep)' + dnf --assumeyes install which dnf --assumeyes builddep scripts/cc-metric-collector.spec - name: RPM build MetricCollector id: rpmbuild run: make RPM - + # AlmaLinux 8.5 is a derivate of RedHat Enterprise Linux 8 (UBI8), # so the created RPM both contain the substring 'el8' in the RPM file names # This step replaces the substring 'el8' to 'alma85'. It uses the move operation