You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by "winterhazel (via GitHub)" <gi...@apache.org> on 2024/01/15 17:41:05 UTC

[PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

winterhazel opened a new pull request, #8511:
URL: https://github.com/apache/cloudstack/pull/8511

   ### Description
   
   The class `LibvirtComputingResource` does not provide enough information about what is happening during the metrics collection process and contains some logs with formatting issues. 
   
   This PR refactors the metrics collection process while adding logs, and fixes some logs that contained problems.
   
   ### Types of changes
   
   - [ ] Breaking change (fix or feature that would cause existing functionality to change)
   - [ ] New feature (non-breaking change which adds functionality)
   - [ ] Bug fix (non-breaking change which fixes an issue)
   - [X] Enhancement (improves an existing feature and functionality)
   - [ ] Cleanup (Code refactoring and cleanup, that may add test cases)
   
   ### Feature/Enhancement Scale or Bug Severity
   
   #### Feature/Enhancement Scale
   
   - [ ] Major
   - [X] Minor
   
   
   ### Screenshots (if appropriate):
   
   
   ### How Has This Been Tested?
   
   I manually verified the logs in the agent. Regarding the refactoring, I adapted the unit tests to a commit before the refactoring and verified that the metrics had the same values before and after the changes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1893389301

   @blueorangutan package


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1943413754

   @blueorangutan test


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1976385553

   @DaanHoogland a [SL] Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2146953756

   <b>[SF] Trillian test result (tid-10331)</b>
   Environment: kvm-centos7 (x2), Advanced Networking with Mgmt server 7
   Total time taken: 82750 seconds
   Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr8511-t10331-kvm-centos7.zip
   Smoke tests completed. 96 look OK, 36 have errors, 0 did not run
   Only failed and skipped tests results shown below:
   
   
   Test | Result | Time (s) | Test File
   --- | --- | --- | ---
   test_01_events_resource | `Error` | 417.65 | test_events_resource.py
   test_01_events_resource | `Error` | 417.67 | test_events_resource.py
   ContextSuite context=TestAddVmToSubDomain>:teardown | `Error` | 31.06 | test_accounts.py
   test_DeleteDomain | `Error` | 194.56 | test_accounts.py
   test_forceDeleteDomain | `Failure` | 370.76 | test_accounts.py
   test_forceDeleteDomain | `Error` | 370.77 | test_accounts.py
   ContextSuite context=TestNonRootAdminsPrivileges>:teardown | `Error` | 8.07 | test_accounts.py
   ContextSuite context=TestServiceOfferingHierarchy>:teardown | `Error` | 3.30 | test_accounts.py
   ContextSuite context=TestServiceOfferingSiblings>:teardown | `Error` | 3.28 | test_accounts.py
   ContextSuite context=TestTemplateHierarchy>:teardown | `Error` | 4.35 | test_accounts.py
   ContextSuite context=TestUserAPIKeys>:teardown | `Error` | 2.75 | test_accounts.py
   test_LoginApiDomain | `Error` | 4.54 | test_accounts.py
   test_delete_global_acl | `Failure` | 2.56 | test_global_acls.py
   test_replace_acl_of_network | `Error` | 46.65 | test_global_acls.py
   test_01_create_list_domain_account_project | `Error` | 7.21 | test_create_list_domain_account_project.py
   test_01_internallb_roundrobin_1VPC_3VM_HTTP_port80 | `Failure` | 3.64 | test_internal_lb.py
   test_02_internallb_roundrobin_1RVPC_3VM_HTTP_port80 | `Failure` | 3.63 | test_internal_lb.py
   test_03_vpc_internallb_haproxy_stats_on_all_interfaces | `Failure` | 2.52 | test_internal_lb.py
   test_04_rvpc_internallb_haproxy_stats_on_all_interfaces | `Failure` | 2.52 | test_internal_lb.py
   test_deploy_more_vms_than_limit_allows | `Error` | 113.55 | test_deploy_vms_in_parallel.py
   test_deployvm_userconcentrated | `Error` | 36.48 | test_deploy_vms_with_varied_deploymentplanners.py
   test_deployvm_userdispersing | `Error` | 0.09 | test_deploy_vms_with_varied_deploymentplanners.py
   ContextSuite context=TestListAccounts>:teardown | `Error` | 4.35 | test_list_accounts.py
   test_04_list_domains_level_filter | `Failure` | 0.02 | test_list_domains.py
   test_05_list_domains_no_filter | `Failure` | 0.16 | test_list_domains.py
   ContextSuite context=TestListDomains>:teardown | `Error` | 3.39 | test_list_domains.py
   ContextSuite context=TestDomainsDiskOfferings>:teardown | `Error` | 17.24 | test_domain_disk_offerings.py
   ContextSuite context=TestDomainsNetworkOfferings>:teardown | `Error` | 13.84 | test_domain_network_offerings.py
   ContextSuite context=TestDomainsServiceOfferings>:teardown | `Error` | 311.89 | test_domain_service_offerings.py
   ContextSuite context=TestDomainsVpcOfferings>:teardown | `Error` | 154.34 | test_domain_vpc_offerings.py
   test_01_disable_account_settings_for_domain | `Error` | 4.51 | test_enable_account_settings_for_domain.py
   test_02_enable_account_settings_for_domain | `Error` | 4.65 | test_enable_account_settings_for_domain.py
   test_03_enable_account_settings_for_domain | `Error` | 4.48 | test_enable_account_settings_for_domain.py
   ContextSuite context=TestHostControlState>:teardown | `Error` | 34.63 | test_host_control_state.py
   ContextSuite context=TestImportAndUnmanageVolumes>:teardown | `Error` | 26.37 | test_import_unmanage_volumes.py
   test_03_deploy_and_scale_kubernetes_cluster | `Failure` | 26.76 | test_kubernetes_clusters.py
   test_08_upgrade_kubernetes_ha_cluster | `Failure` | 93.60 | test_kubernetes_clusters.py
   ContextSuite context=TestListIdsParams>:setup | `Error` | 0.00 | test_list_ids_parameter.py
   ContextSuite context=TestListVolumes>:teardown | `Error` | 15.73 | test_list_volumes.py
   ContextSuite context=TestLoadBalance>:setup | `Error` | 0.00 | test_loadbalance.py
   test_network_acl | `Error` | 1.16 | test_network_acl.py
   ContextSuite context=TestNetworkPermissions>:teardown | `Error` | 18.23 | test_network_permissions.py
   ContextSuite context=TestNonStrictAffinityGroups>:teardown | `Error` | 179.37 | test_nonstrict_affinity_group.py
   ContextSuite context=TestCrossDomainAccountAdd>:teardown | `Error` | 6.62 | test_projects.py
   test_04_delete_domain_with_project | `Error` | 2.23 | test_projects.py
   test_CRUD_operations_userdata | `Error` | 5.49 | test_register_userdata.py
   test_deploy_vm_with_registered_userdata | `Error` | 5.21 | test_register_userdata.py
   test_deploy_vm_with_registered_userdata_with_override_policy_allow | `Error` | 5.39 | test_register_userdata.py
   test_deploy_vm_with_registered_userdata_with_override_policy_append | `Error` | 5.09 | test_register_userdata.py
   test_deploy_vm_with_registered_userdata_with_override_policy_deny | `Error` | 4.93 | test_register_userdata.py
   test_deploy_vm_with_registered_userdata_with_params | `Error` | 4.97 | test_register_userdata.py
   test_link_and_unlink_userdata_to_template | `Error` | 4.96 | test_register_userdata.py
   test_user_userdata_crud | `Error` | 4.96 | test_register_userdata.py
   test_02_test_settings_for_account | `Error` | 0.02 | test_reset_configuration_settings.py
   ContextSuite context=TestRAMCPUResourceAccounting>:teardown | `Error` | 145.09 | test_resource_accounting.py
   test_06_disk_offering_strictness_false | `Error` | 0.12 | test_service_offerings.py
   test_01_positive_tests_usage | `Error` | 11.04 | test_usage_events.py
   ContextSuite context=TestVmAutoScaling>:setup | `Error` | 0.00 | test_vm_autoscaling.py
   test_advZoneVirtualRouter | `Error` | 0.02 | test_vm_life_cycle.py
   test_deploy_vm_multiple | `Error` | 98.52 | test_vm_life_cycle.py
   ContextSuite context=TestVMLifeCycle>:setup | `Error` | 143.00 | test_vm_life_cycle.py
   ContextSuite context=TestVnfTemplates>:teardown | `Error` | 3.38 | test_vnf_templates.py
   test_01_verify_ipv6_vpc | `Error` | 450.87 | test_vpc_ipv6.py
   test_01_create_redundant_VPC_2tiers_4VMs_4IPs_4PF_ACL | `Failure` | 228.50 | test_vpc_redundant.py
   test_02_redundant_VPC_default_routes | `Failure` | 220.38 | test_vpc_redundant.py
   test_05_rvpc_multi_tiers | `Failure` | 213.35 | test_vpc_redundant.py
   test_05_rvpc_multi_tiers | `Error` | 213.36 | test_vpc_redundant.py
   test_01_VPC_nics_after_destroy | `Failure` | 212.94 | test_vpc_router_nics.py
   test_01_redundant_vpc_site2site_vpn | `Failure` | 2.43 | test_vpc_vpn.py
   test_01_vpc_site2site_vpn_multiple_options | `Failure` | 2.32 | test_vpc_vpn.py
   test_01_vpc_remote_access_vpn | `Failure` | 0.06 | test_vpc_vpn.py
   test_01_vpc_site2site_vpn | `Failure` | 3.32 | test_vpc_vpn.py
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1967312105

   @GutoVeronezi @JoaoJandre, I have made some changes in the logs, what do you guys think?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1934110620

   This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1948704455

   @DaanHoogland yes, some users have already testes these changes. I can try asking someone to post their results here.
   
   Though, looking back at this PR, I think I may have overwhelmed the debug level with too many messages for this process, so I will adjust some of these logs first so we can also get feedback on this.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1894791261

   <b>[SF] Trillian test result (tid-8847)</b>
   Environment: kvm-alma8 (x2), Advanced Networking with Mgmt server a8
   Total time taken: 50681 seconds
   Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr8511-t8847-kvm-alma8.zip
   Smoke tests completed. 121 look OK, 0 have errors, 0 did not run
   Only failed and skipped tests results shown below:
   
   
   Test | Result | Time (s) | Test File
   --- | --- | --- | ---
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2141971338

   @blueorangutan package


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1976026565

   @blueorangutan package


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1977729767

   <b>[SF] Trillian test result (tid-9379)</b>
   Environment: kvm-centos7 (x2), Advanced Networking with Mgmt server 7
   Total time taken: 44923 seconds
   Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr8511-t9379-kvm-centos7.zip
   Smoke tests completed. 128 look OK, 1 have errors, 0 did not run
   Only failed and skipped tests results shown below:
   
   
   Test | Result | Time (s) | Test File
   --- | --- | --- | ---
   test_02_list_cpvm_vm | `Failure` | 0.04 | test_ssvm.py
   test_04_cpvm_internals | `Failure` | 0.03 | test_ssvm.py
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2139729551

   This pull request has merge conflicts. Dear author, please fix the conflicts and sync your branch with the base branch.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "GutoVeronezi (via GitHub)" <gi...@apache.org>.
GutoVeronezi commented on code in PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#discussion_r1491606011


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java:
##########
@@ -2309,7 +2311,7 @@ public PowerState getVmState(final Connect conn, final String vmName) {
                 final PowerState s = convertToPowerState(vms.getInfo().state);
                 return s;
             } catch (final LibvirtException e) {
-                LOGGER.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
+                LOGGER.error(String.format("Can't get state for VM [%s] (retry=%s).", vmName, retry), e);

Review Comment:
   ```suggestion
                   LOGGER.error("Can't get state for VM [{}] (retry={}).", vmName, retry, e);
   ```



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtExtendedVmStatsEntry.java:
##########
@@ -0,0 +1,51 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.hypervisor.kvm.resource;
+
+import com.cloud.agent.api.VmStatsEntry;
+import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
+
+import java.util.Calendar;
+
+public class LibvirtExtendedVmStatsEntry extends VmStatsEntry {
+    private long cpuTime;
+    private Calendar timestamp;
+
+    public LibvirtExtendedVmStatsEntry() {
+    }
+
+    public long getCpuTime() {
+        return cpuTime;
+    }
+
+    public void setCpuTime(long cpuTime) {
+        this.cpuTime = cpuTime;
+    }
+
+    public Calendar getTimestamp() {
+        return timestamp;
+    }
+
+    public void setTimestamp(Calendar timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    @Override
+    public String toString() {
+        return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "cpuTime", "networkWriteKBs", "networkReadKBs", "diskReadIOs", "diskWriteIOs", "diskReadKBs", "diskWriteKBs");

Review Comment:
   We could use `ReflectionToStringBuilder.toStringExclude()` if the number of fields omitted is smaller than the printed.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java:
##########
@@ -4388,127 +4390,193 @@ protected String getDiskPathFromDiskDef(DiskDef disk) {
         return null;
     }
 
-    private class VmStats {
-        long usedTime;
-        long tx;
-        long rx;
-        long ioRead;
-        long ioWrote;
-        long bytesRead;
-        long bytesWrote;
-        Calendar timestamp;
+    private String vmToString(Domain dm) throws LibvirtException {
+        return String.format("{\"name\":\"%s\",\"uuid\":\"%s\"}", dm.getName(), dm.getUUIDString());
     }
 
+    /**
+     * Returns metrics for the period since this function was last called for the specified VM.
+     * @param conn the Libvirt connection.
+     * @param vmName name of the VM.
+     * @return metrics for the period since last time this function was called for the VM.
+     * @throws LibvirtException
+     */
     public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
         Domain dm = null;
         try {
+            LOGGER.debug("Trying to get VM with name [{}].", vmName);
             dm = getDomain(conn, vmName);
             if (dm == null) {
+                LOGGER.warn("Could not get VM with name [{}].", vmName);
                 return null;
             }
-            DomainInfo info = dm.getInfo();
-            final VmStatsEntry stats = new VmStatsEntry();
 
-            stats.setNumCPUs(info.nrVirtCpu);
-            stats.setEntityType("vm");
+            LibvirtExtendedVmStatsEntry newStats = getVmCurrentStats(dm);
+            LibvirtExtendedVmStatsEntry oldStats = vmStats.get(vmName);
 
-            stats.setMemoryKBs(info.maxMem);
-            stats.setTargetMemoryKBs(info.memory);
-            stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+            VmStatsEntry metrics = calculateVmMetrics(dm, oldStats, newStats);
 
-            /* get cpu utilization */
-            VmStats oldStats = null;
+            String vmAsString = vmToString(dm);
+            LOGGER.info("Saving stats for VM [{}].", vmAsString);
+            vmStats.put(vmName, newStats);
+            LOGGER.debug("Saved stats for VM [{}]: [{}].", vmAsString, newStats);
 
-            final Calendar now = Calendar.getInstance();
+            return metrics;
+        } finally {
+            if (dm != null) {
+                dm.free();
+            }
+        }
+    }
 
-            oldStats = vmStats.get(vmName);
+    /**
+     * Returns a VM's current statistics.
+     * @param dm domain of the VM.
+     * @return current statistics of the VM.
+     * @throws LibvirtException
+     */
+    protected LibvirtExtendedVmStatsEntry getVmCurrentStats(final Domain dm) throws LibvirtException {
+        final LibvirtExtendedVmStatsEntry stats = new LibvirtExtendedVmStatsEntry();
 
-            long elapsedTime = 0;
-            if (oldStats != null) {
-                elapsedTime = now.getTimeInMillis() - oldStats.timestamp.getTimeInMillis();
-                double utilization = (info.cpuTime - oldStats.usedTime) / ((double)elapsedTime * 1000000);
+        getVmCurrentCpuStats(dm, stats);
+        getVmCurrentNetworkStats(dm, stats);
+        getVmCurrentDiskStats(dm, stats);
 
-                utilization = utilization / info.nrVirtCpu;
-                if (utilization > 0) {
-                    stats.setCPUUtilization(utilization * 100);
-                }
-            }
+        LOGGER.debug("Retrieved statistics for VM [{}]: [{}].", vmToString(dm), stats);
+        stats.setTimestamp(Calendar.getInstance());
+        return stats;
+    }
 
-            /* get network stats */
+    /**
+     * Passes a VM's current CPU statistics into the provided LibvirtExtendedVmStatsEntry.
+     * @param dm domain of the VM.
+     * @param stats LibvirtExtendedVmStatsEntry that will receive the current CPU statistics.
+     * @throws LibvirtException
+     */
+    protected void getVmCurrentCpuStats(final Domain dm, final LibvirtExtendedVmStatsEntry stats) throws LibvirtException {
+        LOGGER.debug("Getting CPU stats for VM [{}].", vmToString(dm));
+        stats.setCpuTime(dm.getInfo().cpuTime);
+    }
 
-            final List<InterfaceDef> vifs = getInterfaces(conn, vmName);
-            long rx = 0;
-            long tx = 0;
-            for (final InterfaceDef vif : vifs) {
-                final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
-                rx += ifStats.rx_bytes;
-                tx += ifStats.tx_bytes;
-            }
+    /**
+     * Passes a VM's current network statistics into the provided LibvirtExtendedVmStatsEntry.
+     * @param dm domain of the VM.
+     * @param stats LibvirtExtendedVmStatsEntry that will receive the current network statistics.
+     * @throws LibvirtException
+     */
+    protected void getVmCurrentNetworkStats(final Domain dm, final LibvirtExtendedVmStatsEntry stats) throws LibvirtException {
+        final String vmAsString = vmToString(dm);
+        LOGGER.debug("Getting network stats for VM [{}].", vmAsString);
+        final List<InterfaceDef> vifs = getInterfaces(dm.getConnect(), dm.getName());
+        LOGGER.debug("Found [{}] network interface(s) for VM [{}].", vifs.size(), vmAsString);
+        double rx = 0;
+        double tx = 0;
+        for (final InterfaceDef vif : vifs) {
+            final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
+            rx += ifStats.rx_bytes;
+            tx += ifStats.tx_bytes;
+        }
+        stats.setNetworkReadKBs(rx / 1024);
+        stats.setNetworkWriteKBs(tx / 1024);
+    }
 
-            if (oldStats != null) {
-                final double deltarx = rx - oldStats.rx;
-                if (deltarx > 0) {
-                    stats.setNetworkReadKBs(deltarx / 1024);
-                }
-                final double deltatx = tx - oldStats.tx;
-                if (deltatx > 0) {
-                    stats.setNetworkWriteKBs(deltatx / 1024);
-                }
+    /**
+     * Passes a VM's current disk statistics into the provided LibvirtExtendedVmStatsEntry.
+     * @param dm domain of the VM.
+     * @param stats LibvirtExtendedVmStatsEntry that will receive the current disk statistics.
+     * @throws LibvirtException
+     */
+    protected void getVmCurrentDiskStats(final Domain dm, final LibvirtExtendedVmStatsEntry stats) throws LibvirtException {
+        final String vmAsString = vmToString(dm);
+        LOGGER.debug("Getting disk stats for VM [{}].", vmAsString);
+        final List<DiskDef> disks = getDisks(dm.getConnect(), dm.getName());
+        LOGGER.debug("Found [{}] disk(s) for VM [{}].", disks.size(), vmAsString);
+        long io_rd = 0;
+        long io_wr = 0;
+        double bytes_rd = 0;
+        double bytes_wr = 0;
+        for (final DiskDef disk : disks) {
+            if (disk.getDeviceType() == DeviceType.CDROM || disk.getDeviceType() == DeviceType.FLOPPY) {
+                LOGGER.debug("Ignoring disk [{}] in VM [{}]'s stats since its deviceType is [{}].", disk.toString().replace("\n", ""), vmAsString, disk.getDeviceType());
+                continue;
             }
+            final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
+            io_rd += blockStats.rd_req;
+            io_wr += blockStats.wr_req;
+            bytes_rd += blockStats.rd_bytes;
+            bytes_wr += blockStats.wr_bytes;
+        }
+        stats.setDiskReadIOs(io_rd);
+        stats.setDiskWriteIOs(io_wr);
+        stats.setDiskReadKBs(bytes_rd / 1024);
+        stats.setDiskWriteKBs(bytes_wr / 1024);
+    }
 
-            /* get disk stats */
-            final List<DiskDef> disks = getDisks(conn, vmName);
-            long io_rd = 0;
-            long io_wr = 0;
-            long bytes_rd = 0;
-            long bytes_wr = 0;
-            for (final DiskDef disk : disks) {
-                if (disk.getDeviceType() == DeviceType.CDROM || disk.getDeviceType() == DeviceType.FLOPPY) {
-                    continue;
-                }
-                final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
-                io_rd += blockStats.rd_req;
-                io_wr += blockStats.wr_req;
-                bytes_rd += blockStats.rd_bytes;
-                bytes_wr += blockStats.wr_bytes;
+    /**
+     * Calculates a VM's metrics for the period between the two statistics given as parameters.
+     * @param dm domain of the VM.
+     * @param oldStats old statistics. If null, the CPU, network and disk utilization won't be calculated.
+     * @param newStats new statistics.
+     * @return metrics for the period between the two statistics.
+     * @throws LibvirtException
+     */
+    protected VmStatsEntry calculateVmMetrics(final Domain dm, final LibvirtExtendedVmStatsEntry oldStats, final LibvirtExtendedVmStatsEntry newStats) throws LibvirtException {
+        final VmStatsEntry metrics = new VmStatsEntry();
+        final DomainInfo info = dm.getInfo();
+        final String vmAsString = vmToString(dm);
+
+        metrics.setEntityType("vm");
+        LOGGER.debug("Writing VM [{}]'s CPU and memory information into the metrics.", vmAsString);
+        metrics.setNumCPUs(info.nrVirtCpu);
+        metrics.setMemoryKBs(info.maxMem);
+        metrics.setTargetMemoryKBs(info.memory);
+        LOGGER.debug("Trying to get free memory for VM [{}].", vmAsString);
+        metrics.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+
+        if (oldStats != null) {
+            LOGGER.debug("Old stats exist for VM [{}]; therefore, the utilization will be calculated.", vmAsString);
+
+            LOGGER.debug("Calculating CPU utilization for VM [{}].", vmAsString);

Review Comment:
   We could unify these logs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "JoaoJandre (via GitHub)" <gi...@apache.org>.
JoaoJandre commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2247738854

   @blueorangutan package


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2293609158

   @DaanHoogland could we rerun the CI here?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "JoaoJandre (via GitHub)" <gi...@apache.org>.
JoaoJandre merged PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2247742203

   @JoaoJandre a [SL] Jenkins job has been kicked to build packages. It will be bundled with  KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1968415334

   > @DaanHoogland yes, some users have already tested these changes. I can try asking someone to post their results here.
   
   i think this is ready otherwise.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on code in PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#discussion_r1473133257


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java:
##########
@@ -4401,127 +4403,193 @@ protected String getDiskPathFromDiskDef(DiskDef disk) {
         return null;
     }
 
-    private class VmStats {
-        long usedTime;
-        long tx;
-        long rx;
-        long ioRead;
-        long ioWrote;
-        long bytesRead;
-        long bytesWrote;
-        Calendar timestamp;
+    private String vmToString(Domain dm) throws LibvirtException {
+        return String.format("{\"name\":\"%s\",\"uuid\":\"%s\"}", dm.getName(), dm.getUUIDString());
     }
 
+    /**
+     * Returns metrics for the period since this function was last called for the specified VM.
+     * @param conn the Libvirt connection.
+     * @param vmName name of the VM.
+     * @return metrics for the period since last time this function was called for the VM.
+     * @throws LibvirtException
+     */
     public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
         Domain dm = null;
         try {
+            s_logger.debug(String.format("Trying to get VM with name [%s].", vmName));
             dm = getDomain(conn, vmName);
             if (dm == null) {
+                s_logger.warn(String.format("Could not get VM with name [%s].", vmName));
                 return null;
             }
-            DomainInfo info = dm.getInfo();
-            final VmStatsEntry stats = new VmStatsEntry();
 
-            stats.setNumCPUs(info.nrVirtCpu);
-            stats.setEntityType("vm");
+            LibvirtExtendedVmStatsEntry newStats = getVmCurrentStats(dm);
+            LibvirtExtendedVmStatsEntry oldStats = vmStats.get(vmName);
 
-            stats.setMemoryKBs(info.maxMem);
-            stats.setTargetMemoryKBs(info.memory);
-            stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+            VmStatsEntry metrics = calculateVmMetrics(dm, oldStats, newStats);
 
-            /* get cpu utilization */
-            VmStats oldStats = null;
+            String vmAsString = vmToString(dm);
+            s_logger.info(String.format("Saving stats for VM [%s].", vmAsString));
+            vmStats.put(vmName, newStats);
+            s_logger.debug(String.format("Saved stats for VM [%s]: [%s].", vmAsString, newStats));

Review Comment:
   I think debug is the appropriate level for the second log, since it presents information more useful for debugging purposes instead of everyday operation. Also, it has a lot of data and the metrics collection is quite frequent, so it can end up overwhelming the info level.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1941963045

   @blueorangutan package


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1893510180

   @DaanHoogland a [SL] Trillian-Jenkins test job (alma8 mgmt + kvm-alma8) has been kicked to run smoke tests


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1892611289

   ## [Codecov](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   Attention: `113 lines` in your changes are missing coverage. Please review.
   > Comparison is base [(`c3b77cb`)](https://app.codecov.io/gh/apache/cloudstack/commit/c3b77cb7b82bfe8e94b024e81e1721532da51a9c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) 18.28% compared to head [(`9035016`)](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) 15.92%.
   
   | [Files](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [...ervisor/kvm/resource/LibvirtComputingResource.java](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGx1Z2lucy9oeXBlcnZpc29ycy9rdm0vc3JjL21haW4vamF2YS9jb20vY2xvdWQvaHlwZXJ2aXNvci9rdm0vcmVzb3VyY2UvTGlidmlydENvbXB1dGluZ1Jlc291cmNlLmphdmE=) | 0.00% | [104 Missing :warning: ](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [...isor/kvm/resource/LibvirtExtendedVmStatsEntry.java](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGx1Z2lucy9oeXBlcnZpc29ycy9rdm0vc3JjL21haW4vamF2YS9jb20vY2xvdWQvaHlwZXJ2aXNvci9rdm0vcmVzb3VyY2UvTGlidmlydEV4dGVuZGVkVm1TdGF0c0VudHJ5LmphdmE=) | 0.00% | [9 Missing :warning: ](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   ```diff
   @@             Coverage Diff              @@
   ##               main    #8511      +/-   ##
   ============================================
   - Coverage     18.28%   15.92%   -2.37%     
   + Complexity    16826    14202    -2624     
   ============================================
     Files          4848     4849       +1     
     Lines        324301   324338      +37     
     Branches      45561    45559       -2     
   ============================================
   - Hits          59290    51640    -7650     
   - Misses       256437   265499    +9062     
   + Partials       8574     7199    -1375     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/cloudstack/pull/8511/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [simulator-marvin-tests](https://app.codecov.io/gh/apache/cloudstack/pull/8511/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `15.92% <0.00%> (-2.37%)` | :arrow_down: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/cloudstack/pull/8511?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "JoaoJandre (via GitHub)" <gi...@apache.org>.
JoaoJandre commented on code in PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#discussion_r1472716226


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java:
##########
@@ -4401,127 +4403,193 @@ protected String getDiskPathFromDiskDef(DiskDef disk) {
         return null;
     }
 
-    private class VmStats {
-        long usedTime;
-        long tx;
-        long rx;
-        long ioRead;
-        long ioWrote;
-        long bytesRead;
-        long bytesWrote;
-        Calendar timestamp;
+    private String vmToString(Domain dm) throws LibvirtException {
+        return String.format("{\"name\":\"%s\",\"uuid\":\"%s\"}", dm.getName(), dm.getUUIDString());
     }
 
+    /**
+     * Returns metrics for the period since this function was last called for the specified VM.
+     * @param conn the Libvirt connection.
+     * @param vmName name of the VM.
+     * @return metrics for the period since last time this function was called for the VM.
+     * @throws LibvirtException
+     */
     public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
         Domain dm = null;
         try {
+            s_logger.debug(String.format("Trying to get VM with name [%s].", vmName));
             dm = getDomain(conn, vmName);
             if (dm == null) {
+                s_logger.warn(String.format("Could not get VM with name [%s].", vmName));
                 return null;
             }
-            DomainInfo info = dm.getInfo();
-            final VmStatsEntry stats = new VmStatsEntry();
 
-            stats.setNumCPUs(info.nrVirtCpu);
-            stats.setEntityType("vm");
+            LibvirtExtendedVmStatsEntry newStats = getVmCurrentStats(dm);
+            LibvirtExtendedVmStatsEntry oldStats = vmStats.get(vmName);
 
-            stats.setMemoryKBs(info.maxMem);
-            stats.setTargetMemoryKBs(info.memory);
-            stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+            VmStatsEntry metrics = calculateVmMetrics(dm, oldStats, newStats);
 
-            /* get cpu utilization */
-            VmStats oldStats = null;
+            String vmAsString = vmToString(dm);
+            s_logger.info(String.format("Saving stats for VM [%s].", vmAsString));
+            vmStats.put(vmName, newStats);
+            s_logger.debug(String.format("Saved stats for VM [%s]: [%s].", vmAsString, newStats));

Review Comment:
   Seems good :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1976029411

   @DaanHoogland a [SL] Jenkins job has been kicked to build packages. It will be bundled with  KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1969428154

   > > @DaanHoogland yes, some users have already tested these changes. I can try asking someone to post their results here.
   > 
   > i think this is ready otherwise.
   
   Okay, I asked @gpordeus to test the latest version of this PR. I would like to hear his thoughts on the logs before merging :)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "gpordeus (via GitHub)" <gi...@apache.org>.
gpordeus commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1969631792

   I ran this on my local lab and it seems to be working fine, with proper DEBUG and TRACE split. If there's anything to point out, I'd say that maybe the TRACE memory statistics log at `2024-02-28T18:17:27,575` should be on one line so it'd be easier to `grep`? What do you think, @winterhazel?
   
   
   <details>
   <summary>DEBUG level</summary>
   
   ```
   2024-02-28T18:44:31,537 DEBUG [kvm.resource.LibvirtConnection] (agentRequest-Handler-2:[]) (logid:be7bc94a) Looking for libvirtd connection at: qemu:///system
   2024-02-28T18:44:31,540 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Trying to get VM with name [v-28-VM].
   2024-02-28T18:44:31,549 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-2:[]) (logid:be7bc94a) Using informed label [hdc] for volume [null].
   2024-02-28T18:44:31,550 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Found [3] network interface(s) for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:44:31,557 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-2:[]) (logid:be7bc94a) Using informed label [hdc] for volume [null].
   2024-02-28T18:44:31,557 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Found [2] disk(s) for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:44:31,560 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Ignoring disk [<disk  device='cdrom' type='file'><driver name='qemu' type='raw' /><source file=''/><target dev='hdc' bus='ide'/></disk>] in VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]'s stats since its deviceType is [cdrom].
   2024-02-28T18:44:31,560 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Retrieved statistics for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]: [{"cpuTime":76720000000,"diskReadIOs":6543.0,"diskReadKBs":174041.0,"diskWriteIOs":2571.0,"diskWriteKBs":260983.0,"networkReadKBs":115348.23828125,"networkWriteKBs":847.6865234375}].
   2024-02-28T18:44:31,583 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Calculated metrics for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]: [{"cpuUtilization":0.0,"diskReadIOs":0.0,"diskReadKBs":0.0,"diskWriteIOs":0.0,"diskWriteKBs":0.0,"entityType":"vm","intFreeMemoryKBs":888484.0,"memoryKBs":1048576.0,"networkReadKBs":0.0,"networkWriteKBs":0.0,"numCPUs":1,"targetMemoryKBs":1048576.0}].
   2024-02-28T18:44:31,583 DEBUG [kvm.resource.LibvirtConnection] (agentRequest-Handler-2:[]) (logid:be7bc94a) Looking for libvirtd connection at: qemu:///system
   2024-02-28T18:44:31,585 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Trying to get VM with name [r-30-VM].
   2024-02-28T18:44:31,593 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-2:[]) (logid:be7bc94a) Using informed label [hdc] for volume [null].
   2024-02-28T18:44:31,597 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Found [3] network interface(s) for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:44:31,602 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-2:[]) (logid:be7bc94a) Using informed label [hdc] for volume [null].
   2024-02-28T18:44:31,602 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Found [2] disk(s) for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:44:31,605 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Ignoring disk [<disk  device='cdrom' type='file'><driver name='qemu' type='raw' /><source file=''/><target dev='hdc' bus='ide'/></disk>] in VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]'s stats since its deviceType is [cdrom].
   2024-02-28T18:44:31,605 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Retrieved statistics for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]: [{"cpuTime":53550000000,"diskReadIOs":8789.0,"diskReadKBs":224117.0,"diskWriteIOs":4080.0,"diskWriteKBs":151843.0,"networkReadKBs":114122.0927734375,"networkWriteKBs":751.208984375}].
   2024-02-28T18:44:31,610 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Calculated metrics for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]: [{"cpuUtilization":0.0,"diskReadIOs":0.0,"diskReadKBs":0.0,"diskWriteIOs":0.0,"diskWriteKBs":0.0,"entityType":"vm","intFreeMemoryKBs":124744.0,"memoryKBs":262144.0,"networkReadKBs":0.0,"networkWriteKBs":0.0,"numCPUs":1,"targetMemoryKBs":262144.0}].
   2024-02-28T18:44:31,613 DEBUG [kvm.resource.LibvirtConnection] (agentRequest-Handler-2:[]) (logid:be7bc94a) Looking for libvirtd connection at: qemu:///system
   2024-02-28T18:44:31,613 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Trying to get VM with name [i-2-29-VM].
   2024-02-28T18:44:31,618 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-2:[]) (logid:be7bc94a) Using informed label [hdc] for volume [null].
   2024-02-28T18:44:31,619 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Found [1] network interface(s) for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:44:31,623 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-2:[]) (logid:be7bc94a) Using informed label [hdc] for volume [null].
   2024-02-28T18:44:31,628 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Found [2] disk(s) for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:44:31,630 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Ignoring disk [<disk  device='cdrom' type='file'><driver name='qemu' type='raw' /><source file=''/><target dev='hdc' bus='ide'/></disk>] in VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]'s stats since its deviceType is [cdrom].
   2024-02-28T18:44:31,630 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Retrieved statistics for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]: [{"cpuTime":93850000000,"diskReadIOs":14216.0,"diskReadKBs":15005.0,"diskWriteIOs":42.0,"diskWriteKBs":155.0,"networkReadKBs":6.3369140625,"networkWriteKBs":17.21875}].
   2024-02-28T18:44:31,633 WARN  [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Couldn't retrieve free memory, returning -1.
   2024-02-28T18:44:31,634 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-2:[]) (logid:be7bc94a) Calculated metrics for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]: [{"cpuUtilization":0.0,"diskReadIOs":0.0,"diskReadKBs":0.0,"diskWriteIOs":0.0,"diskWriteKBs":0.0,"entityType":"vm","intFreeMemoryKBs":-1.0,"memoryKBs":524288.0,"networkReadKBs":0.0,"networkWriteKBs":0.0,"numCPUs":1,"targetMemoryKBs":524288.0}].
   ```
   </details>
   
   
   <details>
   <summary>TRACE level</summary>
   
   ```
   2024-02-28T18:17:27,565 DEBUG [kvm.resource.LibvirtConnection] (agentRequest-Handler-1:[]) (logid:f0618de0) Looking for libvirtd connection at: qemu:///system
   2024-02-28T18:17:27,566 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Trying to get VM with name [v-28-VM].
   2024-02-28T18:17:27,566 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting CPU stats for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,566 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting network stats for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,568 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-1:[]) (logid:f0618de0) Using informed label [hdc] for volume [null].
   2024-02-28T18:17:27,569 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Found [3] network interface(s) for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,569 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting disk stats for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,571 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-1:[]) (logid:f0618de0) Using informed label [hdc] for volume [null].
   2024-02-28T18:17:27,571 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Found [2] disk(s) for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,573 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Ignoring disk [<disk  device='cdrom' type='file'><driver name='qemu' type='raw' /><source file=''/><target dev='hdc' bus='ide'/></disk>] in VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]'s stats since its deviceType is [cdrom].
   2024-02-28T18:17:27,573 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Retrieved statistics for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]: [{"cpuTime":63350000000,"diskReadIOs":6542.0,"diskReadKBs":174037.0,"diskWriteIOs":1877.0,"diskWriteKBs":256823.0,"networkReadKBs":115127.3349609375,"networkWriteKBs":727.4599609375}].
   2024-02-28T18:17:27,573 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Writing VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]'s CPU and memory information into the metrics.
   2024-02-28T18:17:27,573 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Trying to get free memory for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,575 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Retrieved memory statistics (information about tags can be found on the libvirt documentation): [{tag:6
   val:524288
   ,tag:0
   val:0
   ,tag:9
   val:1709143611
   ,tag:7
   val:105644
   }].
   2024-02-28T18:17:27,575 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Old stats exist for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]; therefore, the utilization will be calculated.
   2024-02-28T18:17:27,575 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating CPU utilization for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,575 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating network utilization for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,575 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating disk utilization for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}].
   2024-02-28T18:17:27,575 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculated metrics for VM [{"name":"v-28-VM","uuid":"dcfd12f7-3ad9-4baf-bf27-9d13691256d7"}]: [{"cpuUtilization":1.0634762379528082,"diskReadIOs":0.0,"diskReadKBs":0.0,"diskWriteIOs":34.0,"diskWriteKBs":252.0,"entityType":"vm","intFreeMemoryKBs":888484.0,"memoryKBs":1048576.0,"networkReadKBs":8.2197265625,"networkWriteKBs":4.412109375,"numCPUs":1,"targetMemoryKBs":1048576.0}].
   2024-02-28T18:17:27,575 DEBUG [kvm.resource.LibvirtConnection] (agentRequest-Handler-1:[]) (logid:f0618de0) Looking for libvirtd connection at: qemu:///system
   2024-02-28T18:17:27,576 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Trying to get VM with name [r-30-VM].
   2024-02-28T18:17:27,576 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting CPU stats for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,576 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting network stats for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,578 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-1:[]) (logid:f0618de0) Using informed label [hdc] for volume [null].
   2024-02-28T18:17:27,579 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Found [3] network interface(s) for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,579 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting disk stats for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,581 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-1:[]) (logid:f0618de0) Using informed label [hdc] for volume [null].
   2024-02-28T18:17:27,582 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Found [2] disk(s) for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,583 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Ignoring disk [<disk  device='cdrom' type='file'><driver name='qemu' type='raw' /><source file=''/><target dev='hdc' bus='ide'/></disk>] in VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]'s stats since its deviceType is [cdrom].
   2024-02-28T18:17:27,583 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Retrieved statistics for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]: [{"cpuTime":44390000000,"diskReadIOs":8189.0,"diskReadKBs":210813.0,"diskWriteIOs":2290.0,"diskWriteKBs":137775.0,"networkReadKBs":113924.603515625,"networkWriteKBs":617.99609375}].
   2024-02-28T18:17:27,584 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Writing VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]'s CPU and memory information into the metrics.
   2024-02-28T18:17:27,584 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Trying to get free memory for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,586 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Retrieved memory statistics (information about tags can be found on the libvirt documentation): [{tag:6
   2024-02-28T18:17:27,586 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Old stats exist for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]; therefore, the utilization will be calculated.
   2024-02-28T18:17:27,586 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating CPU utilization for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,586 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating network utilization for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,586 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating disk utilization for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}].
   2024-02-28T18:17:27,586 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculated metrics for VM [{"name":"r-30-VM","uuid":"62030daa-26a0-45bd-aa67-5ff289c24f78"}]: [{"cpuUtilization":0.9804736186123806,"diskReadIOs":305.0,"diskReadKBs":9680.0,"diskWriteIOs":86.0,"diskWriteKBs":528.0,"entityType":"vm","intFreeMemoryKBs":124744.0,"memoryKBs":262144.0,"networkReadKBs":7.5615234375,"networkWriteKBs":4.947265625,"numCPUs":1,"targetMemoryKBs":262144.0}].
   2024-02-28T18:17:27,586 DEBUG [kvm.resource.LibvirtConnection] (agentRequest-Handler-1:[]) (logid:f0618de0) Looking for libvirtd connection at: qemu:///system
   2024-02-28T18:17:27,586 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Trying to get VM with name [i-2-29-VM].
   2024-02-28T18:17:27,587 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting CPU stats for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,587 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting network stats for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,589 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-1:[]) (logid:f0618de0) Using informed label [hdc] for volume [null].
   2024-02-28T18:17:27,589 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Found [1] network interface(s) for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,589 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Getting disk stats for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,591 DEBUG [kvm.resource.LibvirtVMDef] (agentRequest-Handler-1:[]) (logid:f0618de0) Using informed label [hdc] for volume [null].
   2024-02-28T18:17:27,591 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Found [2] disk(s) for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,592 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Ignoring disk [<disk  device='cdrom' type='file'><driver name='qemu' type='raw' /><source file=''/><target dev='hdc' bus='ide'/></disk>] in VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]'s stats since its deviceType is [cdrom].
   2024-02-28T18:17:27,592 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Retrieved statistics for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]: [{"cpuTime":36000000000,"diskReadIOs":14216.0,"diskReadKBs":15005.0,"diskWriteIOs":42.0,"diskWriteKBs":155.0,"networkReadKBs":4.1494140625,"networkWriteKBs":6.994140625}].
   2024-02-28T18:17:27,593 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Writing VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]'s CPU and memory information into the metrics.
   2024-02-28T18:17:27,593 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Trying to get free memory for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,594 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Retrieved memory statistics (information about tags can be found on the libvirt documentation): [{tag:6
   2024-02-28T18:17:27,594 WARN  [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Couldn't retrieve free memory, returning -1.
   2024-02-28T18:17:27,595 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Old stats exist for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]; therefore, the utilization will be calculated.
   2024-02-28T18:17:27,595 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating CPU utilization for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,595 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating network utilization for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,595 TRACE [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculating disk utilization for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}].
   2024-02-28T18:17:27,595 DEBUG [kvm.resource.LibvirtComputingResource] (agentRequest-Handler-1:[]) (logid:f0618de0) Calculated metrics for VM [{"name":"i-2-29-VM","uuid":"c87d40b1-644a-4063-bc83-ba357f6f71c2"}]: [{"cpuUtilization":3.590067479972077,"diskReadIOs":0.0,"diskReadKBs":0.0,"diskWriteIOs":0.0,"diskWriteKBs":0.0,"entityType":"vm","intFreeMemoryKBs":-1.0,"memoryKBs":524288.0,"networkReadKBs":0.041015625,"networkWriteKBs":0.41015625,"numCPUs":1,"targetMemoryKBs":524288.0}].
   ```
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1969700920

   @gpordeus thanks for testing. I will adjust the log you pointed out.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2144701068

   @DaanHoogland a [SL] Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2142185625

   Packaging result [SF]: ✔️ el7 ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 9748


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1945706747

   @JoaoJandre can you approve as is?
   @winterhazel did you get some 3rd party testing on this?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1941967102

   @winterhazel a [SL] Jenkins job has been kicked to build packages. It will be bundled with  KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1943415625

   @DaanHoogland a [SL] Trillian-Jenkins test job (centos7 mgmt + kvm-centos7) has been kicked to run smoke tests


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1893509190

   @blueorangutan test alma8 kvm-alma8


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1893390163

   @DaanHoogland a [SL] Jenkins job has been kicked to build packages. It will be bundled with  KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2144698407

   @blueorangutan test


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "JoaoJandre (via GitHub)" <gi...@apache.org>.
JoaoJandre commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2334102022

   Merging based on approvals and manual tests


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1976161414

   Packaging result [SF]: ✔️ el7 ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 8842


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1976384614

   @blueorangutan test


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2141975442

   @winterhazel a [SL] Jenkins job has been kicked to build packages. It will be bundled with  KVM, XenServer and VMware SystemVM templates. I'll keep you posted as I make progress.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "DaanHoogland (via GitHub)" <gi...@apache.org>.
DaanHoogland closed pull request #8511: Add logs to `LibvirtComputingResource`'s metrics collection process
URL: https://github.com/apache/cloudstack/pull/8511


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-2247946649

   Packaging result [SF]: ✔️ el7 ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 10455


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1944958798

   <b>[SF] Trillian test result (tid-9204)</b>
   Environment: kvm-centos7 (x2), Advanced Networking with Mgmt server 7
   Total time taken: 46454 seconds
   Marvin logs: https://github.com/blueorangutan/acs-prs/releases/download/trillian/pr8511-t9204-kvm-centos7.zip
   Smoke tests completed. 129 look OK, 0 have errors, 0 did not run
   Only failed and skipped tests results shown below:
   
   
   Test | Result | Time (s) | Test File
   --- | --- | --- | ---
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "winterhazel (via GitHub)" <gi...@apache.org>.
winterhazel commented on code in PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#discussion_r1504553313


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtExtendedVmStatsEntry.java:
##########
@@ -0,0 +1,51 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.hypervisor.kvm.resource;
+
+import com.cloud.agent.api.VmStatsEntry;
+import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
+
+import java.util.Calendar;
+
+public class LibvirtExtendedVmStatsEntry extends VmStatsEntry {
+    private long cpuTime;
+    private Calendar timestamp;
+
+    public LibvirtExtendedVmStatsEntry() {
+    }
+
+    public long getCpuTime() {
+        return cpuTime;
+    }
+
+    public void setCpuTime(long cpuTime) {
+        this.cpuTime = cpuTime;
+    }
+
+    public Calendar getTimestamp() {
+        return timestamp;
+    }
+
+    public void setTimestamp(Calendar timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    @Override
+    public String toString() {
+        return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "cpuTime", "networkWriteKBs", "networkReadKBs", "diskReadIOs", "diskWriteIOs", "diskReadKBs", "diskWriteKBs");

Review Comment:
   There are 9 omitted fields, so the number of fields being printed here is smaller.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "JoaoJandre (via GitHub)" <gi...@apache.org>.
JoaoJandre commented on code in PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#discussion_r1471756588


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java:
##########
@@ -4401,127 +4403,193 @@ protected String getDiskPathFromDiskDef(DiskDef disk) {
         return null;
     }
 
-    private class VmStats {
-        long usedTime;
-        long tx;
-        long rx;
-        long ioRead;
-        long ioWrote;
-        long bytesRead;
-        long bytesWrote;
-        Calendar timestamp;
+    private String vmToString(Domain dm) throws LibvirtException {
+        return String.format("{\"name\":\"%s\",\"uuid\":\"%s\"}", dm.getName(), dm.getUUIDString());
     }
 
+    /**
+     * Returns metrics for the period since this function was last called for the specified VM.
+     * @param conn the Libvirt connection.
+     * @param vmName name of the VM.
+     * @return metrics for the period since last time this function was called for the VM.
+     * @throws LibvirtException
+     */
     public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
         Domain dm = null;
         try {
+            s_logger.debug(String.format("Trying to get VM with name [%s].", vmName));
             dm = getDomain(conn, vmName);
             if (dm == null) {
+                s_logger.warn(String.format("Could not get VM with name [%s].", vmName));
                 return null;
             }
-            DomainInfo info = dm.getInfo();
-            final VmStatsEntry stats = new VmStatsEntry();
 
-            stats.setNumCPUs(info.nrVirtCpu);
-            stats.setEntityType("vm");
+            LibvirtExtendedVmStatsEntry newStats = getVmCurrentStats(dm);
+            LibvirtExtendedVmStatsEntry oldStats = vmStats.get(vmName);
 
-            stats.setMemoryKBs(info.maxMem);
-            stats.setTargetMemoryKBs(info.memory);
-            stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+            VmStatsEntry metrics = calculateVmMetrics(dm, oldStats, newStats);
 
-            /* get cpu utilization */
-            VmStats oldStats = null;
+            String vmAsString = vmToString(dm);
+            s_logger.info(String.format("Saving stats for VM [%s].", vmAsString));
+            vmStats.put(vmName, newStats);
+            s_logger.debug(String.format("Saved stats for VM [%s]: [%s].", vmAsString, newStats));

Review Comment:
   Can't we merge these two logs into one? I think we could just remove the info one.



##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java:
##########
@@ -4401,127 +4403,193 @@ protected String getDiskPathFromDiskDef(DiskDef disk) {
         return null;
     }
 
-    private class VmStats {
-        long usedTime;
-        long tx;
-        long rx;
-        long ioRead;
-        long ioWrote;
-        long bytesRead;
-        long bytesWrote;
-        Calendar timestamp;
+    private String vmToString(Domain dm) throws LibvirtException {
+        return String.format("{\"name\":\"%s\",\"uuid\":\"%s\"}", dm.getName(), dm.getUUIDString());
     }
 
+    /**
+     * Returns metrics for the period since this function was last called for the specified VM.
+     * @param conn the Libvirt connection.
+     * @param vmName name of the VM.
+     * @return metrics for the period since last time this function was called for the VM.
+     * @throws LibvirtException
+     */
     public VmStatsEntry getVmStat(final Connect conn, final String vmName) throws LibvirtException {
         Domain dm = null;
         try {
+            s_logger.debug(String.format("Trying to get VM with name [%s].", vmName));
             dm = getDomain(conn, vmName);
             if (dm == null) {
+                s_logger.warn(String.format("Could not get VM with name [%s].", vmName));
                 return null;
             }
-            DomainInfo info = dm.getInfo();
-            final VmStatsEntry stats = new VmStatsEntry();
 
-            stats.setNumCPUs(info.nrVirtCpu);
-            stats.setEntityType("vm");
+            LibvirtExtendedVmStatsEntry newStats = getVmCurrentStats(dm);
+            LibvirtExtendedVmStatsEntry oldStats = vmStats.get(vmName);
 
-            stats.setMemoryKBs(info.maxMem);
-            stats.setTargetMemoryKBs(info.memory);
-            stats.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+            VmStatsEntry metrics = calculateVmMetrics(dm, oldStats, newStats);
 
-            /* get cpu utilization */
-            VmStats oldStats = null;
+            String vmAsString = vmToString(dm);
+            s_logger.info(String.format("Saving stats for VM [%s].", vmAsString));
+            vmStats.put(vmName, newStats);
+            s_logger.debug(String.format("Saved stats for VM [%s]: [%s].", vmAsString, newStats));
 
-            final Calendar now = Calendar.getInstance();
+            return metrics;
+        } finally {
+            if (dm != null) {
+                dm.free();
+            }
+        }
+    }
 
-            oldStats = vmStats.get(vmName);
+    /**
+     * Returns a VM's current statistics.
+     * @param dm domain of the VM.
+     * @return current statistics of the VM.
+     * @throws LibvirtException
+     */
+    protected LibvirtExtendedVmStatsEntry getVmCurrentStats(final Domain dm) throws LibvirtException {
+        final LibvirtExtendedVmStatsEntry stats = new LibvirtExtendedVmStatsEntry();
 
-            long elapsedTime = 0;
-            if (oldStats != null) {
-                elapsedTime = now.getTimeInMillis() - oldStats.timestamp.getTimeInMillis();
-                double utilization = (info.cpuTime - oldStats.usedTime) / ((double)elapsedTime * 1000000);
+        getVmCurrentCpuStats(dm, stats);
+        getVmCurrentNetworkStats(dm, stats);
+        getVmCurrentDiskStats(dm, stats);
 
-                utilization = utilization / info.nrVirtCpu;
-                if (utilization > 0) {
-                    stats.setCPUUtilization(utilization * 100);
-                }
-            }
+        s_logger.debug(String.format("Retrieved statistics for VM [%s]: [%s].", vmToString(dm), stats));
+        stats.setTimestamp(Calendar.getInstance());
+        return stats;
+    }
 
-            /* get network stats */
+    /**
+     * Passes a VM's current CPU statistics into the provided LibvirtExtendedVmStatsEntry.
+     * @param dm domain of the VM.
+     * @param stats LibvirtExtendedVmStatsEntry that will receive the current CPU statistics.
+     * @throws LibvirtException
+     */
+    protected void getVmCurrentCpuStats(final Domain dm, final LibvirtExtendedVmStatsEntry stats) throws LibvirtException {
+        s_logger.debug(String.format("Getting CPU stats for VM [%s].", vmToString(dm)));
+        stats.setCpuTime(dm.getInfo().cpuTime);
+    }
 
-            final List<InterfaceDef> vifs = getInterfaces(conn, vmName);
-            long rx = 0;
-            long tx = 0;
-            for (final InterfaceDef vif : vifs) {
-                final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
-                rx += ifStats.rx_bytes;
-                tx += ifStats.tx_bytes;
-            }
+    /**
+     * Passes a VM's current network statistics into the provided LibvirtExtendedVmStatsEntry.
+     * @param dm domain of the VM.
+     * @param stats LibvirtExtendedVmStatsEntry that will receive the current network statistics.
+     * @throws LibvirtException
+     */
+    protected void getVmCurrentNetworkStats(final Domain dm, final LibvirtExtendedVmStatsEntry stats) throws LibvirtException {
+        final String vmAsString = vmToString(dm);
+        s_logger.debug(String.format("Getting network stats for VM [%s].", vmAsString));
+        final List<InterfaceDef> vifs = getInterfaces(dm.getConnect(), dm.getName());
+        s_logger.debug(String.format("Found [%d] network interface(s) for VM [%s].", vifs.size(), vmAsString));
+        double rx = 0;
+        double tx = 0;
+        for (final InterfaceDef vif : vifs) {
+            final DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
+            rx += ifStats.rx_bytes;
+            tx += ifStats.tx_bytes;
+        }
+        stats.setNetworkReadKBs(rx / 1024);
+        stats.setNetworkWriteKBs(tx / 1024);
+    }
 
-            if (oldStats != null) {
-                final double deltarx = rx - oldStats.rx;
-                if (deltarx > 0) {
-                    stats.setNetworkReadKBs(deltarx / 1024);
-                }
-                final double deltatx = tx - oldStats.tx;
-                if (deltatx > 0) {
-                    stats.setNetworkWriteKBs(deltatx / 1024);
-                }
+    /**
+     * Passes a VM's current disk statistics into the provided LibvirtExtendedVmStatsEntry.
+     * @param dm domain of the VM.
+     * @param stats LibvirtExtendedVmStatsEntry that will receive the current disk statistics.
+     * @throws LibvirtException
+     */
+    protected void getVmCurrentDiskStats(final Domain dm, final LibvirtExtendedVmStatsEntry stats) throws LibvirtException {
+        final String vmAsString = vmToString(dm);
+        s_logger.debug(String.format("Getting disk stats for VM [%s].", vmAsString));
+        final List<DiskDef> disks = getDisks(dm.getConnect(), dm.getName());
+        s_logger.debug(String.format("Found [%d] disk(s) for VM [%s].", disks.size(), vmAsString));
+        long io_rd = 0;
+        long io_wr = 0;
+        double bytes_rd = 0;
+        double bytes_wr = 0;
+        for (final DiskDef disk : disks) {
+            if (disk.getDeviceType() == DeviceType.CDROM || disk.getDeviceType() == DeviceType.FLOPPY) {
+                s_logger.debug(String.format("Ignoring disk [%s] in VM [%s]'s stats since its deviceType is [%s].", disk.toString().replace("\n", ""), vmAsString, disk.getDeviceType()));
+                continue;
             }
+            final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
+            io_rd += blockStats.rd_req;
+            io_wr += blockStats.wr_req;
+            bytes_rd += blockStats.rd_bytes;
+            bytes_wr += blockStats.wr_bytes;
+        }
+        stats.setDiskReadIOs(io_rd);
+        stats.setDiskWriteIOs(io_wr);
+        stats.setDiskReadKBs(bytes_rd / 1024);
+        stats.setDiskWriteKBs(bytes_wr / 1024);
+    }
 
-            /* get disk stats */
-            final List<DiskDef> disks = getDisks(conn, vmName);
-            long io_rd = 0;
-            long io_wr = 0;
-            long bytes_rd = 0;
-            long bytes_wr = 0;
-            for (final DiskDef disk : disks) {
-                if (disk.getDeviceType() == DeviceType.CDROM || disk.getDeviceType() == DeviceType.FLOPPY) {
-                    continue;
-                }
-                final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
-                io_rd += blockStats.rd_req;
-                io_wr += blockStats.wr_req;
-                bytes_rd += blockStats.rd_bytes;
-                bytes_wr += blockStats.wr_bytes;
+    /**
+     * Calculates a VM's metrics for the period between the two statistics given as parameters.
+     * @param dm domain of the VM.
+     * @param oldStats old statistics. If null, the CPU, network and disk utilization won't be calculated.
+     * @param newStats new statistics.
+     * @return metrics for the period between the two statistics.
+     * @throws LibvirtException
+     */
+    protected VmStatsEntry calculateVmMetrics(final Domain dm, final LibvirtExtendedVmStatsEntry oldStats, final LibvirtExtendedVmStatsEntry newStats) throws LibvirtException {
+        final VmStatsEntry metrics = new VmStatsEntry();
+        final DomainInfo info = dm.getInfo();
+        final String vmAsString = vmToString(dm);
+
+        metrics.setEntityType("vm");
+        s_logger.debug(String.format("Writing VM [%s]'s CPU and memory information into the metrics.", vmAsString));
+        metrics.setNumCPUs(info.nrVirtCpu);
+        metrics.setMemoryKBs(info.maxMem);
+        metrics.setTargetMemoryKBs(info.memory);
+        s_logger.debug(String.format("Trying to get free memory for VM [%s].", vmAsString));
+        metrics.setIntFreeMemoryKBs(getMemoryFreeInKBs(dm));
+
+        if (oldStats != null) {
+            s_logger.debug(String.format("Old stats exist for VM [%s]; thefore, the utilization will be calculated.", vmAsString));

Review Comment:
   ```suggestion
               s_logger.debug(String.format("Old stats exist for VM [%s]; therefore, the utilization will be calculated.", vmAsString));
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collection process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1942085085

   Packaging result [SF]: ✔️ el7 ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 8645


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add logs to `LibvirtComputingResource`'s metrics collecting process [cloudstack]

Posted by "blueorangutan (via GitHub)" <gi...@apache.org>.
blueorangutan commented on PR #8511:
URL: https://github.com/apache/cloudstack/pull/8511#issuecomment-1893507095

   Packaging result [SF]: ✔️ el7 ✔️ el8 ✔️ el9 ✔️ debian ✔️ suse15. SL-JID 8332


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org