From 063c39746b2f1c498b65c0752cb940dcf9bd7697 Mon Sep 17 00:00:00 2001 From: Ian Lumsden Date: Tue, 15 Oct 2024 16:43:43 -0400 Subject: [PATCH] Adds clang-format configuration and scripts for formatting (#605) This commit adds the following: * A .clang-format file for configuring formatting * A script for autoformatting with clang-format * A Dockerfile for running the autoformatting script with clang-format 17.0.6 * A script for building and running the autoformatting Dockerfile * A script for checking formatting with clang-format * A Dockerfile for running the format check script with clang-format 17.0.6 * A script for building and running the format check Dockerfile * A GitHub Actions CI file for checking formatting * The results of reformatting the repo --- .clang-format | 220 ++ .github/workflows/format-check.yml | 25 + examples/apps/c-example.c | 6 +- examples/apps/cali-functional.cpp | 17 +- examples/apps/cali-memtracking-macros.cpp | 67 +- examples/apps/cali-memtracking.cpp | 64 +- .../apps/cali-perfproblem-branch-mispred.cpp | 19 +- examples/apps/cali-print-snapshot.c | 19 +- examples/apps/cali-regionprofile.cpp | 52 +- examples/apps/collective-output-channel.cpp | 8 +- examples/apps/cxx-example.cpp | 13 +- include/caliper/Annotation.h | 81 +- include/caliper/AnnotationBinding.h | 59 +- include/caliper/CaliFunctional.h | 394 +-- include/caliper/Caliper.h | 248 +- include/caliper/CaliperService.h | 2 +- include/caliper/ChannelController.h | 10 +- include/caliper/CollectiveOutputChannel.h | 6 +- include/caliper/ConfigManager.h | 58 +- include/caliper/RegionProfile.h | 65 +- include/caliper/SnapshotRecord.h | 99 +- include/caliper/cali-manager.h | 5 +- include/caliper/cali-mpi.h | 28 +- include/caliper/cali.h | 230 +- include/caliper/cali_datatracker.h | 30 +- include/caliper/cali_definitions.h | 15 +- include/caliper/cali_macros.h | 64 +- include/caliper/common/Attribute.h | 87 +- .../common/CaliperMetadataAccessInterface.h | 41 +- include/caliper/common/Entry.h | 68 +- include/caliper/common/Log.h | 7 +- include/caliper/common/Node.h | 20 +- include/caliper/common/OutputStream.h | 38 +- include/caliper/common/RuntimeConfig.h | 31 +- include/caliper/common/StringConverter.h | 35 +- include/caliper/common/Variant.h | 136 +- include/caliper/common/cali_types.h | 113 +- include/caliper/common/cali_variant.h | 112 +- include/caliper/common/callback.hpp | 28 +- include/caliper/common/lockfree-tree.hpp | 37 +- include/caliper/reader/Aggregator.h | 10 +- include/caliper/reader/CalQLParser.h | 20 +- include/caliper/reader/CaliReader.h | 2 +- include/caliper/reader/CaliWriter.h | 17 +- include/caliper/reader/CaliperMetadataDB.h | 80 +- .../reader/FlatExclusiveRegionProfile.h | 21 +- .../reader/FlatInclusiveRegionProfile.h | 20 +- include/caliper/reader/FormatProcessor.h | 6 +- .../reader/NestedExclusiveRegionProfile.h | 20 +- .../reader/NestedInclusiveRegionProfile.h | 20 +- include/caliper/reader/Preprocessor.h | 6 +- include/caliper/reader/QueryProcessor.h | 8 +- include/caliper/reader/QuerySpec.h | 75 +- include/caliper/reader/RecordProcessor.h | 18 +- include/caliper/reader/RecordSelector.h | 8 +- include/caliper/tools-util/Args.h | 13 +- scripts/formatting/Dockerfile.check | 32 + scripts/formatting/Dockerfile.format | 32 + scripts/formatting/autoformat-docker.sh | 24 + scripts/formatting/autoformat.sh | 59 + scripts/formatting/check-formatting-docker.sh | 24 + scripts/formatting/check-formatting.sh | 59 + src/caliper/Annotation.cpp | 130 +- src/caliper/AnnotationBinding.cpp | 82 +- src/caliper/Blackboard.cpp | 73 +- src/caliper/Blackboard.h | 62 +- src/caliper/Caliper.cpp | 697 +++--- src/caliper/ChannelController.cpp | 69 +- src/caliper/CollectiveOutputChannel.cpp | 78 +- src/caliper/ConfigManager.cpp | 605 +++-- src/caliper/CustomOutputController.cpp | 14 +- src/caliper/CustomOutputController.h | 3 +- src/caliper/MemoryPool.cpp | 91 +- src/caliper/MemoryPool.h | 10 +- src/caliper/MetadataTree.cpp | 127 +- src/caliper/MetadataTree.h | 64 +- src/caliper/MpiChannelManager.cpp | 33 +- src/caliper/MpiEvents.h | 11 +- src/caliper/OutputCommMpi.cpp | 16 +- src/caliper/OutputCommMpi.h | 4 +- src/caliper/RegionFilter.cpp | 53 +- src/caliper/RegionFilter.h | 21 +- src/caliper/RegionProfile.cpp | 59 +- src/caliper/aggregate_over_mpi.cpp | 104 +- src/caliper/api.cpp | 85 +- src/caliper/builtin_configmanager.cpp | 41 +- src/caliper/cali.cpp | 413 ++-- src/caliper/cali_datatracker.cpp | 21 +- src/caliper/collective_flush.cpp | 41 +- src/caliper/config_sanity_check.cpp | 82 +- .../CudaActivityProfileController.cpp | 141 +- .../CudaActivityReportController.cpp | 143 +- .../HatchetRegionProfileController.cpp | 136 +- .../HatchetSampleProfileController.cpp | 159 +- .../controllers/LoopReportController.cpp | 137 +- .../controllers/OpenMPReportController.cpp | 177 +- .../ROCmActivityProfileController.cpp | 141 +- .../ROCmActivityReportController.cpp | 143 +- .../controllers/RuntimeReportController.cpp | 143 +- .../controllers/SampleReportController.cpp | 158 +- src/caliper/controllers/SpotController.cpp | 280 ++- src/caliper/controllers/controllers.cpp | 48 +- src/caliper/controllers/util.h | 2 +- src/caliper/machine.h | 4 +- src/caliper/machine_mpi.cpp | 15 +- src/caliper/machine_serial.cpp | 8 +- src/caliper/mpi_flush.cpp | 10 +- src/caliper/setup_mpi.cpp | 16 +- src/caliper/test/MockupMetadataDB.h | 76 +- src/caliper/test/test_attribute.cpp | 46 +- src/caliper/test/test_blackboard.cpp | 50 +- src/caliper/test/test_c_api.cpp | 3 +- src/caliper/test/test_c_snapshot.cpp | 251 +- src/caliper/test/test_c_wrapper.cpp | 3 +- src/caliper/test/test_channel_api.cpp | 65 +- src/caliper/test/test_channel_controller.cpp | 34 +- src/caliper/test/test_configmanager.cpp | 126 +- src/caliper/test/test_metadatatree.cpp | 40 +- .../test/test_postprocess_snapshot.cpp | 23 +- src/caliper/test/test_regionfilter.cpp | 77 +- src/common/Attribute.cpp | 24 +- src/common/CaliperMetadataAccessInterface.cpp | 12 +- src/common/CompressedSnapshotRecord.cpp | 137 +- src/common/CompressedSnapshotRecord.h | 127 +- src/common/Entry.cpp | 23 +- src/common/Log.cpp | 70 +- src/common/NodeBuffer.cpp | 175 +- src/common/NodeBuffer.h | 33 +- src/common/OutputStream.cpp | 73 +- src/common/RuntimeConfig.cpp | 231 +- src/common/SnapshotBuffer.cpp | 42 +- src/common/SnapshotBuffer.h | 31 +- src/common/SnapshotTextFormatter.cpp | 71 +- src/common/SnapshotTextFormatter.h | 8 +- src/common/StringConverter.cpp | 59 +- src/common/Variant.cpp | 84 +- src/common/cali_types.c | 138 +- src/common/cali_variant.c | 177 +- src/common/test/MockupMetadataDB.h | 79 +- src/common/test/test_c_variant.cpp | 94 +- .../test/test_compressedsnapshotrecord.cpp | 193 +- src/common/test/test_runtimeconfig.cpp | 65 +- src/common/test/test_snapshotbuffer.cpp | 14 +- .../test/test_snapshottextformatter.cpp | 11 +- src/common/test/test_stringconverter.cpp | 70 +- src/common/test/test_variant.cpp | 187 +- src/common/util/demangle.cpp | 2 +- src/common/util/demangle.h | 2 +- src/common/util/file_util.cpp | 11 +- src/common/util/format_util.cpp | 29 +- src/common/util/format_util.h | 34 +- src/common/util/parse_util.cpp | 15 +- src/common/util/parse_util.h | 23 +- src/common/util/spinlock.hpp | 17 +- src/common/util/split.hpp | 13 +- src/common/util/unitfmt.c | 19 +- src/common/util/unitfmt.h | 15 +- src/common/util/vlenc.c | 6 +- src/common/util/vlenc.h | 19 +- .../c_fortran/BufferedRegionProfile.cpp | 25 +- .../c_fortran/BufferedRegionProfile.h | 14 +- src/interface/c_fortran/typesCaliper.h | 101 +- src/interface/c_fortran/utilCaliper.cpp | 105 +- src/interface/c_fortran/wrapAnnotation.cpp | 55 +- src/interface/c_fortran/wrapAnnotation.h | 25 +- .../c_fortran/wrapBufferedRegionProfile.cpp | 66 +- .../c_fortran/wrapBufferedRegionProfile.h | 34 +- src/interface/c_fortran/wrapCaliper.h | 48 +- src/interface/c_fortran/wrapConfigManager.cpp | 94 +- src/interface/c_fortran/wrapConfigManager.h | 40 +- .../c_fortran/wrapScopeAnnotation.cpp | 19 +- src/interface/c_fortran/wrapScopeAnnotation.h | 10 +- src/interface/python/annotation.cpp | 198 +- src/interface/python/annotation.h | 31 +- src/interface/python/config_manager.cpp | 264 +- src/interface/python/config_manager.h | 51 +- src/interface/python/instrumentation.cpp | 419 ++-- src/interface/python/instrumentation.h | 44 +- src/interface/python/loop.cpp | 46 +- src/interface/python/loop.h | 20 +- src/interface/python/mod.cpp | 130 +- src/reader/Aggregator.cpp | 982 ++++---- src/reader/CalQLParser.cpp | 197 +- src/reader/CaliReader.cpp | 96 +- src/reader/CaliWriter.cpp | 52 +- src/reader/CaliperMetadataDB.cpp | 388 ++- src/reader/Expand.cpp | 57 +- src/reader/Expand.h | 4 +- src/reader/FlatExclusiveRegionProfile.cpp | 50 +- src/reader/FlatInclusiveRegionProfile.cpp | 47 +- src/reader/FormatProcessor.cpp | 67 +- src/reader/Formatter.h | 7 +- src/reader/JsonFormatter.cpp | 111 +- src/reader/JsonSplitFormatter.cpp | 155 +- src/reader/NestedExclusiveRegionProfile.cpp | 56 +- src/reader/NestedInclusiveRegionProfile.cpp | 63 +- src/reader/Preprocessor.cpp | 257 +- src/reader/QueryProcessor.cpp | 38 +- src/reader/RecordSelector.cpp | 173 +- src/reader/SnapshotTableFormatter.cpp | 29 +- src/reader/SnapshotTableFormatter.h | 3 +- src/reader/SnapshotTree.cpp | 105 +- src/reader/SnapshotTree.h | 44 +- src/reader/TableFormatter.cpp | 138 +- src/reader/TableFormatter.h | 2 +- src/reader/TreeFormatter.cpp | 160 +- src/reader/TreeFormatter.h | 5 +- src/reader/UserFormatter.cpp | 61 +- src/reader/UserFormatter.h | 2 +- src/reader/test/test_aggregator.cpp | 616 +++-- src/reader/test/test_calireader.cpp | 18 +- src/reader/test/test_calqlparser.cpp | 89 +- src/reader/test/test_filter.cpp | 311 ++- .../test/test_flatexclusiveregionprofile.cpp | 87 +- .../test/test_flatinclusiveregionprofile.cpp | 89 +- src/reader/test/test_metadb.cpp | 79 +- .../test_nestedexclusiveregionprofile.cpp | 95 +- .../test_nestedinclusiveregionprofile.cpp | 95 +- src/reader/test/test_nodebuffer.cpp | 90 +- src/reader/test/test_preprocessor.cpp | 159 +- .../test/test_snapshottableformatter.cpp | 11 +- src/services/LoopStatistics.cpp | 55 +- src/services/Services.cpp | 44 +- src/services/Services.h | 6 +- src/services/adiak/AdiakExport.cpp | 79 +- src/services/adiak/AdiakImport.cpp | 233 +- src/services/adiak/test/test_adiak.cpp | 44 +- src/services/aggregate/Aggregate.cpp | 264 +- src/services/aggregate/AggregationDB.cpp | 162 +- src/services/aggregate/AggregationDB.h | 20 +- src/services/alloc/AllocService.cpp | 445 ++-- src/services/alloc/SplayTree.hpp | 129 +- src/services/callpath/Callpath.cpp | 136 +- src/services/cpuinfo/CpuInfo.cpp | 23 +- src/services/craypat/CrayPAT.cpp | 37 +- src/services/cupti/Cupti.cpp | 315 +-- src/services/cupti/CuptiEventSampling.cpp | 165 +- src/services/cupti/CuptiEventSampling.h | 25 +- src/services/cupti/CuptiTrace.cpp | 1021 ++++---- src/services/debug/Debug.cpp | 10 +- src/services/env/EnvironmentInfo.cpp | 77 +- src/services/event/EventTrigger.cpp | 242 +- .../io/CurIOus/include/curious/curious.h | 231 +- src/services/io/CurIOus/src/callbacks.c | 129 +- src/services/io/CurIOus/src/callbacks.h | 47 +- src/services/io/CurIOus/src/curious.c | 64 +- src/services/io/CurIOus/src/dynamic_array.c | 221 +- src/services/io/CurIOus/src/dynamic_array.h | 46 +- src/services/io/CurIOus/src/file_registry.c | 214 +- src/services/io/CurIOus/src/file_registry.h | 36 +- src/services/io/CurIOus/src/mount_tree.c | 273 ++- src/services/io/CurIOus/src/mount_tree.h | 5 +- src/services/io/CurIOus/src/wrappers.c | 2164 +++++++++-------- src/services/io/CurIOus/src/wrappers.h | 47 +- src/services/io/IOService.cpp | 258 +- src/services/kokkos/KokkosLookup.cpp | 147 +- .../kokkos/KokkosProfilingSymbols.cpp | 185 +- .../kokkos/KokkosProfilingSymbols.hpp | 3 - src/services/kokkos/KokkosTime.cpp | 141 +- src/services/kokkos/types.hpp | 81 +- src/services/ldms/LdmsForwarder.cpp | 308 +-- src/services/libpfm/Libpfm.cpp | 567 ++--- src/services/libpfm/perf_postprocessing.cpp | 107 +- src/services/libpfm/perf_util.c | 1341 +++++----- src/services/libpfm/perf_util.h | 229 +- src/services/memusage/MemStatService.cpp | 78 +- src/services/monitor/LoopMonitor.cpp | 84 +- src/services/monitor/RegionMonitor.cpp | 66 +- src/services/monitor/ThreadMonitor.cpp | 100 +- src/services/monitor/Timeseries.cpp | 90 +- src/services/mpireport/MpiReport.cpp | 66 +- src/services/mpit/MpitServiceMPI.cpp | 594 ++--- src/services/mpiwrap/MpiPattern.cpp | 289 ++- src/services/mpiwrap/MpiPattern.h | 59 +- src/services/mpiwrap/MpiTracing.cpp | 316 +-- src/services/mpiwrap/MpiTracing.h | 52 +- src/services/mpiwrap/MpiWrap.cpp | 30 +- src/services/nvtx/Nvtx.cpp | 95 +- src/services/ompt/OmptService.cpp | 180 +- src/services/papi/Papi.cpp | 245 +- src/services/pcp/Pcp.cpp | 177 +- src/services/pcp/PcpMemory.cpp | 106 +- src/services/pthread/PthreadService.cpp | 60 +- src/services/recorder/Recorder.cpp | 9 +- src/services/report/Report.cpp | 44 +- src/services/rocprofiler/RocProfiler.cpp | 506 ++-- src/services/rocprofiler/RocTX.cpp | 37 +- src/services/roctracer/RocTracer.cpp | 440 ++-- src/services/roctx/RocTX.cpp | 37 +- src/services/sampler/Sampler.cpp | 94 +- src/services/sampler/context_ppc.h | 4 +- src/services/statistics/Statistics.cpp | 105 +- src/services/symbollookup/Lookup.h | 11 +- src/services/symbollookup/LookupLibdw.cpp | 49 +- src/services/symbollookup/SymbolLookup.cpp | 148 +- src/services/sysalloc/SysAllocService.cpp | 69 +- src/services/tau/tau.cpp | 31 +- src/services/templates/MeasurementService.cpp | 114 +- src/services/textlog/TextLog.cpp | 100 +- src/services/timer/Timer.cpp | 167 +- src/services/topdown/IntelTopdown.cpp | 286 +-- src/services/trace/Trace.cpp | 258 +- src/services/trace/TraceBufferChunk.cpp | 9 +- src/services/trace/TraceBufferChunk.h | 45 +- src/services/umpire/UmpireStatistics.cpp | 224 +- src/services/util/ChannelList.hpp | 11 +- src/services/validator/validator.cpp | 162 +- src/services/variorum/Variorum.cpp | 161 +- src/services/vtune/VTuneBindings.cpp | 24 +- src/tools/cali-query/AttributeExtract.cpp | 23 +- src/tools/cali-query/AttributeExtract.h | 4 +- src/tools/cali-query/cali-query.cpp | 276 +-- src/tools/cali-query/query_common.cpp | 89 +- src/tools/cali-query/query_common.h | 25 +- src/tools/cali-stat/cali-stat.cpp | 398 ++- src/tools/mpi-caliquery/mpi-caliquery.cpp | 139 +- src/tools/util/Args.cpp | 107 +- test/cali-annotation-perftest.cpp | 106 +- test/cali-flush-perftest.cpp | 57 +- test/cali-functional-debugging.cpp | 47 +- test/cali-test.cpp | 130 +- test/cali-wrap.cpp | 42 +- test/ci_app_tests/ci_test_aggregate.cpp | 19 +- test/ci_app_tests/ci_test_alloc.c | 114 +- test/ci_app_tests/ci_test_alloc_hooks.c | 154 +- test/ci_app_tests/ci_test_basic.cpp | 9 +- test/ci_app_tests/ci_test_binding.cpp | 37 +- test/ci_app_tests/ci_test_c_ann.c | 96 +- test/ci_app_tests/ci_test_c_snapshot.c | 46 +- test/ci_app_tests/ci_test_cali_before_mpi.cpp | 8 +- test/ci_app_tests/ci_test_channel_api.c | 12 +- .../ci_test_collective_output_channel.cpp | 2 +- test/ci_app_tests/ci_test_mpi_before_cali.cpp | 2 +- .../ci_test_mpi_channel_manager.cpp | 2 +- test/ci_app_tests/ci_test_nesting.cpp | 52 +- test/ci_app_tests/ci_test_openmp.cpp | 2 +- test/ci_app_tests/ci_test_thread.cpp | 13 +- 337 files changed, 17798 insertions(+), 18756 deletions(-) create mode 100644 .clang-format create mode 100644 .github/workflows/format-check.yml create mode 100644 scripts/formatting/Dockerfile.check create mode 100644 scripts/formatting/Dockerfile.format create mode 100755 scripts/formatting/autoformat-docker.sh create mode 100755 scripts/formatting/autoformat.sh create mode 100755 scripts/formatting/check-formatting-docker.sh create mode 100755 scripts/formatting/check-formatting.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..4ecc174d9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,220 @@ +--- +BasedOnStyle: Microsoft +AccessModifierOffset: -4 +AlignAfterOpenBracket: BlockIndent +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Inline +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +AttributeMacros: + - __capability +BinPackArguments: false +BinPackParameters: false +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: Never + AfterEnum: false + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: false + AfterUnion: false + AfterExternBlock: true + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +BreakAfterAttributes: Never +BreakAfterJavaFieldAnnotations: false +BreakArrays: false +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Custom +BreakBeforeConceptDeclarations: Always +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 120 +CommentPragmas: ^\\.+ +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Always +EmptyLineBeforeAccessModifier: Always +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: ^"(llvm|llvm-c|clang|clang-c)/ + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: ^(<|"(gtest|gmock|isl|json)/) + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: .* + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: (Test)?$ +IncludeIsMainSourceRegex: "" +IndentAccessModifiers: false +IndentCaseBlocks: true +IndentCaseLabels: false +IndentExternBlock: NoIndent +IndentGotoLabels: false +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + Decimal: 0 + Hex: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +Language: Cpp +LineEnding: DeriveLF +MacroBlockBegin: "" +MacroBlockEnd: "" +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PPIndentWidth: -1 +PackConstructorInitializers: NextLine +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 1000 +PointerAlignment: Left +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: false +RemoveBracesLLVM: false +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Always +ShortNamespaceLines: 1 +SortIncludes: Never +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: true +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: Custom +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDeclarationName: false + AfterFunctionDefinitionName: false + AfterIfMacros: true + AfterOverloadedOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: c++11 +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Never +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE diff --git a/.github/workflows/format-check.yml b/.github/workflows/format-check.yml new file mode 100644 index 000000000..49e2d4d0c --- /dev/null +++ b/.github/workflows/format-check.yml @@ -0,0 +1,25 @@ +name: Check code formatting + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + cpp-format-check: + runs-on: ubuntu-24.04 + + steps: + - uses: actions/checkout@v4 + + - name: Install clang-format + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + build-essential \ + clang-format-17 + + - name: Test formatting + run: | + ./scripts/formatting/check-formatting.sh /usr/bin/clang-format-17 \ No newline at end of file diff --git a/examples/apps/c-example.c b/examples/apps/c-example.c index f67b68923..955ee7566 100644 --- a/examples/apps/c-example.c +++ b/examples/apps/c-example.c @@ -15,7 +15,7 @@ void print_help() { - const char* helpstr = + const char* helpstr = "Usage: c-example [caliper-config(arg=...,),...]." "\nRuns \"runtime-report\" configuration by default." "\nUse \"none\" to run without a ConfigManager configuration." @@ -81,8 +81,8 @@ int main(int argc, char* argv[]) // Mark a code region. Opens region "annotation=init" in Caliper. CALI_MARK_BEGIN("init"); - int count = 4; - double t = 0; + int count = 4; + double t = 0; CALI_MARK_END("init"); // Mark a loop. Opens region "loop=mainloop" in Caliper. diff --git a/examples/apps/cali-functional.cpp b/examples/apps/cali-functional.cpp index 989708593..fd802c94b 100644 --- a/examples/apps/cali-functional.cpp +++ b/examples/apps/cali-functional.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2019, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. -// A minimal Caliper function object demo +// A minimal Caliper function object demo #include #include @@ -24,8 +24,9 @@ int sum(int x, int y){ // To instrument, instead first rename your function to "wrapped" -int wrapped_sum(int x, int y){ - return x + y; +int wrapped_sum(int x, int y) +{ + return x + y; } // Then make a function object with the original name @@ -35,17 +36,17 @@ int wrapped_sum(int x, int y){ // // In this case we want to name the function "sum" and // create it by wrapping "wrapped_sum" -auto sum = cali::wrap_function_and_args("sum",wrapped_sum); +auto sum = cali::wrap_function_and_args("sum", wrapped_sum); // You can wrap anything you can get a handle to, see: -auto wrapped_malloc = cali::wrap_function_and_args("malloc",malloc); +auto wrapped_malloc = cali::wrap_function_and_args("malloc", malloc); // Note that you don't have to profile arguments and return values: -auto minimally_wrapped_free = cali::wrap_function("free",free); +auto minimally_wrapped_free = cali::wrap_function("free", free); int main(int argc, char* argv[]) { - int seven = sum(3,4); - int* int_pointer = (int*)wrapped_malloc(sizeof(int)* 100); + int seven = sum(3, 4); + int* int_pointer = (int*) wrapped_malloc(sizeof(int) * 100); minimally_wrapped_free(int_pointer); } diff --git a/examples/apps/cali-memtracking-macros.cpp b/examples/apps/cali-memtracking-macros.cpp index fde9b4803..3980bc8ef 100644 --- a/examples/apps/cali-memtracking-macros.cpp +++ b/examples/apps/cali-memtracking-macros.cpp @@ -10,7 +10,7 @@ size_t row_major(size_t x, size_t y, size_t width) { - return width*y + x; + return width * y + x; } void do_work(size_t M, size_t W, size_t N) @@ -19,14 +19,14 @@ void do_work(size_t M, size_t W, size_t N) CALI_MARK_BEGIN("allocate"); - double *matA = (double*)malloc(sizeof(double)*M*W); - double *matB = (double*)malloc(sizeof(double)*W*N); - double *matC = (double*)malloc(sizeof(double)*M*N); + double* matA = (double*) malloc(sizeof(double) * M * W); + double* matB = (double*) malloc(sizeof(double) * W * N); + double* matC = (double*) malloc(sizeof(double) * M * N); size_t num_dimensions = 2; - size_t A_dims[] = {M,W}; - size_t B_dims[] = {W,N}; - size_t C_dims[] = {M,N}; + size_t A_dims[] = { M, W }; + size_t B_dims[] = { W, N }; + size_t C_dims[] = { M, N }; CALI_DATATRACKER_TRACK_DIMENSIONAL(matA, sizeof(double), A_dims, num_dimensions); CALI_DATATRACKER_TRACK_DIMENSIONAL(matB, sizeof(double), B_dims, num_dimensions); @@ -37,21 +37,21 @@ void do_work(size_t M, size_t W, size_t N) // Initialize A and B randomly for (i = 0; i < M; i++) - for(k = 0; k < W; k++) - matA[row_major(i,k,M)] = rand(); + for (k = 0; k < W; k++) + matA[row_major(i, k, M)] = rand(); - for(k = 0; k < W; k++) - for(j = 0; j < N; j++) - matB[row_major(k,j,W)] = rand(); + for (k = 0; k < W; k++) + for (j = 0; j < N; j++) + matB[row_major(k, j, W)] = rand(); CALI_MARK_END("initialize values"); CALI_MARK_BEGIN("multiply"); // AB = C for (i = 0; i < M; i++) - for(j = 0; j < N; j++) - for(k = 0; k < W; k++) - matC[row_major(i,j,M)] += matA[row_major(i,k,M)] * matB[row_major(k,j,W)]; + for (j = 0; j < N; j++) + for (k = 0; k < W; k++) + matC[row_major(i, j, M)] += matA[row_major(i, k, M)] * matB[row_major(k, j, W)]; CALI_MARK_END("multiply"); CALI_MARK_BEGIN("sum"); @@ -59,8 +59,8 @@ void do_work(size_t M, size_t W, size_t N) // Print sum of elems in C double cSum = 0; for (i = 0; i < M; i++) - for(j = 0; j < N; j++) - cSum += matC[row_major(i,j,M)]; + for (j = 0; j < N; j++) + cSum += matC[row_major(i, j, M)]; std::cout << "cSum = " << cSum << std::endl; @@ -79,20 +79,12 @@ int main(int argc, const char* argv[]) // parse command line arguments const util::Args::Table option_table[] = { - { "m_size", "m_size", 'm', true, - "Width of input matrix A", - "elements" }, - { "w_size", "w_size", 'w', true, - "Height of input matrix A and width of input matrix B", - "elements" }, - { "n_size", "n_size", 'n', true, - "Height of input matrix B", - "elements" }, - { "iterations", "iterations", 'i', true, - "Number of iterations", - "iterations" }, - - util::Args::Table::Terminator + { "m_size", "m_size", 'm', true, "Width of input matrix A", "elements" }, + { "w_size", "w_size", 'w', true, "Height of input matrix A and width of input matrix B", "elements" }, + { "n_size", "n_size", 'n', true, "Height of input matrix B", "elements" }, + { "iterations", "iterations", 'i', true, "Number of iterations", "iterations" }, + + util::Args::Table::Terminator }; util::Args args(option_table); @@ -100,23 +92,22 @@ int main(int argc, const char* argv[]) int lastarg = args.parse(argc, argv); if (lastarg < argc) { - std::cerr << "cali-throughput-thread: unknown option: " << argv[lastarg] << '\n' - << " Available options: "; + std::cerr << "cali-throughput-thread: unknown option: " << argv[lastarg] << '\n' << " Available options: "; args.print_available_options(std::cerr); return -1; } - size_t m_size = std::stoul(args.get("m_size", "512")); - size_t w_size = std::stoul(args.get("w_size", "512")); - size_t n_size = std::stoul(args.get("n_size", "512")); + size_t m_size = std::stoul(args.get("m_size", "512")); + size_t w_size = std::stoul(args.get("w_size", "512")); + size_t n_size = std::stoul(args.get("n_size", "512")); size_t num_iterations = std::stoul(args.get("iterations", "4")); CALI_MARK_BEGIN("benchmark"); - + CALI_CXX_MARK_LOOP_BEGIN(loop, "loop"); - for(size_t i=0; i(cali_datatracker_allocate_dimensional("data", sizeof(int), &arraySize, 1)); + int* data = static_cast(cali_datatracker_allocate_dimensional("data", sizeof(int), &arraySize, 1)); std::srand(1337); for (size_t c = 0; c < arraySize; ++c) data[c] = std::rand() % 256; if (sort) - std::sort(data, data + arraySize); + std::sort(data, data + arraySize); return data; } -void work(int *data, size_t arraySize) +void work(int* data, size_t arraySize) { CALI_CXX_MARK_FUNCTION; long sum = 0; - for (size_t i = 0; i < 100000; ++i) - { + for (size_t i = 0; i < 100000; ++i) { // Primary loop - for (size_t c = 0; c < arraySize; ++c) - { + for (size_t c = 0; c < arraySize; ++c) { if (data[c] >= 128) sum += data[c]; } @@ -43,7 +40,7 @@ void work(int *data, size_t arraySize) std::cout << "sum = " << sum << std::endl; } -void cleanup(int *data) +void cleanup(int* data) { CALI_CXX_MARK_FUNCTION; @@ -59,7 +56,7 @@ void benchmark(size_t arraySize, bool sort) std::cout << "Initializing benchmark data with sort = " << sort << std::endl; - int *data = init(arraySize, sort); + int* data = init(arraySize, sort); std::cout << "Calculating sum of values >= 128" << std::endl; @@ -72,7 +69,7 @@ void benchmark(size_t arraySize, bool sort) std::cout << "Done!" << std::endl; } -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { CALI_CXX_MARK_FUNCTION; diff --git a/examples/apps/cali-print-snapshot.c b/examples/apps/cali-print-snapshot.c index ad43354a3..76418867c 100644 --- a/examples/apps/cali-print-snapshot.c +++ b/examples/apps/cali-print-snapshot.c @@ -39,7 +39,7 @@ int print_entry(void* user_arg, cali_id_t attr_id, cali_variant_t val) { char buf[20]; size_t len = cali_variant_get_size(val); - len = (len < 19 ? len : 19); + len = (len < 19 ? len : 19); strncpy(buf, (char*) cali_variant_get_data(&val), len); buf[len] = '\0'; @@ -54,19 +54,17 @@ int print_entry(void* user_arg, cali_id_t attr_id, cali_variant_t val) return 1; /* Non-null return value: keep processing. */ } - /* * Take and process a snapshot */ -void -snapshot(cali_id_t channel) +void snapshot(cali_id_t channel) { /* * Take a snapshot, and store it in our buffer */ unsigned char buffer[80]; - size_t len = cali_channel_pull_snapshot(channel, CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD, 80, buffer); + size_t len = cali_channel_pull_snapshot(channel, CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD, 80, buffer); if (len == 0) { fprintf(stderr, "Could not obtain snapshot!\n"); @@ -91,12 +89,10 @@ snapshot(cali_id_t channel) printf(" }. %lu bytes, %d entries.\n", bytes_read, counter); } - /* * Example function: add some annotations and take snapshots */ -void -do_work(cali_id_t channel) +void do_work(cali_id_t channel) { CALI_MARK_FUNCTION_BEGIN; CALI_MARK_LOOP_BEGIN(loopmarker, "foo"); @@ -113,12 +109,11 @@ do_work(cali_id_t channel) CALI_MARK_FUNCTION_END; } - int main() { - const char* cfg[][2] = { { NULL, NULL } }; - cali_configset_t cfgset = cali_create_configset(cfg); - cali_id_t channel = cali_create_channel("print-snapshot", 0, cfgset); + const char* cfg[][2] = { { NULL, NULL } }; + cali_configset_t cfgset = cali_create_configset(cfg); + cali_id_t channel = cali_create_channel("print-snapshot", 0, cfgset); cali_delete_configset(cfgset); CALI_MARK_FUNCTION_BEGIN; diff --git a/examples/apps/cali-regionprofile.cpp b/examples/apps/cali-regionprofile.cpp index 00bb1f111..cff3abd73 100644 --- a/examples/apps/cali-regionprofile.cpp +++ b/examples/apps/cali-regionprofile.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2019, Lawrence Livermore National Security, LLC. +// Copyright (c) 2019, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. // Example of the RegionProfile channel class @@ -12,17 +12,16 @@ void foo() CALI_CXX_MARK_FUNCTION; } - int main() { cali_config_preset("CALI_LOG_VERBOSITY", "0"); - + // The RegionProfile channel controller computes the total time spent // in annotated regions cali::RegionProfile rp; - // Activate recording + // Activate recording rp.start(); CALI_MARK_FUNCTION_BEGIN; @@ -37,7 +36,7 @@ int main() foo(); CALI_CXX_MARK_LOOP_END(mainloop); - + CALI_MARK_FUNCTION_END; // Stop recording @@ -46,16 +45,13 @@ int main() // Get and print the inclusive time spent in each region { std::map region_times; - double total_time; - - std::tie(region_times, std::ignore, total_time) = - rp.inclusive_region_times(); + double total_time; + + std::tie(region_times, std::ignore, total_time) = rp.inclusive_region_times(); std::cerr << "Inclusive time per region:" - << "\n main: " << region_times["main"] - << "\n init: " << region_times["init"] - << "\n mainloop: " << region_times["mainloop"] - << "\n foo: " << region_times["foo"] + << "\n main: " << region_times["main"] << "\n init: " << region_times["init"] + << "\n mainloop: " << region_times["mainloop"] << "\n foo: " << region_times["foo"] << "\n(Total profiling time: " << total_time << " sec)\n" << std::endl; } @@ -63,16 +59,13 @@ int main() // Get and print the exclusive time spent in each region { std::map region_times; - double total_time; - - std::tie(region_times, std::ignore, total_time) = - rp.exclusive_region_times(); + double total_time; + + std::tie(region_times, std::ignore, total_time) = rp.exclusive_region_times(); std::cerr << "Exclusive time per region:" - << "\n main: " << region_times["main"] - << "\n init: " << region_times["init"] - << "\n mainloop: " << region_times["mainloop"] - << "\n foo: " << region_times["foo"] + << "\n main: " << region_times["main"] << "\n init: " << region_times["init"] + << "\n mainloop: " << region_times["mainloop"] << "\n foo: " << region_times["foo"] << "\n(Total profiling time: " << total_time << " sec)\n" << std::endl; } @@ -80,17 +73,14 @@ int main() // Get and print the exclusive time in function regions { std::map region_times; - double total_function_time; - double total_time; - - std::tie(region_times, total_function_time, total_time) = - rp.exclusive_region_times("function"); + double total_function_time; + double total_time; + + std::tie(region_times, total_function_time, total_time) = rp.exclusive_region_times("function"); std::cerr << "Exclusive time per region (functions only):" - << "\n main: " << region_times["main"] - << "\n foo: " << region_times["foo"] - << "\n(Total exclusive time in functions: " << total_function_time - << " of " << total_time << " sec)" + << "\n main: " << region_times["main"] << "\n foo: " << region_times["foo"] + << "\n(Total exclusive time in functions: " << total_function_time << " of " << total_time << " sec)" << std::endl; - } + } } diff --git a/examples/apps/collective-output-channel.cpp b/examples/apps/collective-output-channel.cpp index e04a998ca..44c43bceb 100644 --- a/examples/apps/collective-output-channel.cpp +++ b/examples/apps/collective-output-channel.cpp @@ -13,19 +13,17 @@ #include #include - int main(int argc, char* argv[]) { MPI_Init(&argc, &argv); std::shared_ptr channel; - std::string errmsg; + std::string errmsg; // // Create a "runtime-report" channel and return a control object. - std::tie(channel, errmsg) = - cali::make_collective_output_channel("runtime-report(profile.mpi)"); + std::tie(channel, errmsg) = cali::make_collective_output_channel("runtime-report(profile.mpi)"); // // Check if the channel was created successfully. If not, the second @@ -56,7 +54,7 @@ int main(int argc, char* argv[]) // // Gather and flush channel output into the given std::ostream on // MPI_COMM_WORLD. This is a collective operation on MPI_COMM_WORLD. - // Output will be written on rank 0 on the given communicator. + // Output will be written on rank 0 on the given communicator. // Other ranks ignore the stream argument. channel->collective_flush(os, MPI_COMM_WORLD); diff --git a/examples/apps/cxx-example.cpp b/examples/apps/cxx-example.cpp index 48244b334..aa2d58c27 100644 --- a/examples/apps/cxx-example.cpp +++ b/examples/apps/cxx-example.cpp @@ -36,10 +36,13 @@ double foo(int i) // and automatically closes it at the end of the function. CALI_CXX_MARK_FUNCTION; - struct timespec sleeptime { 0, std::max(i * 500, 100000) }; + struct timespec sleeptime { + 0, std::max(i * 500, 100000) + }; + nanosleep(&sleeptime, nullptr); - return 0.5*i; + return 0.5 * i; } int main(int argc, char* argv[]) @@ -51,7 +54,7 @@ int main(int argc, char* argv[]) // Parse command-line arguments. Let users choose a Caliper performance // profiling configuration via the "-P" command-line argument. std::string configstr; - int iterations = 4; + int iterations = 4; for (int a = 1; a < argc; ++a) { if (strcmp(argv[a], "-h") == 0 || strcmp(argv[a], "--help") == 0) { @@ -69,9 +72,7 @@ int main(int argc, char* argv[]) try { iterations = std::stoi(argv[a]); } catch (std::invalid_argument) { - std::cerr << "Invalid argument: \"" << argv[a] - << "\". Expected a number." - << std::endl; + std::cerr << "Invalid argument: \"" << argv[a] << "\". Expected a number." << std::endl; return 2; } } diff --git a/include/caliper/Annotation.h b/include/caliper/Annotation.h index 7c2c6b1db..50889322a 100644 --- a/include/caliper/Annotation.h +++ b/include/caliper/Annotation.h @@ -28,8 +28,8 @@ class Function Function(const char* name); - Function(const Function&) = delete; - Function& operator = (const Function&) = delete; + Function(const Function&) = delete; + Function& operator= (const Function&) = delete; ~Function(); }; @@ -42,8 +42,8 @@ class ScopeAnnotation explicit ScopeAnnotation(const char* name); - ScopeAnnotation(const ScopeAnnotation&) = delete; - ScopeAnnotation& operator = (const ScopeAnnotation&) = delete; + ScopeAnnotation(const ScopeAnnotation&) = delete; + ScopeAnnotation& operator= (const ScopeAnnotation&) = delete; ~ScopeAnnotation(); }; @@ -56,7 +56,8 @@ class Loop public: - class Iteration { + class Iteration + { const Impl* pI; public: @@ -75,7 +76,6 @@ class Loop void end(); }; - /// \brief Instrumentation interface to add and manipulate context attributes /// /// The Annotation class is the primary source-code instrumentation interface @@ -102,8 +102,7 @@ class Loop class Annotation { struct Impl; - Impl* pI; - + Impl* pI; public: @@ -115,7 +114,7 @@ class Annotation /// of \ref cali_attr_properties values. Annotation(const char* name, int opt = 0); - typedef std::map MetadataListType ; + typedef std::map MetadataListType; /// \brief Creates an annotation object to manipulate /// the context attribute with the given \a name. /// @@ -123,14 +122,13 @@ class Annotation /// \param opt %Attribute flags. Bitwise OR combination /// of \ref cali_attr_properties values. /// \param metadata: a map of - Annotation(const char* name, const MetadataListType& metadata, int opt=0); + Annotation(const char* name, const MetadataListType& metadata, int opt = 0); Annotation(const Annotation&); ~Annotation(); - Annotation& operator = (const Annotation&); - + Annotation& operator= (const Annotation&); /// \brief Scope guard to automatically close an annotation at the end of /// the C++ scope. @@ -145,15 +143,16 @@ class Annotation /// } /// \endcode - class Guard { + class Guard + { Impl* pI; public: Guard(Annotation& a); - Guard(const Guard&) = delete; - Guard& operator = (const Guard&) = delete; + Guard(const Guard&) = delete; + Guard& operator= (const Guard&) = delete; ~Guard(); }; @@ -176,46 +175,57 @@ class Annotation Annotation& begin(int data); /// \copydoc cali::Annotation::begin(int) Annotation& begin(double data); + /// \copydoc cali::Annotation::begin(int) - Annotation& begin(const char* data) { - return begin(Variant(data)); - } + Annotation& begin(const char* data) { return begin(Variant(data)); } + /// \copydoc cali::Annotation::begin(int) - Annotation& begin(cali_attr_type type, void* data, uint64_t size) { - return begin(Variant(type, data, size)); - } + Annotation& begin(cali_attr_type type, void* data, uint64_t size) { return begin(Variant(type, data, size)); } + /// \copydoc cali::Annotation::begin(int) Annotation& begin(const Variant& data); #ifdef CALI_FORWARDING_ENABLED - template - struct head{ + template + struct head { using type = Arg; }; - template - auto begin(Args&&... args) -> typename std::enable_if::type,cali::Variant>::value,Annotation&>::type { + template + auto begin(Args&&... args) -> + typename std::enable_if::type, cali::Variant>::value, Annotation&>::type + { return begin(Variant(std::forward(args)...)); } - template - auto set(Args&&... args) -> typename std::enable_if::type,cali::Variant>::value,Annotation&>::type { + + template + auto set(Args&&... args) -> + typename std::enable_if::type, cali::Variant>::value, Annotation&>::type + { return set(Variant(std::forward(args)...)); } #else - template - Annotation& begin(const Arg& arg){ + template + Annotation& begin(const Arg& arg) + { return begin(Variant(arg)); } - template - Annotation& set(const Arg& arg){ + + template + Annotation& set(const Arg& arg) + { return set(Variant(arg)); } - template - Annotation& begin(Arg& arg){ + + template + Annotation& begin(Arg& arg) + { return begin(Variant(arg)); } - template - Annotation& set(Arg& arg){ + + template + Annotation& set(Arg& arg) + { return set(Variant(arg)); } #endif @@ -253,5 +263,4 @@ class Annotation #undef CALI_FORWARDING_ENABLED - #endif diff --git a/include/caliper/AnnotationBinding.h b/include/caliper/AnnotationBinding.h index bcf8acec8..a5912d1aa 100644 --- a/include/caliper/AnnotationBinding.h +++ b/include/caliper/AnnotationBinding.h @@ -109,29 +109,26 @@ class AnnotationBinding /// \param c The %Caliper instance /// \param chn The channel instance /// \param attr The attribute being marked - virtual void on_mark_attribute(Caliper* c, - Channel* chn, - const Attribute& attr) - { } + virtual void on_mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) {} /// \brief Callback for an annotation begin event /// \param c Caliper instance /// \param attr Attribute on which the %Caliper begin event was invoked. /// \param value The annotation name/value. - virtual void on_begin(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { } + virtual void on_begin(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) {} /// \brief Callback for an annotation end event /// \param c Caliper instance /// \param attr Attribute on which the %Caliper end event was invoked. /// \param value The annotation name/value. - virtual void on_end(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { } + virtual void on_end(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) {} /// \brief Initialization callback. Invoked after the %Caliper /// initialization completed. - virtual void initialize(Caliper* c, Channel* chn) { } + virtual void initialize(Caliper* c, Channel* chn) {} /// \brief Invoked on %Caliper finalization. - virtual void finalize(Caliper* c, Channel* chn) { } + virtual void finalize(Caliper* c, Channel* chn) {} public: @@ -153,37 +150,37 @@ class AnnotationBinding /// %Caliper callback functions. Can be used as a %Caliper service /// initialization function. template - static void make_binding(Caliper* c, Channel* chn) { + static void make_binding(Caliper* c, Channel* chn) + { BindingT* binding = new BindingT(); binding->base_pre_initialize(c, chn); binding->initialize(c, chn); binding->base_post_initialize(c, chn); - chn->events().create_attr_evt.connect( - [binding](Caliper* c, Channel* chn, const Attribute& attr){ - if (!is_subscription_attribute(attr)) - binding->check_attribute(c, chn, attr); - }); - chn->events().subscribe_attribute.connect( - [binding](Caliper* c, Channel* chn, const Attribute& attr){ + chn->events().create_attr_evt.connect([binding](Caliper* c, Channel* chn, const Attribute& attr) { + if (!is_subscription_attribute(attr)) binding->check_attribute(c, chn, attr); - }); + }); + chn->events().subscribe_attribute.connect([binding](Caliper* c, Channel* chn, const Attribute& attr) { + binding->check_attribute(c, chn, attr); + }); chn->events().pre_begin_evt.connect( - [binding](Caliper* c, Channel* chn,const Attribute& attr,const Variant& value){ - binding->begin_cb(c,chn,attr,value); - }); + [binding](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + binding->begin_cb(c, chn, attr, value); + } + ); chn->events().pre_end_evt.connect( - [binding](Caliper* c, Channel* chn,const Attribute& attr,const Variant& value){ - binding->end_cb(c,chn,attr,value); - }); - chn->events().finish_evt.connect( - [binding](Caliper* c, Channel* chn){ - binding->finalize(c,chn); - delete binding; - }); - - Log(1).stream() << "Registered " << binding->service_tag() - << " binding for channel " << chn->name() << std::endl; + [binding](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + binding->end_cb(c, chn, attr, value); + } + ); + chn->events().finish_evt.connect([binding](Caliper* c, Channel* chn) { + binding->finalize(c, chn); + delete binding; + }); + + Log(1).stream() << "Registered " << binding->service_tag() << " binding for channel " << chn->name() + << std::endl; } }; diff --git a/include/caliper/CaliFunctional.h b/include/caliper/CaliFunctional.h index 58e235a59..7b17e771b 100644 --- a/include/caliper/CaliFunctional.h +++ b/include/caliper/CaliFunctional.h @@ -7,13 +7,9 @@ #ifndef CALI_FUNCTIONAL_H #define CALI_FUNCTIONAL_H - -#define GCC_VERSION (__GNUC__ * 10000 \ - + __GNUC_MINOR__ * 100 \ - + __GNUC_PATCHLEVEL__) +#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) /* Test for GCC > 3.2.0 */ - #define VARIADIC_RETURN_SAFE #include "Annotation.h" @@ -26,244 +22,284 @@ #include #include -namespace cali{ -struct SafeAnnotation{ +namespace cali +{ +struct SafeAnnotation { public: + //ValidatedAnnotation() : inner_annot("ERROR"){} - SafeAnnotation(const char* name, int opt=0) : inner_annot(name,opt){ - inner_annot = Annotation(name,opt); - } - SafeAnnotation(SafeAnnotation& other) : inner_annot(other.getAnnot()) { - - } - Annotation& getAnnot() { - return inner_annot; - } - SafeAnnotation& operator = (SafeAnnotation& other){ + SafeAnnotation(const char* name, int opt = 0) : inner_annot(name, opt) { inner_annot = Annotation(name, opt); } + + SafeAnnotation(SafeAnnotation& other) : inner_annot(other.getAnnot()) {} + + Annotation& getAnnot() { return inner_annot; } + + SafeAnnotation& operator= (SafeAnnotation& other) + { inner_annot = other.getAnnot(); return *this; } - SafeAnnotation& begin(){ + + SafeAnnotation& begin() + { inner_annot.begin(); return *this; } - SafeAnnotation& begin(int data){ + + SafeAnnotation& begin(int data) + { inner_annot.begin(data); return *this; } - SafeAnnotation& begin(double data){ + + SafeAnnotation& begin(double data) + { inner_annot.begin(data); return *this; } - SafeAnnotation& begin(const char* data){ + + SafeAnnotation& begin(const char* data) + { inner_annot.begin(Variant(CALI_TYPE_STRING, data, strlen(data))); return *this; } - SafeAnnotation& begin(cali_attr_type type, void* data, uint64_t size){ - inner_annot.begin(type,data,size); + + SafeAnnotation& begin(cali_attr_type type, void* data, uint64_t size) + { + inner_annot.begin(type, data, size); return *this; } - SafeAnnotation& begin(const Variant& data){ + + SafeAnnotation& begin(const Variant& data) + { inner_annot.begin(data); return *this; } + //set - SafeAnnotation& set(int data){ + SafeAnnotation& set(int data) + { inner_annot.set(data); return *this; } - SafeAnnotation& set(double data){ + + SafeAnnotation& set(double data) + { inner_annot.set(data); return *this; } - SafeAnnotation& set(const char* data){ + + SafeAnnotation& set(const char* data) + { inner_annot.set(Variant(CALI_TYPE_STRING, data, strlen(data))); return *this; } - SafeAnnotation& set(cali_attr_type type, void* data, uint64_t size){ - inner_annot.set(type,data,size); + + SafeAnnotation& set(cali_attr_type type, void* data, uint64_t size) + { + inner_annot.set(type, data, size); return *this; } - SafeAnnotation& set(const Variant& data){ + + SafeAnnotation& set(const Variant& data) + { inner_annot.set(data); return *this; } - void end(){ - inner_annot.end(); - } - template - SafeAnnotation & begin(T start){ + + void end() { inner_annot.end(); } + + template + SafeAnnotation& begin(T start) + { inner_annot.begin("Unmeasurable"); return *this; } - template - SafeAnnotation & set(T start){ + + template + SafeAnnotation& set(T start) + { inner_annot.set("Unmeasurable"); return *this; } + Annotation inner_annot; }; -cali::SafeAnnotation& wrapper_annotation(){ - static cali::SafeAnnotation instance("wrapped_function"); - return instance; -} +cali::SafeAnnotation& wrapper_annotation() +{ + static cali::SafeAnnotation instance("wrapped_function"); + return instance; +} -template -const char* annotation_name(){ - static std::string name = "function_argument_"+std::to_string(N); +template +const char* annotation_name() +{ + static std::string name = "function_argument_" + std::to_string(N); return (name.c_str()); } -template -cali::SafeAnnotation& arg_annotation(){ - static cali::SafeAnnotation instance(annotation_name()); - return instance; -} -template -cali::Annotation& arg_annotation_raw(){ - static cali::Annotation instance(annotation_name()); - return instance; -} +template +cali::SafeAnnotation& arg_annotation() +{ + static cali::SafeAnnotation instance(annotation_name()); + return instance; +} + +template +cali::Annotation& arg_annotation_raw() +{ + static cali::Annotation instance(annotation_name()); + return instance; +} //Wrap a call to a function -template -auto wrap(const char* name, LB body, Args... args) -> typename std::result_of::type{ - Annotation startedAnnot = wrapper_annotation().begin(name).getAnnot(); +template +auto wrap(const char* name, LB body, Args... args) -> typename std::result_of::type +{ + Annotation startedAnnot = wrapper_annotation().begin(name).getAnnot(); cali::Annotation::Guard func_annot(startedAnnot); return body(args...); } -cali::Annotation& dummy_annot(){ - static cali::Annotation instance ("wrapped func"); +cali::Annotation& dummy_annot() +{ + static cali::Annotation instance("wrapped func"); return instance; } -template -struct ArgRecorder{ - static auto record(const char* name) -> std::tuple<> { - return std::make_tuple(); - } +template +struct ArgRecorder { + static auto record(const char* name) -> std::tuple<> { return std::make_tuple(); } }; template -struct ArgRecorder{ - using NextRecorder = ArgRecorder; +struct ArgRecorder { + using NextRecorder = ArgRecorder; + static auto record(const char* name, Arg arg, Args... args) -> decltype(std::tuple_cat( - NextRecorder::record(name,args...), - std::move(std::forward_as_tuple(std::move(cali::Annotation::Guard(dummy_annot()) ))) - ) - ){ - cali::Annotation::Guard func_annot (arg_annotation().begin(arg).getAnnot()); - return std::tuple_cat( - NextRecorder::record(name,args...), - std::forward_as_tuple(std::move(func_annot)) - ); + NextRecorder::record(name, args...), + std::move(std::forward_as_tuple(std::move(cali::Annotation::Guard(dummy_annot())))) + )) + { + cali::Annotation::Guard func_annot(arg_annotation().begin(arg).getAnnot()); + return std::tuple_cat(NextRecorder::record(name, args...), std::forward_as_tuple(std::move(func_annot))); } }; //TODO: fix -template -auto wrap_with_args(const char* name, LB body, Args... args) -> typename std::result_of::type{ - Annotation startedAnnot = wrapper_annotation().begin(name).getAnnot(); +template +auto wrap_with_args(const char* name, LB body, Args... args) -> typename std::result_of::type +{ + Annotation startedAnnot = wrapper_annotation().begin(name).getAnnot(); cali::Annotation::Guard func_annot(startedAnnot); - #ifdef VARIADIC_RETURN_SAFE - auto n =ArgRecorder<1,Args...>::record(name,args...); - #else - #warning CALIPER WARNING: This C++ compiler has bugs which prevent argument profiling - #endif +#ifdef VARIADIC_RETURN_SAFE + auto n = ArgRecorder<1, Args...>::record(name, args...); +#else +#warning CALIPER WARNING: This C++ compiler has bugs which prevent argument profiling +#endif return body(args...); } -//Functor containing a function which should always be wrapped. Should not be instantiated directly, +//Functor containing a function which should always be wrapped. Should not be instantiated directly, //but through calls to wrap_function below -template +template struct WrappedFunction { - WrappedFunction(const char* func_name, LB func) : body(func){ - name = func_name; - } + WrappedFunction(const char* func_name, LB func) : body(func) { name = func_name; } + template - auto operator()(Args... args) -> typename std::result_of::type { + auto operator() (Args... args) -> typename std::result_of::type + { cali::Annotation::Guard func_annot(wrapper_annotation().begin(name).getAnnot()); return body(args...); } - LB body; + + LB body; const char* name; }; -template +template struct ArgWrappedFunction { - ArgWrappedFunction(const char* func_name, LB func) : body(func){ - name = func_name; - } + ArgWrappedFunction(const char* func_name, LB func) : body(func) { name = func_name; } + template - auto operator()(Args... args) -> typename std::enable_if< + auto operator() (Args... args) -> typename std::enable_if< !std::is_same::type, void>::value, - typename std::result_of::type>::type { - cali::Annotation::Guard func_annot( - wrapper_annotation().begin(name).getAnnot()); - #ifdef VARIADIC_RETURN_SAFE - auto n =ArgRecorder<1,Args...>::record(name,args...); - #else - #warning CALIPER WARNING: This C++ compiler has bugs which prevent argument profiling - #endif - auto return_value = body(args...); - cali::Annotation return_value_annot("return"); - return_value_annot.set(return_value); - return_value_annot.end(); - return return_value; + typename std::result_of::type>::type + { + cali::Annotation::Guard func_annot(wrapper_annotation().begin(name).getAnnot()); +#ifdef VARIADIC_RETURN_SAFE + auto n = ArgRecorder<1, Args...>::record(name, args...); +#else +#warning CALIPER WARNING: This C++ compiler has bugs which prevent argument profiling +#endif + auto return_value = body(args...); + cali::Annotation return_value_annot("return"); + return_value_annot.set(return_value); + return_value_annot.end(); + return return_value; } + template - auto operator()(Args... args) -> typename std::enable_if< + auto operator() (Args... args) -> typename std::enable_if< std::is_same::type, void>::value, - typename std::result_of::type>::type { - cali::Annotation::Guard func_annot( - wrapper_annotation().begin(name).getAnnot()); - #ifdef VARIADIC_RETURN_SAFE - auto n =ArgRecorder<1,Args...>::record(name,args...); - #else - #warning CALIPER WARNING: This C++ compiler has bugs which prevent argument profiling - #endif - return body(args...); + typename std::result_of::type>::type + { + cali::Annotation::Guard func_annot(wrapper_annotation().begin(name).getAnnot()); +#ifdef VARIADIC_RETURN_SAFE + auto n = ArgRecorder<1, Args...>::record(name, args...); +#else +#warning CALIPER WARNING: This C++ compiler has bugs which prevent argument profiling +#endif + return body(args...); } - LB body; + + LB body; const char* name; }; //Helper factory function to create WrappedFunction objects -template -WrappedFunction wrap_function(const char* name, LB body){ - return WrappedFunction(name,body); +template +WrappedFunction wrap_function(const char* name, LB body) +{ + return WrappedFunction(name, body); } -template -ArgWrappedFunction wrap_function_and_args(const char* name, LB body){ - return ArgWrappedFunction(name,body); +template +ArgWrappedFunction wrap_function_and_args(const char* name, LB body) +{ + return ArgWrappedFunction(name, body); } -template -struct Recordable{ - static void record(InstanceType instance){}; +template +struct Recordable { + static void record(InstanceType instance) {}; }; -template -struct MapRecordingOperator{ - static auto record() -> void { - } +template +struct MapRecordingOperator { + static auto record() -> void {} }; -template -void recordIfPossible(const MaybeRecordable instance){ + +template +void recordIfPossible(const MaybeRecordable instance) +{ Recordable::record(instance); } -template -void recordIfPossible(const MaybeRecordable* instance){ + +template +void recordIfPossible(const MaybeRecordable* instance) +{ Recordable::record(*instance); } + cali::Annotation argument_number_annot("argument"); + template -struct MapRecordingOperator{ - static auto record(Arg arg, Args... args) -> void { +struct MapRecordingOperator { + static auto record(Arg arg, Args... args) -> void + { argument_number_annot.set(static_cast(sizeof...(args))); recordIfPossible(arg); argument_number_annot.end(); // can be moved to callsite for speed, looks bad though @@ -271,60 +307,60 @@ struct MapRecordingOperator{ } }; - - -template -void recordAll(Args... args){ - MapRecordingOperator::record(args...); +template +void recordAll(Args... args) +{ + MapRecordingOperator::record(args...); } cali::Annotation recording_phase("recording_phase"); -template + +template struct RecordedFunction { - RecordedFunction(const char* func_name, LB func) : body(func){ - name = func_name; - } + RecordedFunction(const char* func_name, LB func) : body(func) { name = func_name; } + template - auto operator()(Args... args) -> typename std::enable_if< + auto operator() (Args... args) -> typename std::enable_if< !std::is_same::type, void>::value, - typename std::result_of::type>::type { - recording_phase.begin("pre"); - recordAll(args...); - recording_phase.end(); - cali::Annotation::Guard func_annot( - cali::Annotation("debugged_function").begin(name) - ); - auto return_value = body(args...); - //cali::Annotation return_value_annot("return"); - //return_value_annot.set(return_value); - //return_value_annot.end(); - recording_phase.begin("post"); - recordAll(args...); - recording_phase.end(); - return return_value; + typename std::result_of::type>::type + { + recording_phase.begin("pre"); + recordAll(args...); + recording_phase.end(); + cali::Annotation::Guard func_annot(cali::Annotation("debugged_function").begin(name)); + auto return_value = body(args...); + //cali::Annotation return_value_annot("return"); + //return_value_annot.set(return_value); + //return_value_annot.end(); + recording_phase.begin("post"); + recordAll(args...); + recording_phase.end(); + return return_value; } + template - auto operator()(Args... args) -> typename std::enable_if< + auto operator() (Args... args) -> typename std::enable_if< std::is_same::type, void>::value, - typename std::result_of::type>::type { - cali::Annotation::Guard func_annot( - cali::Annotation("debugged_function").begin(name) - ); - recording_phase.begin("pre"); - recordAll(args...); - recording_phase.end(); - body(args...); - recording_phase.begin("post"); - recordAll(args...); - recording_phase.end(); + typename std::result_of::type>::type + { + cali::Annotation::Guard func_annot(cali::Annotation("debugged_function").begin(name)); + recording_phase.begin("pre"); + recordAll(args...); + recording_phase.end(); + body(args...); + recording_phase.begin("post"); + recordAll(args...); + recording_phase.end(); } - LB body; + + LB body; const char* name; }; -template -RecordedFunction make_recorded_function(const char* name, LB body){ - return RecordedFunction(name,body); +template +RecordedFunction make_recorded_function(const char* name, LB body) +{ + return RecordedFunction(name, body); } } // end namespace cali diff --git a/include/caliper/Caliper.h b/include/caliper/Caliper.h index f1fa8a1f3..e9d15be2e 100644 --- a/include/caliper/Caliper.h +++ b/include/caliper/Caliper.h @@ -24,16 +24,16 @@ namespace cali // --- Forward declarations -class Caliper; +class Caliper; struct CaliperService; -class Node; -class RuntimeConfig; +class Node; +class RuntimeConfig; // --- Typedefs /// \brief A callback function to process snapshot records when flushing /// snapshot buffers. -typedef std::function&)> SnapshotFlushFn; +typedef std::function&)> SnapshotFlushFn; /// \brief Maintain a single data collection configuration with /// callbacks and associated measurement data. @@ -46,14 +46,13 @@ class Channel public: - constexpr Channel() - : mP { nullptr } { } + constexpr Channel() : mP { nullptr } {} Channel(const Channel&) = default; - Channel(Channel&&) = default; + Channel(Channel&&) = default; - Channel& operator = (const Channel&) = default; - Channel& operator = (Channel&&) = default; + Channel& operator= (const Channel&) = default; + Channel& operator= (Channel&&) = default; ~Channel(); @@ -61,66 +60,57 @@ class Channel /// \brief Holds the %Caliper callbacks for a channel. struct Events { - typedef util::callback - attribute_cbvec; - typedef util::callback - update_cbvec; - typedef util::callback - caliper_cbvec; - - typedef util::callback - snapshot_cbvec; - typedef util::callback - process_snapshot_cbvec; - typedef util::callback&)> - edit_snapshot_cbvec; - - typedef util::callback - flush_cbvec; - typedef util::callback - write_cbvec; - - typedef util::callback - track_mem_cbvec; - typedef util::callback - untrack_mem_cbvec; + typedef util::callback attribute_cbvec; + typedef util::callback update_cbvec; + typedef util::callback caliper_cbvec; + + typedef util::callback snapshot_cbvec; + typedef util::callback process_snapshot_cbvec; + typedef util::callback&)> edit_snapshot_cbvec; + + typedef util::callback flush_cbvec; + typedef util::callback write_cbvec; + + typedef util::callback< + void(Caliper*, Channel*, const void*, const char*, size_t, size_t, const size_t*, size_t, const Attribute*, const Variant*)> + track_mem_cbvec; + typedef util::callback untrack_mem_cbvec; /// \brief Invoked when a new attribute has been created. - attribute_cbvec create_attr_evt; + attribute_cbvec create_attr_evt; /// \brief Invoked on region begin, \e before it has been put on the blackboard. - update_cbvec pre_begin_evt; + update_cbvec pre_begin_evt; /// \brief Invoked on region begin, \e after it has been put on the blackboard. - update_cbvec post_begin_evt; + update_cbvec post_begin_evt; /// \brief Invoked when value is set, \e before it has been put on the blackboard. - update_cbvec pre_set_evt; + update_cbvec pre_set_evt; /// \brief Invoked on region end, \e before it has been removed from the blackboard. - update_cbvec pre_end_evt; + update_cbvec pre_end_evt; /// \brief Invoked when a new thread context is being created. - caliper_cbvec create_thread_evt; + caliper_cbvec create_thread_evt; /// \brief Invoked when a thread context is being released. - caliper_cbvec release_thread_evt; + caliper_cbvec release_thread_evt; /// \brief Invoked at the end of a %Caliper channel initialization. /// /// At this point, all registered services have been initialized. - caliper_cbvec post_init_evt; + caliper_cbvec post_init_evt; /// \brief Invoked prior to %Caliper channel finalization. - caliper_cbvec pre_finish_evt; + caliper_cbvec pre_finish_evt; /// \brief Invoked at the end of %Caliper channel finalization. /// /// At this point, other services in this channel may already be /// destroyed. It is no longer safe to use any %Caliper API calls for /// this channel. It is for local cleanup only. - caliper_cbvec finish_evt; + caliper_cbvec finish_evt; /// \brief Invoked when a snapshot is being taken. /// /// Use this callback to take performance measurements and append them /// to the snapshot record. - snapshot_cbvec snapshot; + snapshot_cbvec snapshot; /// \brief Invoked when a snapshot has been completed. /// /// Used by snapshot processing services (e.g., trace and aggregate) to @@ -128,29 +118,29 @@ class Channel process_snapshot_cbvec process_snapshot; /// \brief Invoked before flush. - write_cbvec pre_flush_evt; + write_cbvec pre_flush_evt; /// \brief Flush all snapshot records. - flush_cbvec flush_evt; + flush_cbvec flush_evt; /// \brief Invoked after flush. - write_cbvec post_flush_evt; + write_cbvec post_flush_evt; /// \brief Modify snapshot records during flush. - edit_snapshot_cbvec postprocess_snapshot; + edit_snapshot_cbvec postprocess_snapshot; /// \brief Write output. /// /// This is invoked by the Caliper::flush_and_write() API call, and /// causes output services (e.g., report or recorder) to trigger a /// flush. - write_cbvec write_output_evt; + write_cbvec write_output_evt; /// \brief Invoked at a memory region begin. - track_mem_cbvec track_mem_evt; + track_mem_cbvec track_mem_evt; /// \brief Invoked at memory region end. - untrack_mem_cbvec untrack_mem_evt; + untrack_mem_cbvec untrack_mem_evt; /// \brief Clear local storage (trace buffers, aggregation DB) - caliper_cbvec clear_evt; + caliper_cbvec clear_evt; /// \brief Process events for a subscription attribute in this channel. /// @@ -161,41 +151,41 @@ class Channel /// them, for example with wrapper services like IO or /// MPI, where regions should only be tracked if the service is enabled /// in the given channel. - attribute_cbvec subscribe_attribute; + attribute_cbvec subscribe_attribute; }; /// \brief Access the callback vectors to register callbacks for this channel. - Events& events(); + Events& events(); /// \brief Return the configuration for this channel. - RuntimeConfig config(); + RuntimeConfig config(); // --- Channel management /// \brief Return the channel's name. - std::string name() const; + std::string name() const; /// \brief Is the channel currently active? /// /// Channels can be enabled and disabled with Caliper::activate_channel() /// and Caliper::deactivate_channel(). - bool is_active() const; + bool is_active() const; - cali_id_t id() const; + cali_id_t id() const; - operator bool() const { return mP.use_count() > 0; } + operator bool () const { return mP.use_count() > 0; } friend class Caliper; - friend bool operator == (const Channel&, const Channel&); - friend bool operator != (const Channel&, const Channel&); + friend bool operator== (const Channel&, const Channel&); + friend bool operator!= (const Channel&, const Channel&); }; -inline bool operator == (const Channel& a, const Channel& b) +inline bool operator== (const Channel& a, const Channel& b) { return a.mP == b.mP; } -inline bool operator != (const Channel& a, const Channel& b) +inline bool operator!= (const Channel& a, const Channel& b) { return a.mP != b.mP; } @@ -227,9 +217,7 @@ class Caliper : public CaliperMetadataAccessInterface bool m_is_signal; // are we in a signal handler? - Caliper(GlobalData* g, ThreadData* t, bool sig) - : sG(g), sT(t), m_is_signal(sig) - { } + Caliper(GlobalData* g, ThreadData* t, bool sig) : sG(g), sT(t), m_is_signal(sig) {} void release_thread(); @@ -255,7 +243,7 @@ class Caliper : public CaliperMetadataAccessInterface /// /// \param attr Attribute key /// \param data Value to set - void begin(const Attribute& attr, const Variant& data); + void begin(const Attribute& attr, const Variant& data); /// \brief Pop/remove top-most entry with \a attr from /// the process or thread blackboard. @@ -266,7 +254,7 @@ class Caliper : public CaliperMetadataAccessInterface /// This function is signal safe. /// /// \param attr Attribute key. - void end(const Attribute& attr); + void end(const Attribute& attr); /// \brief Pop/remove top-most \a attr entry from blackboard /// and check if current value is equal to \a data @@ -277,7 +265,7 @@ class Caliper : public CaliperMetadataAccessInterface /// This function is signal safe. /// /// \param attr Attribute key. - void end_with_value_check(const Attribute& attr, const Variant& data); + void end_with_value_check(const Attribute& attr, const Variant& data); /// \brief Set attribute:value pair on the process or thread blackboard. /// @@ -291,7 +279,7 @@ class Caliper : public CaliperMetadataAccessInterface /// /// \param attr Attribute key /// \param data Value to set - void set(const Attribute& attr, const Variant& data); + void set(const Attribute& attr, const Variant& data); /// \} /// \name Memory region tracking (across channels) @@ -311,18 +299,20 @@ class Caliper : public CaliperMetadataAccessInterface /// record for this memory region /// \param extra_attrs Attribute keys for additional attribute:value pairs /// \param estra_vals Values for additional attribute:value pairs - void memory_region_begin(const void* ptr, - const char* label, - size_t elem_size, - size_t ndim, - const size_t dims[], - size_t n_extra = 0, - const Attribute* extra_attrs = nullptr, - const Variant* extra_vals = nullptr); + void memory_region_begin( + const void* ptr, + const char* label, + size_t elem_size, + size_t ndim, + const size_t dims[], + size_t n_extra = 0, + const Attribute* extra_attrs = nullptr, + const Variant* extra_vals = nullptr + ); /// \brief De-register a tracked memory region starting at \a ptr /// \sa memory_region_begin() - void memory_region_end(const void* ptr); + void memory_region_end(const void* ptr); // // --- Per-channel API @@ -343,7 +333,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param trigger_info A caller-provided list of attributes that is passed /// to the snapshot and process_snapshot callbacks, and added to the /// returned snapshot record. - void push_snapshot(Channel* channel, SnapshotView trigger_info); + void push_snapshot(Channel* channel, SnapshotView trigger_info); /// \brief Return context data from blackboards. /// @@ -353,7 +343,7 @@ class Caliper : public CaliperMetadataAccessInterface /// This function is signal safe. /// /// \param rec The snapshot record buffer to update. - void pull_context(SnapshotBuilder& rec); + void pull_context(SnapshotBuilder& rec); /// \brief Trigger and return a snapshot. /// @@ -376,7 +366,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param trigger_info A caller-provided record that is passed to the /// snapshot callback, and added to the returned snapshot record. /// \param rec The snapshot record buffer to update. - void pull_snapshot(Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec); + void pull_snapshot(Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec); // --- Flush and I/O API @@ -386,7 +376,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \brief Flush aggregation/trace buffer contents into the \a proc_fn /// processing function. - void flush(Channel* channel, SnapshotView flush_info, SnapshotFlushFn proc_fn); + void flush(Channel* channel, SnapshotView flush_info, SnapshotFlushFn proc_fn); /// \brief Flush snapshot buffer contents on \a channel into the registered /// output services. @@ -397,7 +387,7 @@ class Caliper : public CaliperMetadataAccessInterface /// /// \param channel The channel to flush /// \param input_flush_info User-provided flush context information - void flush_and_write(Channel* channel, SnapshotView flush_info); + void flush_and_write(Channel* channel, SnapshotView flush_info); /// \brief Clear snapshot buffers on \a channel /// @@ -405,7 +395,7 @@ class Caliper : public CaliperMetadataAccessInterface /// that has not been written yet will be lost. /// /// This function is not signal safe. - void clear(Channel* channel); + void clear(Channel* channel); // --- Annotation API @@ -429,7 +419,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param data Value to set /// /// \sa begin(const Attribute&, const Variant&) - void begin(Channel* channel, const Attribute& attr, const Variant& data); + void begin(Channel* channel, const Attribute& attr, const Variant& data); /// \brief Pop/remove top-most entry with given attribute from /// the channel blackboard. @@ -442,7 +432,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param attr Attribute key. /// /// \sa end(const Attribute&) - void end(Channel* channel, const Attribute& attr); + void end(Channel* channel, const Attribute& attr); /// \brief Set attribute:value pair on the channel blackboard. /// @@ -457,15 +447,24 @@ class Caliper : public CaliperMetadataAccessInterface /// \param attr Attribute key /// \param data Value to set /// \sa set(const Attribute&, const Variant&) - void set(Channel* channel, const Attribute& attr, const Variant& data); + void set(Channel* channel, const Attribute& attr, const Variant& data); /// \} /// \name Memory region tracking (single channel) /// \{ - void memory_region_begin(Channel* chn, const void* ptr, const char* label, size_t elem_size, size_t ndim, const size_t dims[], - size_t n = 0, const Attribute* extra_attr = nullptr, const Variant* extra_val = nullptr); - void memory_region_end(Channel* chn, const void* ptr); + void memory_region_begin( + Channel* chn, + const void* ptr, + const char* label, + size_t elem_size, + size_t ndim, + const size_t dims[], + size_t n = 0, + const Attribute* extra_attr = nullptr, + const Variant* extra_val = nullptr + ); + void memory_region_end(Channel* chn, const void* ptr); /// \} /// \name Blackboard access @@ -480,7 +479,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param data The new value. /// /// \return The previous value \a attr - Variant exchange(const Attribute& attr, const Variant& data); + Variant exchange(const Attribute& attr, const Variant& data); /// \brief Retrieve top-most entry for the given attribute key from the /// process or thread blackboard. @@ -491,7 +490,7 @@ class Caliper : public CaliperMetadataAccessInterface /// /// \return The top-most entry on the blackboard for the given attribute key. /// An empty Entry object if this attribute is not set. - Entry get(const Attribute& attr); + Entry get(const Attribute& attr); /// \brief Retrieve top-most entry for the given attribute key from the /// channel blackboard of the given channel. @@ -502,10 +501,10 @@ class Caliper : public CaliperMetadataAccessInterface /// /// \return The top-most entry on the blackboard for the given attribute key. /// An empty Entry object if this attribute is not set. - Entry get(Channel* channel, const Attribute& attr); + Entry get(Channel* channel, const Attribute& attr); /// \brief Retrieve the current path entry from the blackboard. - Entry get_path_node(); + Entry get_path_node(); /// \brief Return all global attributes for the default channel /// \sa get_globals(Channel*) @@ -530,11 +529,13 @@ class Caliper : public CaliperMetadataAccessInterface /// \param data Value list /// \param list Output record builder /// \param parent (Optional) parent node for any treee elements. - void make_record(size_t n, - const Attribute attr[], - const Variant data[], - SnapshotBuilder& rec, - cali::Node* parent = nullptr); + void make_record( + size_t n, + const Attribute attr[], + const Variant data[], + SnapshotBuilder& rec, + cali::Node* parent = nullptr + ); // --- Metadata Access Interface @@ -543,7 +544,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \{ /// \brief Return \a true if the attribute with name \a name exists - bool attribute_exists(const std::string& name) const; + bool attribute_exists(const std::string& name) const; /// \brief Return the attribute object for a given attribute ID Attribute get_attribute(cali_id_t id) const; @@ -581,15 +582,17 @@ class Caliper : public CaliperMetadataAccessInterface /// entries. /// \param meta_val Metadata values. An array of n_meta values. /// \return The created attribute. - Attribute create_attribute(const std::string& name, - cali_attr_type type, - int prop = CALI_ATTR_DEFAULT, - int meta = 0, - const Attribute* meta_attr = nullptr, - const Variant* meta_data = nullptr); + Attribute create_attribute( + const std::string& name, + cali_attr_type type, + int prop = CALI_ATTR_DEFAULT, + int meta = 0, + const Attribute* meta_attr = nullptr, + const Variant* meta_data = nullptr + ); /// \brief Return node with given \a id - Node* node(cali_id_t id) const; + Node* node(cali_id_t id) const; /// \brief Merge all nodes in \a nodelist into a single path under \a parent /// @@ -603,7 +606,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param parent Construct path off this parent node /// /// \return Node pointing to the new path - Node* make_tree_entry(size_t n, const Node* nodelist[], Node* parent = nullptr); + Node* make_tree_entry(size_t n, const Node* nodelist[], Node* parent = nullptr); /// \brief Return a context tree path for the given key:value pair /// @@ -614,7 +617,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param parent Construct path off this parent node /// /// \return Node pointing to the end of the new path - Node* make_tree_entry(const Attribute& attr, const Variant& value, Node* parent = nullptr); + Node* make_tree_entry(const Attribute& attr, const Variant& value, Node* parent = nullptr); /// \brief Return a context tree branch for the list of values with the given attribute. /// @@ -626,7 +629,7 @@ class Caliper : public CaliperMetadataAccessInterface /// \param parent Construct path off this parent node /// /// \return Node pointing to the end of the new path - Node* make_tree_entry(const Attribute& attr, size_t n, const Variant values[], Node* parent = nullptr); + Node* make_tree_entry(const Attribute& attr, size_t n, const Variant values[], Node* parent = nullptr); /// \} /// \name Channel API @@ -652,31 +655,31 @@ class Caliper : public CaliperMetadataAccessInterface /// in %Caliper log output. /// \param cfg The channel's runtime configuration. /// \return The channel - Channel create_channel(const char* name, const RuntimeConfig& cfg); + Channel create_channel(const char* name, const RuntimeConfig& cfg); /// \brief Return all existing channels std::vector get_all_channels(); /// \brief Return the channel with the given ID or an empty Channel object. - Channel get_channel(cali_id_t id); + Channel get_channel(cali_id_t id); /// \brief Delete the given channel. /// /// Deleting channels is \b not thread-safe. - void delete_channel(Channel& chn); + void delete_channel(Channel& chn); /// \brief Activate the given channel. /// /// Inactive channels will not track or process annotations and other /// blackboard updates. - void activate_channel(Channel& chn); + void activate_channel(Channel& chn); /// \brief Deactivate the given channel. /// \copydetails Caliper::activate_channel - void deactivate_channel(Channel& chn); + void deactivate_channel(Channel& chn); /// \brief Flush and delete all channels - void finalize(); + void finalize(); /// \} @@ -686,12 +689,11 @@ class Caliper : public CaliperMetadataAccessInterface /// \see instance() Caliper(); - ~Caliper() - { } + ~Caliper() {} Caliper(const Caliper&) = default; - Caliper& operator = (const Caliper&) = default; + Caliper& operator= (const Caliper&) = default; /// \brief Check if this is a signal-safe %Caliper instance. /// @@ -749,11 +751,11 @@ class Caliper : public CaliperMetadataAccessInterface static Caliper sigsafe_instance(); /// \brief Test if Caliper has been initialized yet. - static bool is_initialized(); + static bool is_initialized(); /// \brief Release %Caliper. Note that %Caliper cannot be re-initialized /// after it has been released. - static void release(); + static void release(); /// \brief Add a list of %Caliper service specs. /// @@ -773,7 +775,7 @@ class Caliper : public CaliperMetadataAccessInterface /// /// Caliper::add_services(my_services); /// \endcode - static void add_services(const CaliperService*); + static void add_services(const CaliperService*); friend struct GlobalData; }; diff --git a/include/caliper/CaliperService.h b/include/caliper/CaliperService.h index 7baa0dfc2..58c6f17e3 100644 --- a/include/caliper/CaliperService.h +++ b/include/caliper/CaliperService.h @@ -20,7 +20,7 @@ typedef void (*ServiceRegisterFn)(Caliper* c, Channel* chn); /// to Caliper::add_services() _before_ %Caliper is initialized. struct CaliperService { /// \brief Name (old style) or JSON spec (new) of the service - const char* name_or_spec; + const char* name_or_spec; /// \brief Registration function ServiceRegisterFn register_fn; }; diff --git a/include/caliper/ChannelController.h b/include/caliper/ChannelController.h index afa32ad12..d9246e3fa 100644 --- a/include/caliper/ChannelController.h +++ b/include/caliper/ChannelController.h @@ -48,23 +48,23 @@ class ChannelController config_map_t& config(); /// \brief Provide access to the underlying metadata info map. - info_map_t& metadata(); + info_map_t& metadata(); /// \brief Create the channel with the controller's /// name, flags, and config map - Channel create(); + Channel create(); /// \brief Callback function, invoked after the underlying channel /// has been created. /// /// This can be used to setup additional functionality, e.g. /// registering %Caliper callbacks. - virtual void on_create(Caliper* /*c*/, Channel& /*chn*/) { } + virtual void on_create(Caliper* /*c*/, Channel& /*chn*/) {} public: /// \brief Return the underlying %Caliper channel object. - Channel channel(); + Channel channel(); /// \brief Create and activate the %Caliper channel, /// or reactivate a stopped %Caliper channel. @@ -94,6 +94,6 @@ class ChannelController virtual ~ChannelController(); }; -} // namespace cali; +} // namespace cali #endif \ No newline at end of file diff --git a/include/caliper/CollectiveOutputChannel.h b/include/caliper/CollectiveOutputChannel.h index 1a6a275c0..cb3abf7c7 100644 --- a/include/caliper/CollectiveOutputChannel.h +++ b/include/caliper/CollectiveOutputChannel.h @@ -49,8 +49,7 @@ class CollectiveOutputChannel /// \return A new std::shared_ptr-wrapped CollectiveOutputChannel, or /// an empty \c std::shared_ptr object if \a from couldn't be /// made into a collective output channel. - static std::shared_ptr - from(const std::shared_ptr& from); + static std::shared_ptr from(const std::shared_ptr& from); /// \brief Aggregate data from MPI ranks in \a comm and write it into /// \a os. @@ -146,8 +145,7 @@ class CollectiveOutputChannel /// std::shared_ptr-wrapped CollectiveOutputChannel object for the /// given configuration. If the \c std::shared_ptr object is empty, an error /// occured and the second pair element contains an error message. -std::pair< std::shared_ptr, std::string > -make_collective_output_channel(const char* config_str); +std::pair, std::string> make_collective_output_channel(const char* config_str); } // namespace cali diff --git a/include/caliper/ConfigManager.h b/include/caliper/ConfigManager.h index 3842de3a7..4a0e19692 100644 --- a/include/caliper/ConfigManager.h +++ b/include/caliper/ConfigManager.h @@ -69,12 +69,13 @@ class ConfigManager public: - typedef std::map argmap_t; + typedef std::map argmap_t; typedef std::vector> arglist_t; /// \brief Manages the list of options given to a ConfigManager config controller. /// Internal use. - class Options { + class Options + { struct OptionsImpl; std::shared_ptr mP; @@ -129,8 +130,7 @@ class ConfigManager /// \param level The aggreatation level ("local" or "cross") /// \param in Base CalQL clauses as needed by the controller /// \return Complete CalQL query statement - std::string - build_query(const char* level, const std::map& in) const; + std::string build_query(const char* level, const std::map& in) const; friend class ConfigManager; }; @@ -166,21 +166,25 @@ class ConfigManager /// Options::update_channel_config(config_map_t&) const /// on the ChannelController's config map to apply options. /// \return A new ChannelController object. - typedef cali::ChannelController* (*CreateConfigFn)(const char* name, const config_map_t& initial_cfg, const Options& opts); + typedef cali::ChannelController* (*CreateConfigFn)( + const char* name, + const config_map_t& initial_cfg, + const Options& opts + ); /// \brief Callback function to implement custom options checking for a config spec. - typedef std::string (*CheckArgsFn)(const Options& opts); + typedef std::string (*CheckArgsFn)(const Options& opts); /// \brief Define a config spec with custom ChannelController creation /// and option checking functions struct ConfigInfo { /// \brief JSON config spec. \see ConfigManager::add_config_spec(const char*) - const char* spec; + const char* spec; /// \brief Optional custom ChannelController creation function, /// or \a nullptr to use the default. CreateConfigFn create; /// \brief Optional argument checking function, or \a nullptr /// to use the default. - CheckArgsFn check_args; + CheckArgsFn check_args; }; ConfigManager(); @@ -410,7 +414,7 @@ class ConfigManager std::string error_msg() const; typedef std::shared_ptr ChannelPtr; - typedef std::vector ChannelList; + typedef std::vector ChannelList; /// \brief Parse \a config_string and return the specified configuration /// channels @@ -431,16 +435,14 @@ class ConfigManager /// /// \return An STL container with C++ shared_ptr objects to the /// ChannelController instances created from the configuration strings. - ChannelList - get_all_channels() const; + ChannelList get_all_channels() const; /// \brief Return a channel controller instance for configuration \a name /// /// Returns a C++ shared pointer containing the channel controller instance /// with the given \a name, or an empty shared_ptr object when no such /// channel exists. - ChannelPtr - get_channel(const char* name) const; + ChannelPtr get_channel(const char* name) const; /// \brief Start all configured measurement channels, or re-start /// paused ones @@ -454,15 +456,13 @@ class ConfigManager /// for (auto& channel : channels) /// channel->start(); /// \endcode - void - start(); + void start(); /// \brief Pause all configured measurement channels /// /// Invokes the ChannelController::stop() method on all configuration /// channel controllers created by the ConfigManager. - void - stop(); + void stop(); /// \brief Flush all configured measurement channels /// @@ -473,8 +473,7 @@ class ConfigManager /// for (auto& channel : channels) /// channel->flush(); /// \endcode - void - flush(); + void flush(); /// \brief Check if the given config string is valid. /// @@ -483,8 +482,7 @@ class ConfigManager /// will be marked as errors. /// /// \return Error message, or empty string if input is valid. - std::string - check(const char* config_string, bool allow_extra_kv_pairs = false) const; + std::string check(const char* config_string, bool allow_extra_kv_pairs = false) const; /// \brief Return names of available config specs /// @@ -492,26 +490,21 @@ class ConfigManager /// are met in this %Caliper instance. /// /// \return Names of all available config specs for this ConfigManager. - std::vector - available_config_specs() const; + std::vector available_config_specs() const; /// \brief Return short description for the given config spec. - std::string - get_description_for_spec(const char* name) const; + std::string get_description_for_spec(const char* name) const; /// \brief Return description and options for the given config spec. - std::string - get_documentation_for_spec(const char* name) const; + std::string get_documentation_for_spec(const char* name) const; /// \brief Return names of global config specs. /// /// \deprecated Query specific ConfigManager object instead. - static std::vector - available_configs(); + static std::vector available_configs(); /// \brief Return descriptions for available global configs. - static std::vector - get_config_docstrings(); + static std::vector get_config_docstrings(); /// \brief Check if given config string is valid for global config specs. /// Deprecated. @@ -523,8 +516,7 @@ class ConfigManager /// \deprecated Create a ConfigManager object and use its check() method. /// /// \return Error message, or empty string if input is valid. - static std::string - check_config_string(const char* config_string, bool allow_extra_kv_pairs = false); + static std::string check_config_string(const char* config_string, bool allow_extra_kv_pairs = false); }; /// \brief Add a set of global ConfigManager configs diff --git a/include/caliper/RegionProfile.h b/include/caliper/RegionProfile.h index f01f5921e..ccfa28621 100644 --- a/include/caliper/RegionProfile.h +++ b/include/caliper/RegionProfile.h @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. /// \file RuntimeProfile.h @@ -20,26 +20,26 @@ namespace cali /// \brief Collect and return time profiles for annotated %Caliper regions /// in a C++ map /// -/// The RegionProfile class is a %Caliper controller that allows one to +/// The RegionProfile class is a %Caliper controller that allows one to /// collect and examine time spent in %Caliper-annotated code regions within /// an instrumented program. /// It can compute inclusive or exclusive profiles. Start/stop profiling with -/// the start() and stop() methods. Once started, time profiles can -/// be retrieved at any time with exclusive_region_times() or +/// the start() and stop() methods. Once started, time profiles can +/// be retrieved at any time with exclusive_region_times() or /// inclusive_region_times(). /// /// \example cali-regionprofile.cpp -/// The example shows how to use the RegionProfile controller to examine -/// inclusive and exclusive time profiles for annotated regions within a +/// The example shows how to use the RegionProfile controller to examine +/// inclusive and exclusive time profiles for annotated regions within a /// program. class RegionProfile : public ChannelController { struct RegionProfileImpl; std::shared_ptr mP; - + public: - /// \brief Create a RegionProfile controller object. + /// \brief Create a RegionProfile controller object. /// /// Note that profiling must be started explicitly with the start() method. RegionProfile(); @@ -47,33 +47,33 @@ class RegionProfile : public ChannelController virtual ~RegionProfile(); /// \brief A tuple containing the computed time profiles. - /// - /// The first member is a string -> double STL map that stores the times - /// per region. The map keys are the region names used in the annotations, + /// + /// The first member is a string -> double STL map that stores the times + /// per region. The map keys are the region names used in the annotations, /// e.g., "work" for \c CALI_MARK_BEGIN("work"). /// Note that nested regions with the same name can't be distinguished. /// - /// The second member stores the total time spent in the \em selected + /// The second member stores the total time spent in the \em selected /// region type, and the third member stores the total time spent /// profiling. - typedef std::tuple< std::map, double, double > region_profile_t; + typedef std::tuple, double, double> region_profile_t; /// \brief Return an exclusive time profile for annotated regions. - /// + /// /// Exclusive time is the time spent in within a begin/end region itself, /// excluding time spent in sub-regions that are nested within. /// /// Profiling must have been started with start(). - /// - /// By default, the result contains times for all region types with the + /// + /// By default, the result contains times for all region types with the /// \c CALI_ATTR_NESTED flag. Specifically, this includes regions marked /// with the %Caliper annotations macros such as \c CALI_MARK_BEGIN. /// With the optional \a region_type argument, profiles can be calculated /// for a specific region type (%Caliper attribute) only. In this case, /// the second value in the result tuple contains the total time spent in - /// regions with the selected type. + /// regions with the selected type. /// - /// As an example, the following code returns a profile for "function" + /// As an example, the following code returns a profile for "function" /// regions: /// /// \code @@ -86,39 +86,36 @@ class RegionProfile : public ChannelController /// double total_function_time; /// double total_profiling_time; /// - /// std::tie(function_times, total_function_time, total_profiling_time) = + /// std::tie(function_times, total_function_time, total_profiling_time) = /// rp.exclusive_region_times("function"); /// \endcode /// - /// \returns A region_profile_t tuple with exclusive time per region in - /// a region name -> time STL map, the total time for the selected - /// region time, and the total time spent profiling. + /// \returns A region_profile_t tuple with exclusive time per region in + /// a region name -> time STL map, the total time for the selected + /// region time, and the total time spent profiling. /// All time values are in seconds. /// /// \sa region_profile_t - region_profile_t - exclusive_region_times(const std::string& region_type = ""); + region_profile_t exclusive_region_times(const std::string& region_type = ""); /// \brief Return an inclusive time profile for annotated regions. - /// - /// Inclusive time is time spent within a begin/end region, + /// + /// Inclusive time is time spent within a begin/end region, /// including time spent in sub-regions that are nested within. /// /// Other than returning inclusive rather than exclusive times, this /// method works the same as exclusive_region_times(). /// - /// \returns A region_profile_t tuple with inclusive time per region in - /// a region name -> time STL map, the total time for the selected - /// region time, and the total time spent profiling. + /// \returns A region_profile_t tuple with inclusive time per region in + /// a region name -> time STL map, the total time for the selected + /// region time, and the total time spent profiling. /// All time values are in seconds. /// /// \sa region_profile_t, exclusive_region_times() - region_profile_t - inclusive_region_times(const std::string& region_type = ""); + region_profile_t inclusive_region_times(const std::string& region_type = ""); /// \brief Reset the profiling database. - void - clear(); + void clear(); }; -} +} // namespace cali diff --git a/include/caliper/SnapshotRecord.h b/include/caliper/SnapshotRecord.h index 6b6d68687..3441789e1 100644 --- a/include/caliper/SnapshotRecord.h +++ b/include/caliper/SnapshotRecord.h @@ -19,33 +19,33 @@ class CaliperMetadataAccessInterface; class SnapshotView { const Entry* m_data; - size_t m_len; + size_t m_len; public: - SnapshotView() - : m_data { nullptr }, m_len { 0 } - { } - SnapshotView(size_t len, const Entry* data) - : m_data { data }, m_len { len } - { } - SnapshotView(const Entry& e) - : m_data { &e }, m_len { 1 } - { } - - using iterator = Entry*; + SnapshotView() : m_data { nullptr }, m_len { 0 } {} + + SnapshotView(size_t len, const Entry* data) : m_data { data }, m_len { len } {} + + SnapshotView(const Entry& e) : m_data { &e }, m_len { 1 } {} + + using iterator = Entry*; using const_iterator = const Entry*; - const_iterator begin() const { return m_data; } - const_iterator end() const { return m_data+m_len; } + const_iterator begin() const { return m_data; } - size_t size() const { return m_len; } - const Entry* data() const { return m_data; } - bool empty() const { return m_len == 0; } + const_iterator end() const { return m_data + m_len; } - const Entry& operator[](std::size_t n) const { return m_data[n]; } + size_t size() const { return m_len; } - Entry get(const Attribute& attr) const { + const Entry* data() const { return m_data; } + + bool empty() const { return m_len == 0; } + + const Entry& operator[] (std::size_t n) const { return m_data[n]; } + + Entry get(const Attribute& attr) const + { for (const Entry& e : *this) { Entry ret = e.get(attr); if (!ret.empty()) @@ -55,7 +55,8 @@ class SnapshotView return Entry(); } - Entry get_immediate_entry(const Attribute& attr) const { + Entry get_immediate_entry(const Attribute& attr) const + { cali_id_t attr_id = attr.id(); for (const Entry& e : *this) { if (e.node() && e.node()->id() == attr_id) @@ -76,72 +77,66 @@ class SnapshotBuilder public: - SnapshotBuilder() - : m_data { nullptr}, m_capacity { 0 }, m_len { 0 }, m_skipped { 0 } - { } + SnapshotBuilder() : m_data { nullptr }, m_capacity { 0 }, m_len { 0 }, m_skipped { 0 } {} + SnapshotBuilder(size_t capacity, Entry* data) : m_data { data }, m_capacity { capacity }, m_len { 0 }, m_skipped { 0 } - { } + {} - SnapshotBuilder(SnapshotBuilder&&) = default; + SnapshotBuilder(SnapshotBuilder&&) = default; SnapshotBuilder(const SnapshotBuilder&) = delete; - SnapshotBuilder& operator = (SnapshotBuilder&&) = default; - SnapshotBuilder& operator = (const SnapshotBuilder&) = delete; + SnapshotBuilder& operator= (SnapshotBuilder&&) = default; + SnapshotBuilder& operator= (const SnapshotBuilder&) = delete; size_t capacity() const { return m_capacity; } - size_t size() const { return m_len; } - size_t skipped() const { return m_skipped; } - void append(const Entry& e) { + size_t size() const { return m_len; } + + size_t skipped() const { return m_skipped; } + + void append(const Entry& e) + { if (m_len < m_capacity) m_data[m_len++] = e; else ++m_skipped; } - void append(size_t n, const Entry* entries) { + void append(size_t n, const Entry* entries) + { size_t num_copied = std::min(n, m_capacity - m_len); - std::copy_n(entries, num_copied, m_data+m_len); + std::copy_n(entries, num_copied, m_data + m_len); m_len += num_copied; - m_skipped += n-num_copied; + m_skipped += n - num_copied; } - void append(const Attribute& attr, const Variant& val) { - append(Entry(attr, val)); - } + void append(const Attribute& attr, const Variant& val) { append(Entry(attr, val)); } - void append(SnapshotView view) { - append(view.size(), view.data()); - } + void append(SnapshotView view) { append(view.size(), view.data()); } - SnapshotView view() const { - return SnapshotView { m_len, m_data }; - } + SnapshotView view() const { return SnapshotView { m_len, m_data }; } }; /// \brief A fixed-size snapshot record template class FixedSizeSnapshotRecord { - Entry m_data[N]; + Entry m_data[N]; SnapshotBuilder m_builder; public: - FixedSizeSnapshotRecord() - : m_builder { N, m_data } - { } + FixedSizeSnapshotRecord() : m_builder { N, m_data } {} - FixedSizeSnapshotRecord(const FixedSizeSnapshotRecord&) = delete; - FixedSizeSnapshotRecord operator = (const FixedSizeSnapshotRecord&) = delete; + FixedSizeSnapshotRecord(const FixedSizeSnapshotRecord&) = delete; + FixedSizeSnapshotRecord operator= (const FixedSizeSnapshotRecord&) = delete; SnapshotBuilder& builder() { return m_builder; } - SnapshotView view() const { return m_builder.view(); } - void reset() { - m_builder = SnapshotBuilder(N, m_data); - } + SnapshotView view() const { return m_builder.view(); } + + void reset() { m_builder = SnapshotBuilder(N, m_data); } }; } // namespace cali diff --git a/include/caliper/cali-manager.h b/include/caliper/cali-manager.h index 91c68980e..68cd209ae 100644 --- a/include/caliper/cali-manager.h +++ b/include/caliper/cali-manager.h @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #pragma once @@ -9,7 +9,8 @@ #include "ConfigManager.h" #include "ChannelController.h" -extern "C" { +extern "C" +{ #endif #ifdef __cplusplus diff --git a/include/caliper/cali-mpi.h b/include/caliper/cali-mpi.h index b32bb9512..da30e022b 100644 --- a/include/caliper/cali-mpi.h +++ b/include/caliper/cali-mpi.h @@ -49,21 +49,22 @@ struct QuerySpec; * \ingroup ReaderAPI */ -void -aggregate_over_mpi(CaliperMetadataDB& db, Aggregator& a, MPI_Comm comm); - -void -collective_flush(OutputStream& stream, - Caliper& c, - Channel& channel, - SnapshotView flush_info, - const QuerySpec& local_query, - const QuerySpec& cross_query, - MPI_Comm comm); +void aggregate_over_mpi(CaliperMetadataDB& db, Aggregator& a, MPI_Comm comm); + +void collective_flush( + OutputStream& stream, + Caliper& c, + Channel& channel, + SnapshotView flush_info, + const QuerySpec& local_query, + const QuerySpec& cross_query, + MPI_Comm comm +); } /* namespace cali */ -extern "C" { +extern "C" +{ #endif /* @@ -84,8 +85,7 @@ extern "C" { * and makes the mpi-specific services visible to the Caliper initializer. */ -void -cali_mpi_init(); +void cali_mpi_init(); #ifdef __cplusplus } /* extern "C" */ diff --git a/include/caliper/cali.h b/include/caliper/cali.h index 9db883f5f..a18281e05 100644 --- a/include/caliper/cali.h +++ b/include/caliper/cali.h @@ -18,7 +18,8 @@ #include /* size_t */ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* @@ -64,10 +65,7 @@ const char* cali_caliper_version(); * \param properties Attribute properties * \return Attribute id */ -cali_id_t -cali_create_attribute(const char* name, - cali_attr_type type, - int properties); +cali_id_t cali_create_attribute(const char* name, cali_attr_type type, int properties); /** * \brief Create an attribute key with additional metadata. @@ -84,13 +82,14 @@ cali_create_attribute(const char* name, * \sa cali_create_attribute */ -cali_id_t -cali_create_attribute_with_metadata(const char* name, - cali_attr_type type, - int properties, - int n, - const cali_id_t meta_attr_list[], - const cali_variant_t meta_val_list[]); +cali_id_t cali_create_attribute_with_metadata( + const char* name, + cali_attr_type type, + int properties, + int n, + const cali_id_t meta_attr_list[], + const cali_variant_t meta_val_list[] +); /** * \brief Find attribute by name @@ -98,32 +97,28 @@ cali_create_attribute_with_metadata(const char* name, * \return Attribute ID, or CALI_INV_ID if attribute was not found */ -cali_id_t -cali_find_attribute (const char* name); +cali_id_t cali_find_attribute(const char* name); /** * \brief Return name of attribute with given ID * \param attr_id Attribute id * \return Attribute name, or NULL if `attr_id` is not a valid attribute ID */ -const char* -cali_attribute_name(cali_id_t attr_id); +const char* cali_attribute_name(cali_id_t attr_id); /** * \brief Return the type of the attribute with given ID * \param attr_id Attribute id * \return Attribute type, or CALI_TYPE_INV if `attr_id` is not a valid attribute ID */ -cali_attr_type -cali_attribute_type(cali_id_t attr_id); +cali_attr_type cali_attribute_type(cali_id_t attr_id); /** * \brief Return attribute property flags of the attribute with given ID * \param attr_id Attribute id * \return Attribute properties, OR'ed */ -int -cali_attribute_properties(cali_id_t attr_id); +int cali_attribute_properties(cali_id_t attr_id); /** * \} @@ -150,10 +145,12 @@ cali_attribute_properties(cali_id_t attr_id); * \param trigger_info_attr_list Attribute IDs of event info entries * \param trigger_info_val_list Values of event info entries */ -void -cali_push_snapshot(int scope, int n, - const cali_id_t trigger_info_attr_list[], - const cali_variant_t trigger_info_val_list[]); +void cali_push_snapshot( + int scope, + int n, + const cali_id_t trigger_info_attr_list[], + const cali_variant_t trigger_info_val_list[] +); /** * \brief Take a snapshot on the given channel and push it into its @@ -169,11 +166,13 @@ cali_push_snapshot(int scope, int n, * \param trigger_info_attr_list Attribute IDs of event info entries * \param trigger_info_val_list Values of event info entries */ -void -cali_channel_push_snapshot(cali_id_t chn_id, - int scope, int n, - const cali_id_t trigger_info_attr_list[], - const cali_variant_t trigger_info_val_list[]); +void cali_channel_push_snapshot( + cali_id_t chn_id, + int scope, + int n, + const cali_id_t trigger_info_attr_list[], + const cali_variant_t trigger_info_val_list[] +); /** * \brief Take a snapshot on the given channel @@ -199,8 +198,7 @@ cali_channel_push_snapshot(cali_id_t chn_id, * not all of the snapshot was returned. * If this is zero, no snapshot was taken. */ -size_t -cali_channel_pull_snapshot(cali_id_t chn_id, int scope, size_t len, unsigned char* buf); +size_t cali_channel_pull_snapshot(cali_id_t chn_id, int scope, size_t len, unsigned char* buf); /** * \} @@ -235,11 +233,7 @@ cali_channel_pull_snapshot(cali_id_t chn_id, int scope, size_t len, unsigned cha * * \sa cali_pull_snapshot, cali_entry_proc_fn */ -void -cali_unpack_snapshot(const unsigned char* buf, - size_t* bytes_read, - cali_entry_proc_fn proc_fn, - void* user_arg); +void cali_unpack_snapshot(const unsigned char* buf, size_t* bytes_read, cali_entry_proc_fn proc_fn, void* user_arg); /** * Return top-most value for attribute ID \a attr_id from snapshot \a buf. @@ -255,10 +249,7 @@ cali_unpack_snapshot(const unsigned char* buf, * \return The top-most stacked value for the given attribute ID, or an empty * variant if none was found */ -cali_variant_t -cali_find_first_in_snapshot(const unsigned char* buf, - cali_id_t attr_id, - size_t* bytes_read); +cali_variant_t cali_find_first_in_snapshot(const unsigned char* buf, cali_id_t attr_id, size_t* bytes_read); /** * Run all entries with attribute `attr_id` in a snapshot that was previously @@ -273,12 +264,13 @@ cali_find_first_in_snapshot(const unsigned char* buf, * \param proc_fn Callback function to process individidual entries * \param userdata User-defined parameter passed to `proc_fn` */ -void -cali_find_all_in_snapshot(const unsigned char* buf, - cali_id_t attr_id, - size_t* bytes_read, - cali_entry_proc_fn proc_fn, - void* userdata); +void cali_find_all_in_snapshot( + const unsigned char* buf, + cali_id_t attr_id, + size_t* bytes_read, + cali_entry_proc_fn proc_fn, + void* userdata +); /** * \} @@ -303,8 +295,7 @@ cali_find_all_in_snapshot(const unsigned char* buf, * \return The top-most stacked value on the blackboard for the given * attribute ID, or an empty variant if it was not found */ -cali_variant_t -cali_get(cali_id_t attr_id); +cali_variant_t cali_get(cali_id_t attr_id); /** * \brief Return top-most value for attribute \a attr_id from the @@ -317,8 +308,7 @@ cali_get(cali_id_t attr_id); * \return The top-most stacked value on the blackboard for the given * attribute ID, or an empty variant if it was not found */ -cali_variant_t -cali_channel_get(cali_id_t chn_id, cali_id_t attr_id); +cali_variant_t cali_channel_get(cali_id_t chn_id, cali_id_t attr_id); /** * \brief Return the innermost current %Caliper region on the calling thread. @@ -332,8 +322,7 @@ cali_channel_get(cali_id_t chn_id, cali_id_t attr_id); * \a alt if no region has been set or the innermost path region is * not a string attribute. */ -const char* -cali_get_current_region_or(const char* alt); +const char* cali_get_current_region_or(const char* alt); /** * \} @@ -358,8 +347,7 @@ cali_get_current_region_or(const char* alt); * * \see cali_end_region() */ -void -cali_begin_region(const char* name); +void cali_begin_region(const char* name); /** * \brief End nested region \a name @@ -370,8 +358,7 @@ cali_begin_region(const char* name); * * \see cali_begin_region() */ -void -cali_end_region(const char* name); +void cali_end_region(const char* name); /** * \brief Begin phase region \a name @@ -381,14 +368,12 @@ cali_end_region(const char* name); * use the "phase" attribute with annotation level 4. Otherwise phases behave * identical to regular Caliper regions. */ -void -cali_begin_phase(const char* name); +void cali_begin_phase(const char* name); /** * \brief End phase region \a name */ -void -cali_end_phase(const char* name); +void cali_end_phase(const char* name); /** * \brief Begin communication region \a name @@ -398,57 +383,44 @@ cali_end_phase(const char* name); * to summarize communication pattern statistics. Otherwise they behave * identical to regular Caliper regions. */ -void -cali_begin_comm_region(const char* name); +void cali_begin_comm_region(const char* name); /** * \brief End phase region \a name */ -void -cali_end_comm_region(const char* name); +void cali_end_comm_region(const char* name); /** * \brief Begin region where the value for \a attr is `true` on the blackboard. */ -void -cali_begin(cali_id_t attr); +void cali_begin(cali_id_t attr); /** * \brief Begin region \a val for attribute \a attr on the blackboard. * * The region may be nested. */ -void -cali_begin_double(cali_id_t attr, double val); +void cali_begin_double(cali_id_t attr, double val); /** \copydoc cali_begin_double() */ -void -cali_begin_int(cali_id_t attr, int val); +void cali_begin_int(cali_id_t attr, int val); /** \copydoc cali_begin_double() */ -void -cali_begin_string(cali_id_t attr, const char* val); +void cali_begin_string(cali_id_t attr, const char* val); /** * End innermost open region for \a attr on the blackboard. */ -void -cali_end (cali_id_t attr); +void cali_end(cali_id_t attr); /** * \brief Set value for attribute \a attr to \a val on the blackboard. */ -void -cali_set (cali_id_t attr, - const void* value, - size_t size); +void cali_set(cali_id_t attr, const void* value, size_t size); /** \copybrief cali_set() */ -void -cali_set_double(cali_id_t attr, double val); +void cali_set_double(cali_id_t attr, double val); /** \copybrief cali_set() */ -void -cali_set_int(cali_id_t attr, int val); +void cali_set_int(cali_id_t attr, int val); /** \copybrief cali_set() */ -void -cali_set_string(cali_id_t attr, const char* val); +void cali_set_string(cali_id_t attr, const char* val); /** * \brief Begin region where the value for the attribute named \a attr_name @@ -456,56 +428,44 @@ cali_set_string(cali_id_t attr, const char* val); * * \deprecated Use cali_begin_region() */ -void -cali_begin_byname(const char* attr_name); +void cali_begin_byname(const char* attr_name); /** * \brief Begin region \a val for attribute named \a attr_name on the blackboard. * * The region may be nested. */ -void -cali_begin_double_byname(const char* attr_name, double val); +void cali_begin_double_byname(const char* attr_name, double val); /** \copydoc cali_begin_double_byname() */ -void -cali_begin_int_byname(const char* attr_name, int val); +void cali_begin_int_byname(const char* attr_name, int val); /** \copydoc cali_begin_double_byname() */ -void -cali_begin_string_byname(const char* attr_name, const char* val); +void cali_begin_string_byname(const char* attr_name, const char* val); /** * \brief Set value for attribute named \a attr_name to \a val on the blackboard. */ -void -cali_set_double_byname(const char* attr_name, double val); +void cali_set_double_byname(const char* attr_name, double val); /** \copydoc cali_set_double_byname() */ -void -cali_set_int_byname(const char* attr_name, int val); +void cali_set_int_byname(const char* attr_name, int val); /** \copydoc cali_set_double_byname() */ -void -cali_set_string_byname(const char* attr_name, const char* val); +void cali_set_string_byname(const char* attr_name, const char* val); /** * \brief End innermost open region for attribute named \a attr_name on the * blackboard. */ -void -cali_end_byname(const char* attr_name); +void cali_end_byname(const char* attr_name); /** * \brief Set a global attribute with name \a attr_name to \a val. */ -void -cali_set_global_double_byname(const char* attr_name, double val); +void cali_set_global_double_byname(const char* attr_name, double val); /** \copydoc cali_set_global_double_byname() */ -void -cali_set_global_int_byname(const char* attr_name, int val); +void cali_set_global_int_byname(const char* attr_name, int val); /** \copydoc cali_set_global_double_byname() */ -void -cali_set_global_string_byname(const char* attr_name, const char* val); +void cali_set_global_string_byname(const char* attr_name, const char* val); /** \copydoc cali_set_global_double_byname() */ -void -cali_set_global_uint_byname(const char* attr_name, uint64_t val); +void cali_set_global_uint_byname(const char* attr_name, uint64_t val); /** * \} @@ -526,23 +486,20 @@ cali_set_global_uint_byname(const char* attr_name, uint64_t val); * * The entry can still be overwritten by environment variables. */ -void -cali_config_preset(const char* key, const char* value); +void cali_config_preset(const char* key, const char* value); /** * \brief Set a config entry in the default configset. * * This entry will not be overwritten by environment variables. */ -void -cali_config_set(const char* key, const char* value); +void cali_config_set(const char* key, const char* value); /** * \brief Enable or disable reading environment variables for the default * configset. */ -void -cali_config_allow_read_env(int allow); +void cali_config_allow_read_env(int allow); struct _cali_configset_t; typedef struct _cali_configset_t* cali_configset_t; @@ -562,19 +519,16 @@ typedef struct _cali_configset_t* cali_configset_t; * NULL entries: { NULL, NULL } . * \return The config set. */ -cali_configset_t -cali_create_configset(const char* keyvallist[][2]); +cali_configset_t cali_create_configset(const char* keyvallist[][2]); /** \brief Delete a config set created with cali_create_configset(). */ -void -cali_delete_configset(cali_configset_t cfg); +void cali_delete_configset(cali_configset_t cfg); /** \brief Modify a config set created with cali_create_configset(). * * Sets or overwrites the value for \a key with \a value. */ -void -cali_configset_set(cali_configset_t cfg, const char* key, const char* value); +void cali_configset_set(cali_configset_t cfg, const char* key, const char* value); /** * \} @@ -638,8 +592,7 @@ cali_configset_set(cali_configset_t cfg, const char* key, const char* value); * * \return ID of the created channel. */ -cali_id_t -cali_create_channel(const char* name, int flags, const cali_configset_t cfg); +cali_id_t cali_create_channel(const char* name, int flags, const cali_configset_t cfg); /** * \brief Delete a channel. Frees associated resources, e.g. blackboards, @@ -650,16 +603,14 @@ cali_create_channel(const char* name, int flags, const cali_configset_t cfg); * * \param chn_id ID of the channel */ -void -cali_delete_channel(cali_id_t chn_id); +void cali_delete_channel(cali_id_t chn_id); /** * \brief Activate the (inactive) channel with the given ID. * * Only active channels will process annotations and other events. */ -void -cali_activate_channel(cali_id_t chn_id); +void cali_activate_channel(cali_id_t chn_id); /** * \brief Deactivate the channel with the given ID. @@ -669,15 +620,13 @@ cali_activate_channel(cali_id_t chn_id); * * \sa cali_channel_activate */ -void -cali_deactivate_channel(cali_id_t chn_id); +void cali_deactivate_channel(cali_id_t chn_id); /** * \brief Returns a non-zero value if the channel with the given ID * is active, otherwise 0. */ -int -cali_channel_is_active(cali_id_t chn_id); +int cali_channel_is_active(cali_id_t chn_id); /** * \} @@ -712,8 +661,7 @@ cali_channel_is_active(cali_id_t chn_id); * \param flush_opts Flush options as bitwise-OR of cali_flush_opt * flags. Use 0 for default behavior. */ -void -cali_flush(int flush_opts); +void cali_flush(int flush_opts); /** * \brief Forward aggregation or trace buffer contents to output services @@ -731,8 +679,7 @@ cali_flush(int flush_opts); * \param flush_opts Flush options as bitwise-OR of cali_flush_opt flags. * Use 0 for default behavior. */ -void -cali_channel_flush(cali_id_t chn_id, int flush_opts); +void cali_channel_flush(cali_id_t chn_id, int flush_opts); /** * \} @@ -753,15 +700,13 @@ cali_channel_flush(cali_id_t chn_id, int flush_opts); * It can also be used to avoid high initialization costs in the first * Caliper API call. */ -void -cali_init(); +void cali_init(); /** * \brief Check if Caliper is initialized on this process. * \return A non-zero value if Caliper is initialized, 0 if it is not initialized. */ -int -cali_is_initialized(); +int cali_is_initialized(); /* * --- Macro annotation helper functions @@ -771,8 +716,7 @@ cali_is_initialized(); * \brief Create a loop iteration attribute for CALI_MARK_LOOP_BEGIN. * \param name User-defined name of the loop */ -cali_id_t -cali_make_loop_iteration_attribute(const char* name); +cali_id_t cali_make_loop_iteration_attribute(const char* name); #ifdef __cplusplus } // extern "C" @@ -830,8 +774,7 @@ typedef std::map config_map_t; * * \return ID of the created channel. */ -cali_id_t -create_channel(const char* name, int flags, const config_map_t& cfg); +cali_id_t create_channel(const char* name, int flags, const config_map_t& cfg); /** * \brief Run a CalQL query on channel \a channel_id and write its output into @@ -869,8 +812,7 @@ create_channel(const char* name, int flags, const config_map_t& cfg); * Use 0 for default behavior. * \param os A C++ ostream to write the query output to. */ -void -write_report_for_query(cali_id_t channel_id, const char* query, int flush_opts, std::ostream& os); +void write_report_for_query(cali_id_t channel_id, const char* query, int flush_opts, std::ostream& os); } // namespace cali diff --git a/include/caliper/cali_datatracker.h b/include/caliper/cali_datatracker.h index 2515b5982..6d4910457 100644 --- a/include/caliper/cali_datatracker.h +++ b/include/caliper/cali_datatracker.h @@ -13,7 +13,8 @@ #include /* size_t */ #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /* @@ -31,10 +32,7 @@ extern "C" { * \param size Size of the allocation */ -void -cali_datatracker_track(const void *ptr, - const char *label, - size_t size); +void cali_datatracker_track(const void* ptr, const char* label, size_t size); /** * Track an existing allocation in Caliper. @@ -46,12 +44,13 @@ cali_datatracker_track(const void *ptr, * \param num_dimensions The number of dimensions in `dimensions` */ -void -cali_datatracker_track_dimensional(const void *ptr, - const char *label, - size_t elem_size, - const size_t *dimensions, - size_t num_dimensions); +void cali_datatracker_track_dimensional( + const void* ptr, + const char* label, + size_t elem_size, + const size_t* dimensions, + size_t num_dimensions +); /** * Untrack a previously tracked allocation in Caliper. @@ -59,8 +58,7 @@ cali_datatracker_track_dimensional(const void *ptr, * \param ptr The pointer to the beginning of the allocation to untrack */ -void -cali_datatracker_untrack(const void *ptr); +void cali_datatracker_untrack(const void* ptr); /** * \} @@ -83,8 +81,7 @@ cali_datatracker_untrack(const void *ptr); * and resolved with the alloc service. */ -#define CALI_DATATRACKER_TRACK(ptr, size) \ - cali_datatracker_track(ptr, #ptr, size) +#define CALI_DATATRACKER_TRACK(ptr, size) cali_datatracker_track(ptr, #ptr, size) /** * \brief Label and track a multi-dimensional array. @@ -105,8 +102,7 @@ cali_datatracker_untrack(const void *ptr); * \brief Stop tracking the memory region pointed to by \a ptr */ -#define CALI_DATATRACKER_UNTRACK(ptr) \ - cali_datatracker_untrack(ptr) +#define CALI_DATATRACKER_UNTRACK(ptr) cali_datatracker_untrack(ptr) /** * \} diff --git a/include/caliper/cali_definitions.h b/include/caliper/cali_definitions.h index fab5d79a8..1bf968ba7 100644 --- a/include/caliper/cali_definitions.h +++ b/include/caliper/cali_definitions.h @@ -11,14 +11,15 @@ #define CALI_CALI_DEFINITIONS_H #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif typedef enum { - CALI_SCOPE_PROCESS = 1, - CALI_SCOPE_THREAD = 2, - CALI_SCOPE_TASK = 4, - CALI_SCOPE_CHANNEL = 8 + CALI_SCOPE_PROCESS = 1, + CALI_SCOPE_THREAD = 2, + CALI_SCOPE_TASK = 4, + CALI_SCOPE_CHANNEL = 8 } cali_context_scope_t; /** @@ -28,10 +29,10 @@ typedef enum { /** Clear trace and aggregation buffers after flush. */ CALI_FLUSH_CLEAR_BUFFERS = 1 } cali_flush_opt; - + /** * Channel creation options - */ + */ typedef enum { /** Leave the channel inactive after it is created. */ CALI_CHANNEL_LEAVE_INACTIVE = 1, diff --git a/include/caliper/cali_macros.h b/include/caliper/cali_macros.h index a7b103c85..2416ad355 100644 --- a/include/caliper/cali_macros.h +++ b/include/caliper/cali_macros.h @@ -30,8 +30,7 @@ /// function, and will automatically "close" the function at any return /// point. Will export the annotated function by name in the pre-defined /// `function` attribute. Only available in C++. -#define CALI_CXX_MARK_FUNCTION \ - cali::Function CALI_CREATE_VAR_NAME(__cali_ann, __func__)(__func__) +#define CALI_CXX_MARK_FUNCTION cali::Function CALI_CREATE_VAR_NAME(__cali_ann, __func__)(__func__) /// \brief C++ macro marking a scoped region /// @@ -39,18 +38,15 @@ /// scope, and will automatically "close" the function at any return /// point. Will export the annotated function by name in the pre-defined /// `annotation` attribute. Only available in C++. -#define CALI_CXX_MARK_SCOPE(name) \ - cali::ScopeAnnotation CALI_CREATE_VAR_NAME(__cali_ann_scope, __LINE__)(name) +#define CALI_CXX_MARK_SCOPE(name) cali::ScopeAnnotation CALI_CREATE_VAR_NAME(__cali_ann_scope, __LINE__)(name) /// \brief Mark loop in C++ /// \copydetails CALI_MARK_LOOP_BEGIN -#define CALI_CXX_MARK_LOOP_BEGIN(loop_id, name) \ - cali::Loop __cali_loop_##loop_id(name) +#define CALI_CXX_MARK_LOOP_BEGIN(loop_id, name) cali::Loop __cali_loop_##loop_id(name) /// \brief Mark loop end in C++ /// \copydetails CALI_MARK_LOOP_END -#define CALI_CXX_MARK_LOOP_END(loop_id) \ - __cali_loop_##loop_id.end() +#define CALI_CXX_MARK_LOOP_END(loop_id) __cali_loop_##loop_id.end() /// \brief C++ macro for a loop iteration /// @@ -74,7 +70,7 @@ /// \param loop_id The loop identifier given to \ref CALI_CXX_MARK_LOOP_BEGIN /// \param iter The iteration number. Must be convertible to \c int. #define CALI_CXX_MARK_LOOP_ITERATION(loop_id, iter) \ - cali::Loop::Iteration __cali_iter_##loop_id ( __cali_loop_##loop_id.iteration(static_cast(iter)) ) + cali::Loop::Iteration __cali_iter_##loop_id(__cali_loop_##loop_id.iteration(static_cast(iter))) #endif // __cplusplus @@ -88,16 +84,14 @@ extern cali_id_t cali_annotation_attr_id; /// placed at \e all function exit points. For C++, we recommend using /// \ref CALI_CXX_MARK_FUNCTION instead. /// \sa CALI_MARK_FUNCTION_END, CALI_CXX_MARK_FUNCTION -#define CALI_MARK_FUNCTION_BEGIN \ - cali_begin_region(__func__) +#define CALI_MARK_FUNCTION_BEGIN cali_begin_region(__func__) /// \brief Mark end of a function. /// /// Must be placed at \e all exit points of a function marked with /// \ref CALI_MARK_FUNCTION_BEGIN. /// \sa CALI_MARK_FUNCTION_BEGIN -#define CALI_MARK_FUNCTION_END \ - cali_end_region(__func__) +#define CALI_MARK_FUNCTION_END cali_end_region(__func__) /// \brief Mark a loop /// @@ -109,12 +103,11 @@ extern cali_id_t cali_annotation_attr_id; /// \param loop_id A loop identifier. Needed to refer to the loop /// from the \a iteration and \a end annotations. /// \param name Name of the loop. -#define CALI_MARK_LOOP_BEGIN(loop_id, name) \ - if (cali_loop_attr_id == CALI_INV_ID) \ - cali_init(); \ - cali_begin_string(cali_loop_attr_id, (name)); \ - cali_id_t __cali_iter_##loop_id = \ - cali_make_loop_iteration_attribute(name) +#define CALI_MARK_LOOP_BEGIN(loop_id, name) \ + if (cali_loop_attr_id == CALI_INV_ID) \ + cali_init(); \ + cali_begin_string(cali_loop_attr_id, (name)); \ + cali_id_t __cali_iter_##loop_id = cali_make_loop_iteration_attribute(name) /// \brief Mark a loop /// @@ -128,8 +121,7 @@ extern cali_id_t cali_annotation_attr_id; /// as well. /// /// \param loop_id The loop identifier given to \ref CALI_MARK_LOOP_BEGIN. -#define CALI_MARK_LOOP_END(loop_id) \ - cali_end(cali_loop_attr_id) +#define CALI_MARK_LOOP_END(loop_id) cali_end(cali_loop_attr_id) /// \brief Mark begin of a loop iteration. /// @@ -145,8 +137,7 @@ extern cali_id_t cali_annotation_attr_id; /// convertible to \c int. /// \sa CALI_MARK_ITERATION_END, CALI_MARK_LOOP_BEGIN, /// CALI_CXX_MARK_LOOP_ITERATION -#define CALI_MARK_ITERATION_BEGIN(loop_id, iter) \ - cali_begin_int( __cali_iter_##loop_id, ((int) (iter))) +#define CALI_MARK_ITERATION_BEGIN(loop_id, iter) cali_begin_int(__cali_iter_##loop_id, ((int) (iter))) /// \brief Mark end of a loop iteration. /// @@ -157,8 +148,7 @@ extern cali_id_t cali_annotation_attr_id; /// /// \param loop_id Loop identifier given in \ref CALI_MARK_LOOP_BEGIN. /// \sa CALI_MARK_ITERATION_BEGIN -#define CALI_MARK_ITERATION_END(loop_id) \ - cali_end( __cali_iter_##loop_id ) +#define CALI_MARK_ITERATION_END(loop_id) cali_end(__cali_iter_##loop_id) /// \brief Wrap Caliper annotations around a C/C++ statement. /// @@ -178,9 +168,9 @@ extern cali_id_t cali_annotation_attr_id; /// cannot branch out of the macro (e.g. with \c goto, \c continue, /// \c break, or \c return). /// -#define CALI_WRAP_STATEMENT(name, statement) \ - cali_begin_region((name)); \ - statement; \ +#define CALI_WRAP_STATEMENT(name, statement) \ + cali_begin_region((name)); \ + statement; \ cali_end_region((name)) /// \brief Mark begin of a user-defined code region. @@ -196,8 +186,7 @@ extern cali_id_t cali_annotation_attr_id; /// /// \param name The region name. Must be convertible to `const char*`. /// \sa CALI_MARK_END -#define CALI_MARK_BEGIN(name) \ - cali_begin_region(name) +#define CALI_MARK_BEGIN(name) cali_begin_region(name) /// \brief Mark end of a user-defined code region. /// @@ -208,8 +197,7 @@ extern cali_id_t cali_annotation_attr_id; /// The macro will check if the name matches, and report an error /// if it doesn't. /// \sa CALI_MARK_BEGIN -#define CALI_MARK_END(name) \ - cali_end_region(name) +#define CALI_MARK_END(name) cali_end_region(name) /// \brief Mark begin of a phase region /// @@ -217,23 +205,19 @@ extern cali_id_t cali_annotation_attr_id; /// regions use the "region" attribute with annotation level 0, phase regions /// use the "phase" attribute with annotation level 4. Otherwise phases behave /// identical to regular Caliper regions. -#define CALI_MARK_PHASE_BEGIN(name) \ - cali_begin_phase(name) +#define CALI_MARK_PHASE_BEGIN(name) cali_begin_phase(name) /// \brief Mark end of a phase region -#define CALI_MARK_PHASE_END(name) \ - cali_end_phase(name) +#define CALI_MARK_PHASE_END(name) cali_end_phase(name) /// \brief Mark begin of a communication region /// /// A communication region marks communication operations (e.g., MPI calls) /// that belong to a single communication pattern. -#define CALI_MARK_COMM_REGION_BEGIN(name) \ - cali_begin_comm_region(name) +#define CALI_MARK_COMM_REGION_BEGIN(name) cali_begin_comm_region(name) /// \brief Mark end of a communication region -#define CALI_MARK_COMM_REGION_END(name) \ - cali_end_comm_region(name) +#define CALI_MARK_COMM_REGION_END(name) cali_end_comm_region(name) /** * \} (group) */ diff --git a/include/caliper/common/Attribute.h b/include/caliper/common/Attribute.h index d75e97338..62e8c517e 100644 --- a/include/caliper/common/Attribute.h +++ b/include/caliper/common/Attribute.h @@ -33,88 +33,75 @@ class Attribute public: - constexpr static cali_id_t NAME_ATTR_ID = 8; - constexpr static cali_id_t TYPE_ATTR_ID = 9; + constexpr static cali_id_t NAME_ATTR_ID = 8; + constexpr static cali_id_t TYPE_ATTR_ID = 9; constexpr static cali_id_t PROP_ATTR_ID = 10; - constexpr Attribute() - : m_node(nullptr) - { } + constexpr Attribute() : m_node(nullptr) {} - operator bool() const { return m_node != nullptr; } + operator bool () const { return m_node != nullptr; } - cali_id_t id() const { return m_node ? m_node->id() : CALI_INV_ID; } + cali_id_t id() const { return m_node ? m_node->id() : CALI_INV_ID; } - std::string name() const; - const char* name_c_str() const; + std::string name() const; + const char* name_c_str() const; cali_attr_type type() const; - int properties() const; + int properties() const; /// \brief Return the context tree node pointer that represents /// this attribute key. - Node* node() const { - return m_node; - } - - Variant get(const Attribute& attr) const; - - bool store_as_value() const { - return properties() & CALI_ATTR_ASVALUE; - } - bool is_autocombineable() const { - return !store_as_value() && !(properties() & CALI_ATTR_NOMERGE); - } - bool skip_events() const { - return properties() & CALI_ATTR_SKIP_EVENTS; - } - bool is_hidden() const { - return properties() & CALI_ATTR_HIDDEN; - } - bool is_nested() const { - return properties() & CALI_ATTR_NESTED; - } - bool is_global() const { - return properties() & CALI_ATTR_GLOBAL; - } - int level() const { - return (properties() & CALI_ATTR_LEVEL_MASK) >> 16; - } + Node* node() const { return m_node; } + + Variant get(const Attribute& attr) const; + + bool store_as_value() const { return properties() & CALI_ATTR_ASVALUE; } + + bool is_autocombineable() const { return !store_as_value() && !(properties() & CALI_ATTR_NOMERGE); } + + bool skip_events() const { return properties() & CALI_ATTR_SKIP_EVENTS; } + + bool is_hidden() const { return properties() & CALI_ATTR_HIDDEN; } + + bool is_nested() const { return properties() & CALI_ATTR_NESTED; } + + bool is_global() const { return properties() & CALI_ATTR_GLOBAL; } + + int level() const { return (properties() & CALI_ATTR_LEVEL_MASK) >> 16; } static Attribute make_attribute(Node* node); - static bool is_attribute(const Node* node) { - return node && node->attribute() == NAME_ATTR_ID; - } + static bool is_attribute(const Node* node) { return node && node->attribute() == NAME_ATTR_ID; } private: Node* m_node; - Attribute(Node* node) - : m_node(node) - { } + Attribute(Node* node) : m_node(node) {} - friend bool operator < (const cali::Attribute& a, const cali::Attribute& b); - friend bool operator == (const cali::Attribute& a, const cali::Attribute& b); - friend bool operator != (const cali::Attribute& a, const cali::Attribute& b); + friend bool operator< (const cali::Attribute& a, const cali::Attribute& b); + friend bool operator== (const cali::Attribute& a, const cali::Attribute& b); + friend bool operator!= (const cali::Attribute& a, const cali::Attribute& b); }; -inline bool operator < (const cali::Attribute& a, const cali::Attribute& b) { +inline bool operator< (const cali::Attribute& a, const cali::Attribute& b) +{ return a.id() < b.id(); } -inline bool operator == (const cali::Attribute& a, const cali::Attribute& b) { +inline bool operator== (const cali::Attribute& a, const cali::Attribute& b) +{ // we don't have copies of nodes, so the ptr should be unique return a.m_node == b.m_node; } -inline bool operator != (const cali::Attribute& a, const cali::Attribute& b) { +inline bool operator!= (const cali::Attribute& a, const cali::Attribute& b) +{ return a.m_node != b.m_node; } -std::ostream& operator << (std::ostream&, const cali::Attribute&); +std::ostream& operator<< (std::ostream&, const cali::Attribute&); } // namespace cali diff --git a/include/caliper/common/CaliperMetadataAccessInterface.h b/include/caliper/common/CaliperMetadataAccessInterface.h index d57316041..b89f55661 100644 --- a/include/caliper/common/CaliperMetadataAccessInterface.h +++ b/include/caliper/common/CaliperMetadataAccessInterface.h @@ -25,46 +25,39 @@ class CaliperMetadataAccessInterface // --- query operations - virtual Node* - node(cali_id_t) const = 0; + virtual Node* node(cali_id_t) const = 0; - virtual Attribute - get_attribute(cali_id_t id) const = 0; - virtual Attribute - get_attribute(const std::string& str) const = 0; + virtual Attribute get_attribute(cali_id_t id) const = 0; + virtual Attribute get_attribute(const std::string& str) const = 0; /// \brief Return all attributes - virtual std::vector - get_all_attributes() const = 0; + virtual std::vector get_all_attributes() const = 0; /// \brief Return all attributes that have a metadata entry \a meta, of any value - std::vector - find_attributes_with(const Attribute& meta) const; + std::vector find_attributes_with(const Attribute& meta) const; /// \brief Return all attributes with the given property flag(s) - std::vector - find_attributes_with_prop(int prop) const; + std::vector find_attributes_with_prop(int prop) const; // --- modifying operations - virtual Attribute - create_attribute(const std::string& name, - cali_attr_type type, - int prop = CALI_ATTR_DEFAULT, - int meta = 0, - const Attribute* meta_attr = nullptr, - const Variant* meta_data = nullptr) = 0; + virtual Attribute create_attribute( + const std::string& name, + cali_attr_type type, + int prop = CALI_ATTR_DEFAULT, + int meta = 0, + const Attribute* meta_attr = nullptr, + const Variant* meta_data = nullptr + ) = 0; - virtual Node* - make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent = 0) = 0; + virtual Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent = 0) = 0; // --- globals /// \brief Return global entries (entries with the \a CALI_ATTR_GLOBAL flag set) - virtual std::vector - get_globals() = 0; + virtual std::vector get_globals() = 0; }; Entry get_path_entry(const CaliperMetadataAccessInterface& db, const Entry& e); -} +} // namespace cali diff --git a/include/caliper/common/Entry.h b/include/caliper/common/Entry.h index 628df2fee..dd4ac1c33 100644 --- a/include/caliper/common/Entry.h +++ b/include/caliper/common/Entry.h @@ -29,88 +29,72 @@ class CaliperMetadataAccessInterface; class Entry { - Node* m_node; // reference or attribute node + Node* m_node; // reference or attribute node Variant m_value; public: constexpr static size_t MAX_PACKED_SIZE = 30; - constexpr Entry() - : m_node(nullptr) - { } + constexpr Entry() : m_node(nullptr) {} - Entry(Node* node) - : m_node(node), m_value(node->data()) - { } + Entry(Node* node) : m_node(node), m_value(node->data()) {} - Entry(const Attribute& attr, const Variant& val) - : m_node(attr.node()), m_value(val) - { } + Entry(const Attribute& attr, const Variant& val) : m_node(attr.node()), m_value(val) {} /// \brief Return context tree node pointer for reference entries. /// \return The context tree node or attribute node - Node* node() const { - return m_node; - } + Node* node() const { return m_node; } /// \brief Return top-level attribute ID of this entry /// /// For immediate entries, returns the stored attribute id. /// For reference entries, returns the referenced node's /// attribute id. - cali_id_t attribute() const { + cali_id_t attribute() const + { return Attribute::is_attribute(m_node) ? m_node->id() : (m_node ? m_node->attribute() : CALI_INV_ID); } /// \brief Count instances of attribute \a attr_id in this entry - int count(cali_id_t attr_id = CALI_INV_ID) const; - int count(const Attribute& attr) const { - return count(attr.id()); - } + int count(cali_id_t attr_id = CALI_INV_ID) const; + + int count(const Attribute& attr) const { return count(attr.id()); } /// \brief Return top-level data value of this entry - Variant value() const { - return m_value; - } + Variant value() const { return m_value; } /// \brief Extract data value for attribute \a attr_id from this entry - Variant value(cali_id_t attr_id) const; - Variant value(const Attribute& attr) const { - return value(attr.id()); - } + Variant value(cali_id_t attr_id) const; + + Variant value(const Attribute& attr) const { return value(attr.id()); } /// \brief Find and return entry for the given attribute in this entries' /// value or path - Entry get(const Attribute& attr) const; + Entry get(const Attribute& attr) const; - bool empty() const { - return m_node == nullptr; - } - bool is_immediate() const { - return Attribute::is_attribute(m_node); - } - bool is_reference() const { - return !empty() && !is_immediate(); - } + bool empty() const { return m_node == nullptr; } + + bool is_immediate() const { return Attribute::is_attribute(m_node); } + + bool is_reference() const { return !empty() && !is_immediate(); } - size_t pack(unsigned char* buffer) const; + size_t pack(unsigned char* buffer) const; static Entry unpack(const CaliperMetadataAccessInterface& db, const unsigned char* buffer, size_t* inc); - friend bool operator == (const Entry&, const Entry&); + friend bool operator== (const Entry&, const Entry&); }; -inline bool operator == (const Entry& lhs, const Entry& rhs) +inline bool operator== (const Entry& lhs, const Entry& rhs) { - bool node_eq = lhs.m_node == rhs.m_node || - (lhs.m_node && rhs.m_node && lhs.m_node->id() == rhs.m_node->id()); - bool is_imm = lhs.is_immediate(); + bool node_eq = lhs.m_node == rhs.m_node || (lhs.m_node && rhs.m_node && lhs.m_node->id() == rhs.m_node->id()); + bool is_imm = lhs.is_immediate(); return node_eq && (!is_imm || (is_imm && lhs.m_value == rhs.m_value)); } -inline bool operator != (const Entry& lhs, const Entry& rhs) +inline bool operator!= (const Entry& lhs, const Entry& rhs) { return !(lhs == rhs); } diff --git a/include/caliper/common/Log.h b/include/caliper/common/Log.h index 6525d38a6..2f45aa312 100644 --- a/include/caliper/common/Log.h +++ b/include/caliper/common/Log.h @@ -31,7 +31,8 @@ class Log static void fini(); static bool is_initialized(); - inline std::ostream& stream() { + inline std::ostream& stream() + { if (verbosity() < m_level) return m_nullstream; @@ -57,9 +58,7 @@ class Log /// \return The log stream's \a std::ostream object std::ostream& perror(int errnum, const char* msg = ""); - Log(int level = 1) - : m_level { level } - { } + Log(int level = 1) : m_level { level } {} }; } // namespace cali diff --git a/include/caliper/common/Node.h b/include/caliper/common/Node.h index c3e259e6e..9ee9650b9 100644 --- a/include/caliper/common/Node.h +++ b/include/caliper/common/Node.h @@ -24,7 +24,7 @@ namespace cali class Node : public util::LockfreeIntrusiveTree { - cali_id_t m_id; + cali_id_t m_id; util::LockfreeIntrusiveTree::Node m_treenode; cali_id_t m_attribute; @@ -33,28 +33,24 @@ class Node : public util::LockfreeIntrusiveTree public: Node(cali_id_t id, cali_id_t attr, const Variant& data) - : util::LockfreeIntrusiveTree(this, &Node::m_treenode), - m_id { id }, - m_attribute { attr }, - m_data { data } - { } + : util::LockfreeIntrusiveTree(this, &Node::m_treenode), m_id { id }, m_attribute { attr }, m_data { data } + {} Node(const Node&) = delete; - Node& operator = (const Node&) = delete; + Node& operator= (const Node&) = delete; /// \brief Check if the node's attribute and value are equal to /// \a attr and \a v - bool equals(cali_id_t attr, const Variant& v) const { - return m_data == v && m_attribute == attr; - } + bool equals(cali_id_t attr, const Variant& v) const { return m_data == v && m_attribute == attr; } /// \brief Return the node's attribute ID cali_id_t attribute() const { return m_attribute; } + /// \brief Return the node's data element - Variant data() const { return m_data; } + Variant data() const { return m_data; } - cali_id_t id() const { return m_id; } + cali_id_t id() const { return m_id; } }; } // namespace cali diff --git a/include/caliper/common/OutputStream.h b/include/caliper/common/OutputStream.h index 86b1f0f0a..fca088718 100644 --- a/include/caliper/common/OutputStream.h +++ b/include/caliper/common/OutputStream.h @@ -27,24 +27,15 @@ class OutputStream public: - enum StreamType { - None, - StdOut, - StdErr, - File, - User - }; - - enum Mode { - Truncate, - Append - }; + enum StreamType { None, StdOut, StdErr, File, User }; + + enum Mode { Truncate, Append }; /// \brief Returns \a true if the stream is initialized, otherwise /// returns \a false - operator bool() const; + operator bool () const; - StreamType type() const; + StreamType type() const; /// \brief Return a C++ ostream. Opens/creates the underlying file stream /// if needed. @@ -55,23 +46,19 @@ class OutputStream ~OutputStream(); /// \brief Set stream type. (Note: for file streams, use \a set_filename). - void - set_stream(StreamType type); + void set_stream(StreamType type); /// \brief Assign a user-given stream - void - set_stream(std::ostream* os); + void set_stream(std::ostream* os); /// \brief Set stream's file name to \a filename - void - set_filename(const char* filename); + void set_filename(const char* filename); /// \brief Set stream's output mode for files /// /// Sets the open mode. \a Truncate resets the file, \a Append /// appends to it. Only applies to file streams. - void - set_mode(Mode mode); + void set_mode(Mode mode); /// \brief Create stream's filename from the given format string pattern and /// entry list. @@ -86,10 +73,7 @@ class OutputStream /// /// The special values "stdout" and "stderr" for \a formatstr will redirect /// output to standard out and standard error, respectively. - void - set_filename(const char* formatstr, - const CaliperMetadataAccessInterface& db, - const std::vector& rec); + void set_filename(const char* formatstr, const CaliperMetadataAccessInterface& db, const std::vector& rec); }; -} +} // namespace cali diff --git a/include/caliper/common/RuntimeConfig.h b/include/caliper/common/RuntimeConfig.h index f32ae389c..06de6d9a7 100644 --- a/include/caliper/common/RuntimeConfig.h +++ b/include/caliper/common/RuntimeConfig.h @@ -31,21 +31,20 @@ class ConfigSet public: struct Entry { - const char* key; ///< Variable key - cali_attr_type type; ///< Variable type - const char* value; ///< (Default) value as string - const char* descr; ///< One-line description - const char* long_descr; ///< Extensive, multi-line description + const char* key; ///< Variable key + cali_attr_type type; ///< Variable type + const char* value; ///< (Default) value as string + const char* descr; ///< One-line description + const char* long_descr; ///< Extensive, multi-line description }; static constexpr Entry Terminator = { 0, CALI_TYPE_INV, 0, 0, 0 }; constexpr ConfigSet() = default; - StringConverter get(const char* key) const; + StringConverter get(const char* key) const; }; - /// \brief Functionality to read and retrieve %Caliper config settings. class RuntimeConfig { @@ -55,7 +54,7 @@ class RuntimeConfig public: - typedef std::vector< std::pair > config_entry_list_t; + typedef std::vector> config_entry_list_t; RuntimeConfig(); @@ -63,27 +62,27 @@ class RuntimeConfig StringConverter get(const char* set, const char* key); /// \brief Initialize a ConfigSet from ConfigSet::Entry list (old method) - ConfigSet init(const char* name, const ConfigSet::Entry* set); + ConfigSet init(const char* name, const ConfigSet::Entry* set); /// \brief Initialize a ConfigSet - ConfigSet init(const char* name, const config_entry_list_t&); + ConfigSet init(const char* name, const config_entry_list_t&); /// \brief Pre-set config entry \a key to \a value. /// /// The value may be overwritten by configuration files or environment /// variables. - void preset(const char* key, const std::string& value); + void preset(const char* key, const std::string& value); /// \brief Set config entry \a key to \a value. /// /// The value will *not* be overwritten by configuration files, /// profile settings, or environment variables. - void set(const char* key, const std::string& value); + void set(const char* key, const std::string& value); /// \brief Import config values from the given the \a values map - void import(const std::map& values); + void import(const std::map& values); - bool allow_read_env(); + bool allow_read_env(); /// \brief Enable or disable reading of configuration settings /// from environment variables. @@ -96,7 +95,7 @@ class RuntimeConfig /// /// \note Only effective *before* initialization of the %Caliper /// runtime system. - bool allow_read_env(bool allow); + bool allow_read_env(bool allow); /// \brief Print the current configuration settings. /// @@ -108,7 +107,7 @@ class RuntimeConfig // --- Static API (temporary) // - static RuntimeConfig get_default_config(); + static RuntimeConfig get_default_config(); }; // class RuntimeConfig diff --git a/include/caliper/common/StringConverter.h b/include/caliper/common/StringConverter.h index 71e3e28c0..99ec997be 100644 --- a/include/caliper/common/StringConverter.h +++ b/include/caliper/common/StringConverter.h @@ -17,43 +17,38 @@ namespace cali { -class StringConverter { +class StringConverter +{ std::string m_str; public: - StringConverter() - : m_str() - { } + StringConverter() : m_str() {} - StringConverter(const std::string& str) - : m_str(str) - { } + StringConverter(const std::string& str) : m_str(str) {} - cali_id_t to_id() const; + cali_id_t to_id() const; - bool to_bool(bool* okptr = nullptr) const; + bool to_bool(bool* okptr = nullptr) const; - int to_int(bool* okptr = nullptr) const; - int64_t to_int64(bool* okptr = nullptr) const; - uint64_t to_uint(bool* okptr = nullptr, int base = 10) const; + int to_int(bool* okptr = nullptr) const; + int64_t to_int64(bool* okptr = nullptr) const; + uint64_t to_uint(bool* okptr = nullptr, int base = 10) const; - double to_double(bool* okptr = nullptr) const; + double to_double(bool* okptr = nullptr) const; - std::string to_string(bool* okptr = nullptr) const { + std::string to_string(bool* okptr = nullptr) const + { if (okptr) *okptr = true; return m_str; } - std::vector - to_stringlist(const char* separators = ",", bool* okptr = nullptr) const; + std::vector to_stringlist(const char* separators = ",", bool* okptr = nullptr) const; - std::vector - rec_list(bool* okptr = nullptr) const; + std::vector rec_list(bool* okptr = nullptr) const; - std::map - rec_dict(bool* okptr = nullptr) const; + std::map rec_dict(bool* okptr = nullptr) const; }; } // namespace cali diff --git a/include/caliper/common/Variant.h b/include/caliper/common/Variant.h index d9ca4a80f..60c3019ac 100644 --- a/include/caliper/common/Variant.h +++ b/include/caliper/common/Variant.h @@ -34,99 +34,85 @@ class Variant public: - constexpr Variant() - : m_v { CALI_TYPE_INV, { static_cast(0) } } { } + constexpr Variant() : m_v { CALI_TYPE_INV, { static_cast(0) } } {} - Variant(const cali_variant_t& v) - : m_v(v) { } + Variant(const cali_variant_t& v) : m_v(v) {} - Variant(bool val) - : m_v(cali_make_variant_from_bool(val)) { } - Variant(int val) - : m_v(cali_make_variant_from_int(val)) { } - Variant(double val) - : m_v(cali_make_variant_from_double(val)) { } + Variant(bool val) : m_v(cali_make_variant_from_bool(val)) {} - template::value, int >::type = 0> - Variant(U val) - : m_v(cali_make_variant_from_uint(val)) { } + Variant(int val) : m_v(cali_make_variant_from_int(val)) {} - Variant(const char* val) - : m_v(cali_make_variant_from_string(val)) { } - Variant(cali_attr_type val) - : m_v(cali_make_variant_from_type(val)) { } + Variant(double val) : m_v(cali_make_variant_from_double(val)) {} - Variant(cali_attr_type type, const void* data, std::size_t size) - { - m_v = cali_make_variant(type, data, size); - } + template ::value, int>::type = 0> + Variant(U val) : m_v(cali_make_variant_from_uint(val)) + {} - bool empty() const { - return (m_v.type_and_size & CALI_VARIANT_TYPE_MASK) == CALI_TYPE_INV; - }; - operator bool() const { - return !empty(); - } + Variant(const char* val) : m_v(cali_make_variant_from_string(val)) {} + + Variant(cali_attr_type val) : m_v(cali_make_variant_from_type(val)) {} - bool has_unmanaged_data() const { + Variant(cali_attr_type type, const void* data, std::size_t size) { m_v = cali_make_variant(type, data, size); } + + bool empty() const { return (m_v.type_and_size & CALI_VARIANT_TYPE_MASK) == CALI_TYPE_INV; }; + + operator bool () const { return !empty(); } + + bool has_unmanaged_data() const + { cali_attr_type t = type(); return (t == CALI_TYPE_STRING || t == CALI_TYPE_USR); } cali_variant_t c_variant() const { return m_v; } - cali_attr_type type() const { return cali_variant_get_type(m_v); } - const void* data() const { return cali_variant_get_data(&m_v); } - size_t size() const { return cali_variant_get_size(m_v); } + cali_attr_type type() const { return cali_variant_get_type(m_v); } - void* get_ptr() const { return cali_variant_get_ptr(m_v); } + const void* data() const { return cali_variant_get_data(&m_v); } - cali_id_t to_id(bool* okptr = nullptr) const; - int to_int(bool* okptr = nullptr) const { - return cali_variant_to_int(m_v, okptr); - } - int64_t to_int64(bool* okptr = nullptr) const { - return cali_variant_to_int64(m_v, okptr); - } - uint64_t to_uint(bool* okptr = nullptr) const { - return cali_variant_to_uint(m_v, okptr); - } - bool to_bool(bool* okptr = nullptr) const { - return cali_variant_to_bool(m_v, okptr); - } - double to_double(bool* okptr = nullptr) const { - return cali_variant_to_double(m_v, okptr); - } - cali_attr_type to_attr_type(bool* okptr = nullptr) const { - return cali_variant_to_type(m_v, okptr); - } + size_t size() const { return cali_variant_get_size(m_v); } + + void* get_ptr() const { return cali_variant_get_ptr(m_v); } - std::string to_string() const; + cali_id_t to_id(bool* okptr = nullptr) const; - Variant copy(void* ptr) const { + int to_int(bool* okptr = nullptr) const { return cali_variant_to_int(m_v, okptr); } + + int64_t to_int64(bool* okptr = nullptr) const { return cali_variant_to_int64(m_v, okptr); } + + uint64_t to_uint(bool* okptr = nullptr) const { return cali_variant_to_uint(m_v, okptr); } + + bool to_bool(bool* okptr = nullptr) const { return cali_variant_to_bool(m_v, okptr); } + + double to_double(bool* okptr = nullptr) const { return cali_variant_to_double(m_v, okptr); } + + cali_attr_type to_attr_type(bool* okptr = nullptr) const { return cali_variant_to_type(m_v, okptr); } + + std::string to_string() const; + + Variant copy(void* ptr) const + { Variant to(*this); if (has_unmanaged_data()) - to.m_v.value.unmanaged_ptr = - memcpy(ptr, m_v.value.unmanaged_const_ptr, size()); + to.m_v.value.unmanaged_ptr = memcpy(ptr, m_v.value.unmanaged_const_ptr, size()); return to; } - Variant& operator += (const Variant& val); + Variant& operator+= (const Variant& val); - Variant& min(const Variant& val); - Variant& max(const Variant& val); + Variant& min(const Variant& val); + Variant& max(const Variant& val); - static void update_minmaxsum(const Variant& val, Variant& min_val, Variant& max_val, Variant& sum_val); + static void update_minmaxsum(const Variant& val, Variant& min_val, Variant& max_val, Variant& sum_val); - size_t pack(unsigned char* buf) const { - return cali_variant_pack(m_v, buf); - } + size_t pack(unsigned char* buf) const { return cali_variant_pack(m_v, buf); } - std::ostream& write_cali(std::ostream& os); + std::ostream& write_cali(std::ostream& os); - static Variant unpack(const unsigned char* buf, size_t* inc, bool* ok = nullptr) { + static Variant unpack(const unsigned char* buf, size_t* inc, bool* ok = nullptr) + { return Variant(cali_variant_unpack(buf, inc, ok)); } @@ -134,28 +120,32 @@ class Variant // vector data() const; - friend bool operator == (const Variant& lhs, const Variant& rhs); - friend bool operator != (const Variant& lhs, const Variant& rhs); - friend bool operator < (const Variant& lhs, const Variant& rhs); - friend bool operator > (const Variant& lhs, const Variant& rhs); + friend bool operator== (const Variant& lhs, const Variant& rhs); + friend bool operator!= (const Variant& lhs, const Variant& rhs); + friend bool operator< (const Variant& lhs, const Variant& rhs); + friend bool operator> (const Variant& lhs, const Variant& rhs); }; -inline bool operator == (const Variant& lhs, const Variant& rhs) { +inline bool operator== (const Variant& lhs, const Variant& rhs) +{ return cali_variant_eq(lhs.m_v, rhs.m_v); } -inline bool operator != (const Variant& lhs, const Variant& rhs) { +inline bool operator!= (const Variant& lhs, const Variant& rhs) +{ return !cali_variant_eq(lhs.m_v, rhs.m_v); } -inline bool operator < (const Variant& lhs, const Variant& rhs) { +inline bool operator< (const Variant& lhs, const Variant& rhs) +{ return (cali_variant_compare(lhs.m_v, rhs.m_v) < 0); } -inline bool operator > (const Variant& lhs, const Variant& rhs) { +inline bool operator> (const Variant& lhs, const Variant& rhs) +{ return (cali_variant_compare(lhs.m_v, rhs.m_v) > 0); } -std::ostream& operator << (std::ostream& os, const Variant& v); +std::ostream& operator<< (std::ostream& os, const Variant& v); } // namespace cali diff --git a/include/caliper/common/cali_types.h b/include/caliper/common/cali_types.h index f295c86b5..d79d4dc57 100644 --- a/include/caliper/common/cali_types.h +++ b/include/caliper/common/cali_types.h @@ -14,7 +14,8 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif typedef uint64_t cali_id_t; @@ -25,16 +26,16 @@ typedef uint64_t cali_id_t; * \brief Data type of an attribute. */ typedef enum { - CALI_TYPE_INV = 0, /**< Invalid type */ - CALI_TYPE_USR = 1, /**< User-defined type (pointer to binary data) */ - CALI_TYPE_INT = 2, /**< 64-bit signed integer */ - CALI_TYPE_UINT = 3, /**< 64-bit unsigned integer */ - CALI_TYPE_STRING = 4, /**< String (\a char*) */ - CALI_TYPE_ADDR = 5, /**< 64-bit address */ - CALI_TYPE_DOUBLE = 6, /**< Double-precision floating point type */ - CALI_TYPE_BOOL = 7, /**< C or C++ boolean */ - CALI_TYPE_TYPE = 8, /**< Instance of cali_attr_type */ - CALI_TYPE_PTR = 9 /**< Raw pointer. Internal use only. */ + CALI_TYPE_INV = 0, /**< Invalid type */ + CALI_TYPE_USR = 1, /**< User-defined type (pointer to binary data) */ + CALI_TYPE_INT = 2, /**< 64-bit signed integer */ + CALI_TYPE_UINT = 3, /**< 64-bit unsigned integer */ + CALI_TYPE_STRING = 4, /**< String (\a char*) */ + CALI_TYPE_ADDR = 5, /**< 64-bit address */ + CALI_TYPE_DOUBLE = 6, /**< Double-precision floating point type */ + CALI_TYPE_BOOL = 7, /**< C or C++ boolean */ + CALI_TYPE_TYPE = 8, /**< Instance of cali_attr_type */ + CALI_TYPE_PTR = 9 /**< Raw pointer. Internal use only. */ } cali_attr_type; #define CALI_MAXTYPE CALI_TYPE_PTR @@ -42,11 +43,9 @@ typedef enum { /** * \brief */ -const char* -cali_type2string(cali_attr_type type); +const char* cali_type2string(cali_attr_type type); -cali_attr_type -cali_string2type(const char* str); +cali_attr_type cali_string2type(const char* str); /** * \brief Attribute property flags. @@ -56,10 +55,10 @@ cali_string2type(const char* str); * (however, the scope flags are mutually exclusive). */ typedef enum { - /** \brief Default value */ - CALI_ATTR_DEFAULT = 0, + /** \brief Default value */ + CALI_ATTR_DEFAULT = 0, - /** + /** * \brief Store directly as key:value pair, not in the context tree. * * Entries with this property will be not be put into the context @@ -67,28 +66,28 @@ typedef enum { * and in snapshot records. ASVALUE attributes cannot be * nested. Only applicable to scalar data types. */ - CALI_ATTR_ASVALUE = 1, + CALI_ATTR_ASVALUE = 1, - /** \brief Create a separate context tree root node for this attribute (deprecated). + /** \brief Create a separate context tree root node for this attribute (deprecated). * * Useful for attributes that form overlapping hierarchies separate from * the main region stack. No longer used. */ - CALI_ATTR_NOMERGE = 2, - /** \brief Process-scope attribute. Shared between all threads. */ - CALI_ATTR_SCOPE_PROCESS = 12, /* scope flags are mutually exclusive */ - /** \brief Thread-scope attribute. */ - CALI_ATTR_SCOPE_THREAD = 20, - /** \brief Task-scope attribute. Currently unused. */ - CALI_ATTR_SCOPE_TASK = 24, + CALI_ATTR_NOMERGE = 2, + /** \brief Process-scope attribute. Shared between all threads. */ + CALI_ATTR_SCOPE_PROCESS = 12, /* scope flags are mutually exclusive */ + /** \brief Thread-scope attribute. */ + CALI_ATTR_SCOPE_THREAD = 20, + /** \brief Task-scope attribute. Currently unused. */ + CALI_ATTR_SCOPE_TASK = 24, - /** \brief Skip event callbacks for blackboard updates with this attribute */ - CALI_ATTR_SKIP_EVENTS = 64, + /** \brief Skip event callbacks for blackboard updates with this attribute */ + CALI_ATTR_SKIP_EVENTS = 64, - /** \brief Do not include this attribute in snapshots */ - CALI_ATTR_HIDDEN = 128, + /** \brief Do not include this attribute in snapshots */ + CALI_ATTR_HIDDEN = 128, - /** \brief Begin/end calls are properly aligned with the call stack. + /** \brief Begin/end calls are properly aligned with the call stack. * * Indicates that \a begin/end calls for this attribute are * correctly nested with the call stack and other NESTED attributes. @@ -96,9 +95,9 @@ typedef enum { * partially overlap function calls or other NESTED attribute * regions. */ - CALI_ATTR_NESTED = 256, + CALI_ATTR_NESTED = 256, - /** \brief A metadata attribute describing global information + /** \brief A metadata attribute describing global information * for a measurement run * * Global attributes represent metadata associated with an application @@ -107,47 +106,47 @@ typedef enum { * in some output formats. For distributed programs (e.g. MPI), * global attributes should have the same value on each process. */ - CALI_ATTR_GLOBAL = 512, + CALI_ATTR_GLOBAL = 512, - /** \brief This attribute is not aligned with stacked begin/end regions. + /** \brief This attribute is not aligned with stacked begin/end regions. * * Entries with this property may still be merged into a single context * tree branch, but one that is separate from the properly nested * region branch. Stack nesting checks are skipped. */ - CALI_ATTR_UNALIGNED = 1024, + CALI_ATTR_UNALIGNED = 1024, - /** \brief This attribute is aggregatable (i.e., a metric). + /** \brief This attribute is aggregatable (i.e., a metric). * * Attributes with this flag will automatically be aggregated by the * \a aggregate service. This flag replaces the previous * \a class.aggregatable meta-attribute. */ - CALI_ATTR_AGGREGATABLE = 2048, + CALI_ATTR_AGGREGATABLE = 2048, - /** + /** * \brief Annotation level 1 * * Annotation levels can be used to describe and set the granularity of * region annotations. Level 0 is the finest level, level 7 is the * coarsest. */ - CALI_ATTR_LEVEL_1 = 0x10000, - /** \brief Annotation level 2 */ - CALI_ATTR_LEVEL_2 = 0x20000, - /** \brief Annotation level 3 */ - CALI_ATTR_LEVEL_3 = 0x30000, - /** \brief Annotation level 4 */ - CALI_ATTR_LEVEL_4 = 0x40000, - /** \brief Annotation level 5 */ - CALI_ATTR_LEVEL_5 = 0x50000, - /** \brief Annotation level 6 */ - CALI_ATTR_LEVEL_6 = 0x60000, - /** \brief Annotation level 7 */ - CALI_ATTR_LEVEL_7 = 0x70000 + CALI_ATTR_LEVEL_1 = 0x10000, + /** \brief Annotation level 2 */ + CALI_ATTR_LEVEL_2 = 0x20000, + /** \brief Annotation level 3 */ + CALI_ATTR_LEVEL_3 = 0x30000, + /** \brief Annotation level 4 */ + CALI_ATTR_LEVEL_4 = 0x40000, + /** \brief Annotation level 5 */ + CALI_ATTR_LEVEL_5 = 0x50000, + /** \brief Annotation level 6 */ + CALI_ATTR_LEVEL_6 = 0x60000, + /** \brief Annotation level 7 */ + CALI_ATTR_LEVEL_7 = 0x70000 } cali_attr_properties; -#define CALI_ATTR_LEVEL_0 0x0 +#define CALI_ATTR_LEVEL_0 0x0 #define CALI_ATTR_SCOPE_MASK 60 #define CALI_ATTR_LEVEL_MASK 0x70000 @@ -158,11 +157,9 @@ typedef enum { * \param len Length of string buffer * \return -1 if provided buffer is too short; length of written string otherwise */ -int -cali_prop2string(int prop, char* buf, size_t len); +int cali_prop2string(int prop, char* buf, size_t len); -int -cali_string2prop(const char*); +int cali_string2prop(const char*); #ifdef __cplusplus } // extern "C" diff --git a/include/caliper/common/cali_variant.h b/include/caliper/common/cali_variant.h index 9c88d995e..462d30383 100644 --- a/include/caliper/common/cali_variant.h +++ b/include/caliper/common/cali_variant.h @@ -15,7 +15,8 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /** The variant struct manages values of different types in Caliper. @@ -39,133 +40,118 @@ typedef struct { cali_attr_type v_type; void* unmanaged_ptr; const void* unmanaged_const_ptr; - } value; + } value; } cali_variant_t; #define CALI_VARIANT_TYPE_MASK 0xFF #define CALI_VARIANT_HASH_MASK 0xFFFFFF00 -inline cali_variant_t -cali_make_empty_variant() +inline cali_variant_t cali_make_empty_variant() { cali_variant_t v; v.type_and_size = 0; - v.value.v_uint = 0; + v.value.v_uint = 0; return v; } /** \brief Test if variant is empty */ -inline bool -cali_variant_is_empty(cali_variant_t v) +inline bool cali_variant_is_empty(cali_variant_t v) { return 0 == v.type_and_size; } /** \brief Return type of a variant */ -cali_attr_type -cali_variant_get_type(cali_variant_t v); +cali_attr_type cali_variant_get_type(cali_variant_t v); /** \brief Return size of the variant's value */ -size_t -cali_variant_get_size(cali_variant_t v); +size_t cali_variant_get_size(cali_variant_t v); /** \brief Get a pointer to the variant's data */ -const void* -cali_variant_get_data(const cali_variant_t* v); +const void* cali_variant_get_data(const cali_variant_t* v); /** \brief Construct variant from type, pointer, and size */ -cali_variant_t -cali_make_variant(cali_attr_type type, const void* ptr, size_t size); - +cali_variant_t cali_make_variant(cali_attr_type type, const void* ptr, size_t size); -inline cali_variant_t -cali_make_variant_from_bool(bool value) +inline cali_variant_t cali_make_variant_from_bool(bool value) { cali_variant_t v; v.type_and_size = CALI_TYPE_BOOL; - v.value.v_uint = 0; - v.value.v_bool = value; + v.value.v_uint = 0; + v.value.v_bool = value; return v; } -inline cali_variant_t -cali_make_variant_from_int(int value) +inline cali_variant_t cali_make_variant_from_int(int value) { cali_variant_t v; v.type_and_size = CALI_TYPE_INT; - v.value.v_int = value; + v.value.v_int = value; return v; } -inline cali_variant_t -cali_make_variant_from_int64(int64_t value) +inline cali_variant_t cali_make_variant_from_int64(int64_t value) { cali_variant_t v; v.type_and_size = CALI_TYPE_INT; - v.value.v_int = value; + v.value.v_int = value; return v; } -inline cali_variant_t -cali_make_variant_from_uint(uint64_t value) +inline cali_variant_t cali_make_variant_from_uint(uint64_t value) { cali_variant_t v; v.type_and_size = CALI_TYPE_UINT; - v.value.v_uint = value; + v.value.v_uint = value; return v; } -inline cali_variant_t -cali_make_variant_from_double(double value) +inline cali_variant_t cali_make_variant_from_double(double value) { cali_variant_t v; - v.type_and_size = CALI_TYPE_DOUBLE; + v.type_and_size = CALI_TYPE_DOUBLE; v.value.v_double = value; return v; } -inline cali_variant_t -cali_make_variant_from_string(const char* value) +inline cali_variant_t cali_make_variant_from_string(const char* value) { - size_t size = strlen(value); + size_t size = strlen(value); uint64_t hash = 0; const unsigned char* p = (const unsigned char*) value; hash |= p[0]; hash <<= 8; - hash |= p[size/2]; + hash |= p[size / 2]; hash <<= 8; - hash |= p[size-1]; + hash |= p[size - 1]; hash <<= 8; cali_variant_t v; - v.type_and_size = (size << 32) | hash | (CALI_TYPE_STRING & CALI_VARIANT_TYPE_MASK); + v.type_and_size = (size << 32) | hash | (CALI_TYPE_STRING & CALI_VARIANT_TYPE_MASK); v.value.unmanaged_const_ptr = value; return v; } -inline cali_variant_t -cali_make_variant_from_type(cali_attr_type value) +inline cali_variant_t cali_make_variant_from_type(cali_attr_type value) { cali_variant_t v; v.type_and_size = CALI_TYPE_TYPE; - v.value.v_uint = 0; - v.value.v_type = value; + v.value.v_uint = 0; + v.value.v_type = value; return v; } -inline cali_variant_t -cali_make_variant_from_ptr(void* ptr) +inline cali_variant_t cali_make_variant_from_ptr(void* ptr) { cali_variant_t v; - v.type_and_size = CALI_TYPE_PTR; + v.type_and_size = CALI_TYPE_PTR; v.value.unmanaged_ptr = ptr; return v; } @@ -173,60 +159,48 @@ cali_make_variant_from_ptr(void* ptr) /** \brief Return the pointer stored in the variant. Only works for * CALI_TYPE_PTR. */ -inline void* -cali_variant_get_ptr(cali_variant_t v) +inline void* cali_variant_get_ptr(cali_variant_t v) { return v.type_and_size == CALI_TYPE_PTR ? v.value.unmanaged_ptr : NULL; } /** \brief Return the variant's value as integer */ -int -cali_variant_to_int(cali_variant_t v, bool* okptr); +int cali_variant_to_int(cali_variant_t v, bool* okptr); -int64_t -cali_variant_to_int64(cali_variant_t v, bool* okptr); +int64_t cali_variant_to_int64(cali_variant_t v, bool* okptr); -uint64_t -cali_variant_to_uint(cali_variant_t v, bool* okptr); +uint64_t cali_variant_to_uint(cali_variant_t v, bool* okptr); -double -cali_variant_to_double(cali_variant_t v, bool* okptr); +double cali_variant_to_double(cali_variant_t v, bool* okptr); -cali_attr_type -cali_variant_to_type(cali_variant_t v, bool* okptr); +cali_attr_type cali_variant_to_type(cali_variant_t v, bool* okptr); -bool -cali_variant_to_bool(cali_variant_t v, bool* okptr); +bool cali_variant_to_bool(cali_variant_t v, bool* okptr); /** \brief Compare variant values. */ -int -cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs); +int cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs); /** \brief Check if lhs and rhs' values are equal using a deep comparison. * Caller must make sure that lhs' and rhs' type and size are equal. */ -bool -_cali_variant_value_eq(cali_variant_t lhs, cali_variant_t rhs); +bool _cali_variant_value_eq(cali_variant_t lhs, cali_variant_t rhs); /** \brief Check if lhs is equal to rhs */ -inline bool -cali_variant_eq(cali_variant_t lhs, cali_variant_t rhs) +inline bool cali_variant_eq(cali_variant_t lhs, cali_variant_t rhs) { return lhs.type_and_size == rhs.type_and_size && _cali_variant_value_eq(lhs, rhs); } /** \brief Pack variant into byte buffer */ -size_t -cali_variant_pack(cali_variant_t v, unsigned char* buf); +size_t cali_variant_pack(cali_variant_t v, unsigned char* buf); /** \brief Unpack variant from byte buffer */ -cali_variant_t -cali_variant_unpack(const unsigned char* buf, size_t* inc, bool* okptr); +cali_variant_t cali_variant_unpack(const unsigned char* buf, size_t* inc, bool* okptr); #ifdef __cplusplus } /* extern "C" */ diff --git a/include/caliper/common/callback.hpp b/include/caliper/common/callback.hpp index 7610aba4f..cd98da727 100644 --- a/include/caliper/common/callback.hpp +++ b/include/caliper/common/callback.hpp @@ -13,36 +13,34 @@ namespace util { -template +template class callback { - std::vector< std::function > mCb; + std::vector> mCb; public: - void connect(std::function f) { - mCb.push_back(f); - } + void connect(std::function f) { mCb.push_back(f); } - bool empty() const { - return mCb.empty(); - } + bool empty() const { return mCb.empty(); } - template - void operator()(Args&&... a) { - for ( auto& f : mCb ) + template + void operator() (Args&&... a) + { + for (auto& f : mCb) f(a...); } - template - R accumulate(Op op, R init, Args&&... a) { - for ( auto& f : mCb ) + template + R accumulate(Op op, R init, Args&&... a) + { + for (auto& f : mCb) init = Op(init, f(a...)); return init; } }; -} +} // namespace util #endif diff --git a/include/caliper/common/lockfree-tree.hpp b/include/caliper/common/lockfree-tree.hpp index 7727c223a..f6d2c1354 100644 --- a/include/caliper/common/lockfree-tree.hpp +++ b/include/caliper/common/lockfree-tree.hpp @@ -44,52 +44,51 @@ namespace util /// performed. Specifically, tree nodes can only be added, but not /// moved or removed. -template -class LockfreeIntrusiveTree { +template +class LockfreeIntrusiveTree +{ public: struct Node { - T* parent; - T* next; + T* parent; + T* next; std::atomic head; - Node() - : parent(0), next(0), head(0) - { } + Node() : parent(0), next(0), head(0) {} }; private: // --- private data - T* m_me; + T* m_me; LockfreeIntrusiveTree::Node T::*m_node; // --- private methods static LockfreeIntrusiveTree::Node& node(T* t, LockfreeIntrusiveTree::Node T::*node) { return (t->*node); } - LockfreeIntrusiveTree tree(T* t) const { return LockfreeIntrusiveTree(t, m_node); } + LockfreeIntrusiveTree tree(T* t) const { return LockfreeIntrusiveTree(t, m_node); } + LockfreeIntrusiveTree::Node& node(T* t) const { return node(t, m_node); } public: - LockfreeIntrusiveTree(T* me, LockfreeIntrusiveTree::Node T::*nodeptr) - : m_me(me), m_node(nodeptr) - { } + LockfreeIntrusiveTree(T* me, LockfreeIntrusiveTree::Node T::*nodeptr) : m_me(me), m_node(nodeptr) {} + + T* parent() const { return node(m_me).parent; } + + T* first_child() const { return node(m_me).head.load(std::memory_order_relaxed); } - T* parent() const { return node(m_me).parent; } - T* first_child() const { return node(m_me).head.load(std::memory_order_relaxed); } - T* next_sibling() const { return node(m_me).next; } + T* next_sibling() const { return node(m_me).next; } - void append(T* sub) { + void append(T* sub) + { LockfreeIntrusiveTree::Node& n = node(m_me); node(sub).parent = m_me; - while(!n.head.compare_exchange_weak(node(sub).next, sub, - std::memory_order_release, - std::memory_order_relaxed)) + while (!n.head.compare_exchange_weak(node(sub).next, sub, std::memory_order_release, std::memory_order_relaxed)) ; } }; diff --git a/include/caliper/reader/Aggregator.h b/include/caliper/reader/Aggregator.h index a08e7c5b6..d55280270 100644 --- a/include/caliper/reader/Aggregator.h +++ b/include/caliper/reader/Aggregator.h @@ -34,17 +34,13 @@ class Aggregator void add(CaliperMetadataAccessInterface&, const EntryList&); - void operator()(CaliperMetadataAccessInterface& db, const EntryList& list) { - add(db, list); - } + void operator() (CaliperMetadataAccessInterface& db, const EntryList& list) { add(db, list); } void flush(CaliperMetadataAccessInterface&, SnapshotProcessFn push); - static const QuerySpec::FunctionSignature* - aggregation_defs(); + static const QuerySpec::FunctionSignature* aggregation_defs(); - static std::string - get_aggregation_attribute_name(const QuerySpec::AggregationOp& op); + static std::string get_aggregation_attribute_name(const QuerySpec::AggregationOp& op); }; } // namespace cali diff --git a/include/caliper/reader/CalQLParser.h b/include/caliper/reader/CalQLParser.h index 4afaf04f6..305bd7d03 100644 --- a/include/caliper/reader/CalQLParser.h +++ b/include/caliper/reader/CalQLParser.h @@ -22,31 +22,27 @@ class CalQLParser { struct CalQLParserImpl; std::unique_ptr mP; - + public: /// \brief Read %CalQL expression from the given input stream \a is. CalQLParser(std::istream& is); /// \brief Read %CalQL expression from \a str. CalQLParser(const char* str); - + ~CalQLParser(); /// \brief Indicate if there was an error parsing the %CalQL expression. - bool - error() const; + bool error() const; /// \brief Approximate position of a parser error in the given string or stream. - std::istream::pos_type - error_pos(); + std::istream::pos_type error_pos(); /// \brief A descriptive error message in case of a parse error. - std::string - error_msg() const; + std::string error_msg() const; /// \brief Returns the query specification object for the given %CalQL expression. - QuerySpec - spec() const; -}; + QuerySpec spec() const; +}; -} +} // namespace cali diff --git a/include/caliper/reader/CaliReader.h b/include/caliper/reader/CaliReader.h index decfad5cb..5ba7be974 100644 --- a/include/caliper/reader/CaliReader.h +++ b/include/caliper/reader/CaliReader.h @@ -27,7 +27,7 @@ class CaliReader CaliReader(); ~CaliReader(); - bool error() const; + bool error() const; std::string error_msg() const; void read(std::istream& is, CaliperMetadataDB& db, NodeProcessFn node_proc, SnapshotProcessFn snap_proc); diff --git a/include/caliper/reader/CaliWriter.h b/include/caliper/reader/CaliWriter.h index a7fba415c..4e424a90f 100644 --- a/include/caliper/reader/CaliWriter.h +++ b/include/caliper/reader/CaliWriter.h @@ -17,7 +17,7 @@ namespace cali class CaliperMetadataAccessInterface; class Node; class OutputStream; - + class CaliWriter { struct CaliWriterImpl; @@ -25,20 +25,17 @@ class CaliWriter public: - CaliWriter() - { } - + CaliWriter() {} + CaliWriter(OutputStream& os); ~CaliWriter(); size_t num_written() const; - - void write_snapshot(const CaliperMetadataAccessInterface&, - const std::vector&); - void write_globals(const CaliperMetadataAccessInterface&, - const std::vector&); + void write_snapshot(const CaliperMetadataAccessInterface&, const std::vector&); + + void write_globals(const CaliperMetadataAccessInterface&, const std::vector&); }; -} +} // namespace cali diff --git a/include/caliper/reader/CaliperMetadataDB.h b/include/caliper/reader/CaliperMetadataDB.h index 8d4f63618..fd7dde0cc 100644 --- a/include/caliper/reader/CaliperMetadataDB.h +++ b/include/caliper/reader/CaliperMetadataDB.h @@ -43,50 +43,37 @@ class CaliperMetadataDB : public CaliperMetadataAccessInterface // --- I/O API // - Node* merge_node (cali_id_t node_id, - cali_id_t attr_id, - cali_id_t prnt_id, - const Variant& v_data, - IdMap& idmap); - - Node* merge_node (cali_id_t node_id, - cali_id_t attr_id, - cali_id_t prnt_id, - const std::string& data, - IdMap& idmap); - - EntryList merge_snapshot(size_t n_nodes, - const cali_id_t node_ids[], - size_t n_imm, - const cali_id_t attr_ids[], - const Variant values[], - const IdMap& idmap) const; + Node* merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const Variant& v_data, IdMap& idmap); + + Node* merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const std::string& data, IdMap& idmap); + + EntryList merge_snapshot( + size_t n_nodes, + const cali_id_t node_ids[], + size_t n_imm, + const cali_id_t attr_ids[], + const Variant values[], + const IdMap& idmap + ) const; /// \brief Merge snapshot record bound to metadata DB \a db /// into this metadata DB - EntryList merge_snapshot(const CaliperMetadataAccessInterface& db, - const std::vector& rec); + EntryList merge_snapshot(const CaliperMetadataAccessInterface& db, const std::vector& rec); - Entry merge_entry (cali_id_t node_id, - const IdMap& idmap); - Entry merge_entry (cali_id_t attr_id, - const std::string& data, - const IdMap& idmap); + Entry merge_entry(cali_id_t node_id, const IdMap& idmap); + Entry merge_entry(cali_id_t attr_id, const std::string& data, const IdMap& idmap); - void merge_global (cali_id_t node_id, - const IdMap& idmap); - void merge_global (cali_id_t attr_id, - const std::string& data, - const IdMap& idmap); + void merge_global(cali_id_t node_id, const IdMap& idmap); + void merge_global(cali_id_t attr_id, const std::string& data, const IdMap& idmap); // // --- Query API // - Node* node(cali_id_t id) const; + Node* node(cali_id_t id) const; - Attribute get_attribute(cali_id_t id) const; - Attribute get_attribute(const std::string& name) const; + Attribute get_attribute(cali_id_t id) const; + Attribute get_attribute(const std::string& name) const; std::vector get_all_attributes() const; @@ -94,15 +81,17 @@ class CaliperMetadataDB : public CaliperMetadataAccessInterface // --- Manipulation // - Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent = 0); - Node* make_tree_entry(std::size_t n, const Attribute attr[], const Variant data[], Node* parent = 0); - - Attribute create_attribute(const std::string& name, - cali_attr_type type, - int prop, - int meta = 0, - const Attribute* meta_attr = nullptr, - const Variant* meta_data = nullptr); + Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent = 0); + Node* make_tree_entry(std::size_t n, const Attribute attr[], const Variant data[], Node* parent = 0); + + Attribute create_attribute( + const std::string& name, + cali_attr_type type, + int prop, + int meta = 0, + const Attribute* meta_attr = nullptr, + const Variant* meta_data = nullptr + ); // // --- Globals // @@ -111,7 +100,7 @@ class CaliperMetadataDB : public CaliperMetadataAccessInterface std::vector get_globals(); /// \brief Set a global entry - void set_global(const Attribute& attr, const Variant& value); + void set_global(const Attribute& attr, const Variant& value); /// \brief Import global entries from metadata DB \a db into this /// metadata DB @@ -140,10 +129,9 @@ class CaliperMetadataDB : public CaliperMetadataAccessInterface void add_attribute_units(const std::map& aliases); /// \brief print usage statistics - std::ostream& - print_statistics(std::ostream& os); + std::ostream& print_statistics(std::ostream& os); }; -} +} // namespace cali #endif diff --git a/include/caliper/reader/FlatExclusiveRegionProfile.h b/include/caliper/reader/FlatExclusiveRegionProfile.h index 61627fdd0..c5063c5a2 100644 --- a/include/caliper/reader/FlatExclusiveRegionProfile.h +++ b/include/caliper/reader/FlatExclusiveRegionProfile.h @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. /// \file FlatExclusiveRegionProfile.h @@ -19,22 +19,23 @@ class CaliperMetadataAccessInterface; class Entry; /// \brief Calculate a flat exclusive region profile -class FlatExclusiveRegionProfile { +class FlatExclusiveRegionProfile +{ struct FlatExclusiveRegionProfileImpl; std::shared_ptr mP; public: - - FlatExclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name = ""); - void operator ()(CaliperMetadataAccessInterface& db, const std::vector& rec); + FlatExclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name = "" + ); + + void operator() (CaliperMetadataAccessInterface& db, const std::vector& rec); /// \brief Return tuple with { { region name -> value } map, sum in given region type, total sum } - std::tuple< std::map, double, double > - result() const; + std::tuple, double, double> result() const; }; } // namespace cali - diff --git a/include/caliper/reader/FlatInclusiveRegionProfile.h b/include/caliper/reader/FlatInclusiveRegionProfile.h index 655deb2d5..442c2ef58 100644 --- a/include/caliper/reader/FlatInclusiveRegionProfile.h +++ b/include/caliper/reader/FlatInclusiveRegionProfile.h @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. /// \file FlatInclusiveRegionProfile.h @@ -19,21 +19,23 @@ class CaliperMetadataAccessInterface; class Entry; /// \brief Calculate a flat exclusive region profile -class FlatInclusiveRegionProfile { +class FlatInclusiveRegionProfile +{ struct FlatInclusiveRegionProfileImpl; std::shared_ptr mP; public: - - FlatInclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name = ""); - void operator ()(CaliperMetadataAccessInterface& db, const std::vector& rec); + FlatInclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name = "" + ); + + void operator() (CaliperMetadataAccessInterface& db, const std::vector& rec); /// \brief Return tuple with { { region name -> value } map, sum in given region type, total sum } - std::tuple< std::map, double, double > - result() const; + std::tuple, double, double> result() const; }; } // namespace cali diff --git a/include/caliper/reader/FormatProcessor.h b/include/caliper/reader/FormatProcessor.h index b25338ef1..70006ce2e 100644 --- a/include/caliper/reader/FormatProcessor.h +++ b/include/caliper/reader/FormatProcessor.h @@ -47,12 +47,10 @@ class FormatProcessor void flush(CaliperMetadataAccessInterface& db); /// \brief Make FormatProcessor usable as a SnapshotProcessFn. - void operator()(CaliperMetadataAccessInterface& db, const EntryList& rec) { - process_record(db, rec); - } + void operator() (CaliperMetadataAccessInterface& db, const EntryList& rec) { process_record(db, rec); } /// \brief Return all known formatter signatures. static const QuerySpec::FunctionSignature* formatter_defs(); }; -} +} // namespace cali diff --git a/include/caliper/reader/NestedExclusiveRegionProfile.h b/include/caliper/reader/NestedExclusiveRegionProfile.h index feecb4e74..673b7c5d0 100644 --- a/include/caliper/reader/NestedExclusiveRegionProfile.h +++ b/include/caliper/reader/NestedExclusiveRegionProfile.h @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. /// \file NestedExclusiveRegionProfile.h @@ -19,21 +19,23 @@ class CaliperMetadataAccessInterface; class Entry; /// \brief Calculate a flat exclusive region profile -class NestedExclusiveRegionProfile { +class NestedExclusiveRegionProfile +{ struct NestedExclusiveRegionProfileImpl; std::shared_ptr mP; public: - - NestedExclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name = ""); - void operator ()(CaliperMetadataAccessInterface& db, const std::vector& rec); + NestedExclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name = "" + ); + + void operator() (CaliperMetadataAccessInterface& db, const std::vector& rec); /// \brief Return tuple with { { region name -> value } map, sum in given region type, total sum } - std::tuple< std::map, double, double > - result() const; + std::tuple, double, double> result() const; }; } // namespace cali diff --git a/include/caliper/reader/NestedInclusiveRegionProfile.h b/include/caliper/reader/NestedInclusiveRegionProfile.h index 45972cee5..89e638946 100644 --- a/include/caliper/reader/NestedInclusiveRegionProfile.h +++ b/include/caliper/reader/NestedInclusiveRegionProfile.h @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. /// \file NestedInclusiveRegionProfile.h @@ -19,21 +19,23 @@ class CaliperMetadataAccessInterface; class Entry; /// \brief Calculate a flat exclusive region profile -class NestedInclusiveRegionProfile { +class NestedInclusiveRegionProfile +{ struct NestedInclusiveRegionProfileImpl; std::shared_ptr mP; public: - - NestedInclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name = ""); - void operator ()(CaliperMetadataAccessInterface& db, const std::vector& rec); + NestedInclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name = "" + ); + + void operator() (CaliperMetadataAccessInterface& db, const std::vector& rec); /// \brief Return tuple with { { region name -> value } map, sum in given region type, total sum } - std::tuple< std::map, double, double > - result() const; + std::tuple, double, double> result() const; }; } // namespace cali diff --git a/include/caliper/reader/Preprocessor.h b/include/caliper/reader/Preprocessor.h index 5abd6f7c0..34fe34e8d 100644 --- a/include/caliper/reader/Preprocessor.h +++ b/include/caliper/reader/Preprocessor.h @@ -34,12 +34,12 @@ class Preprocessor EntryList process(CaliperMetadataAccessInterface&, const EntryList&); - void operator()(CaliperMetadataAccessInterface& db, const EntryList& rec, SnapshotProcessFn push) { + void operator() (CaliperMetadataAccessInterface& db, const EntryList& rec, SnapshotProcessFn push) + { push(db, process(db, rec)); } - static const QuerySpec::FunctionSignature* - preprocess_defs(); + static const QuerySpec::FunctionSignature* preprocess_defs(); }; } // namespace cali diff --git a/include/caliper/reader/QueryProcessor.h b/include/caliper/reader/QueryProcessor.h index f724e8a91..a98374475 100644 --- a/include/caliper/reader/QueryProcessor.h +++ b/include/caliper/reader/QueryProcessor.h @@ -24,7 +24,7 @@ class QueryProcessor { struct QueryProcessorImpl; std::shared_ptr mP; - + public: QueryProcessor(const QuerySpec&, OutputStream& stream); @@ -35,9 +35,7 @@ class QueryProcessor void flush(CaliperMetadataAccessInterface&); - void operator()(CaliperMetadataAccessInterface& db, const EntryList& rec) { - process_record(db, rec); - } + void operator() (CaliperMetadataAccessInterface& db, const EntryList& rec) { process_record(db, rec); } }; -} +} // namespace cali diff --git a/include/caliper/reader/QuerySpec.h b/include/caliper/reader/QuerySpec.h index 9fb7bda7c..961b0b764 100644 --- a/include/caliper/reader/QuerySpec.h +++ b/include/caliper/reader/QuerySpec.h @@ -15,27 +15,26 @@ namespace cali /// \brief Describes a %Caliper data processing rule (filter, aggregation, formatting) /// \ingroup ReaderAPI -struct QuerySpec -{ +struct QuerySpec { // // --- Types // /// \brief Template type for list-style query options - template + template struct SelectionList { enum SelectionOpt { Default, ///< Take the default None, ///< Take none All, ///< Take all available List ///< User-defined list - } selection; ///< Selection specification + } selection; ///< Selection specification /// \brief User-defined list std::vector list; /// \brief Use path (main nested region hierarchy) attributes - bool use_path; + bool use_path; }; /// \brief Describe function signatures in query specs @@ -51,64 +50,46 @@ struct QuerySpec /// \brief An aggregation function invocation in a query spec struct AggregationOp { - FunctionSignature op; ///< The aggregation operator - std::vector args; ///< Arguments for the aggregation operator (typically, the attribute name) + FunctionSignature op; ///< The aggregation operator + std::vector args; ///< Arguments for the aggregation operator (typically, the attribute name) - AggregationOp() - : op(FunctionSignatureTerminator) - { } + AggregationOp() : op(FunctionSignatureTerminator) {} - AggregationOp(const FunctionSignature& s) - : op(s) - { } + AggregationOp(const FunctionSignature& s) : op(s) {} - AggregationOp(const FunctionSignature& s, const std::vector& a) - : op(s), args(a) - { } + AggregationOp(const FunctionSignature& s, const std::vector& a) : op(s), args(a) {} }; /// \brief Sort description. struct SortSpec { /// \brief The sort order. - enum Order { - None, Ascending, Descending - } order; + enum Order { None, Ascending, Descending } order; + /// \brief Name of the attribute to be sorted. std::string attribute; - SortSpec(const std::string& s, Order o = Ascending) - : order(o), attribute(s) - { } + SortSpec(const std::string& s, Order o = Ascending) : order(o), attribute(s) {} }; /// \brief Filter condition struct Condition { - enum Op { - None, - Exist, NotExist, - Equal, NotEqual, - LessThan, GreaterThan, - LessOrEqual, GreaterOrEqual - } op; + enum Op { None, Exist, NotExist, Equal, NotEqual, LessThan, GreaterThan, LessOrEqual, GreaterOrEqual } op; + std::string attr_name; std::string value; - Condition() - : op(Op::None) - { } + Condition() : op(Op::None) {} - Condition(Condition::Op o, const std::string& name, const std::string& val) - : op(o), attr_name(name), value(val) - { } + Condition(Condition::Op o, const std::string& name, const std::string& val) : op(o), attr_name(name), value(val) + {} }; /// \brief Output formatter specification. struct FormatSpec { - enum Opt { - Default, User - } opt; ///< Default or user-defined formatter - FunctionSignature formatter; ///< The formatter to use. Signatures provided by FormatProcessor. - std::map kwargs; ///< Arguments to the formatter. + enum Opt { Default, User } opt; ///< Default or user-defined formatter + + FunctionSignature formatter; ///< The formatter to use. Signatures provided by FormatProcessor. + std::map kwargs; ///< Arguments to the formatter. }; struct PreprocessSpec { @@ -127,21 +108,21 @@ struct QuerySpec typedef SelectionList SortSelection; /// \brief List of aggregations to be performed. - AggregationSelection aggregate; + AggregationSelection aggregate; /// \brief List of attribute names that form the aggregation key (i.e., GROUP BY spec). - AttributeSelection groupby; + AttributeSelection groupby; /// \brief List of attributes to print in output - AttributeSelection select; + AttributeSelection select; /// \brief List of filter clauses (filters will be combined with AND) - FilterSelection filter; + FilterSelection filter; /// \brief List of sort specifications - SortSelection sort; + SortSelection sort; /// \brief Output formatter specification - FormatSpec format; + FormatSpec format; /// \brief Output aliases for attributes (i.e., "select x AS y" ) std::map aliases; @@ -150,7 +131,7 @@ struct QuerySpec std::map units; /// \brief List of preprocessing operations (i.e., "LET y=f(x)") - std::vector preprocess_ops; + std::vector preprocess_ops; }; } // namespace cali diff --git a/include/caliper/reader/RecordProcessor.h b/include/caliper/reader/RecordProcessor.h index 82a857616..78a872113 100644 --- a/include/caliper/reader/RecordProcessor.h +++ b/include/caliper/reader/RecordProcessor.h @@ -14,19 +14,15 @@ namespace cali { - class CaliperMetadataAccessInterface; +class CaliperMetadataAccessInterface; - typedef std::vector - EntryList; +typedef std::vector EntryList; - typedef std::function - NodeProcessFn; - typedef std::function - NodeFilterFn; +typedef std::function NodeProcessFn; +typedef std::function NodeFilterFn; - typedef std::function - SnapshotProcessFn; - typedef std::function - SnapshotFilterFn; +typedef std::function SnapshotProcessFn; +typedef std::function + SnapshotFilterFn; } // namespace cali diff --git a/include/caliper/reader/RecordSelector.h b/include/caliper/reader/RecordSelector.h index 01e4836e8..b69d1e92e 100644 --- a/include/caliper/reader/RecordSelector.h +++ b/include/caliper/reader/RecordSelector.h @@ -19,8 +19,8 @@ class CaliperMetadataAccessInterface; /// \brief Filter for snapshot records /// \ingroup ReaderAPI - -class RecordSelector + +class RecordSelector { struct RecordSelectorImpl; std::shared_ptr mP; @@ -30,12 +30,12 @@ class RecordSelector RecordSelector(const std::string& filter_string); RecordSelector(const QuerySpec& spec); RecordSelector(const QuerySpec::Condition& cond); - + ~RecordSelector(); bool pass(const CaliperMetadataAccessInterface&, const EntryList&); - void operator()(CaliperMetadataAccessInterface&, const EntryList&, SnapshotProcessFn) const; + void operator() (CaliperMetadataAccessInterface&, const EntryList&, SnapshotProcessFn) const; static std::vector parse(const std::string&); }; diff --git a/include/caliper/tools-util/Args.h b/include/caliper/tools-util/Args.h index 613e64fe4..f922f96dd 100644 --- a/include/caliper/tools-util/Args.h +++ b/include/caliper/tools-util/Args.h @@ -17,7 +17,8 @@ namespace util /// \brief Parse command-line arguments. -class Args { +class Args +{ struct ArgsImpl; std::shared_ptr mP; @@ -29,10 +30,10 @@ class Args { const char* longopt; char shortopt; - bool has_argument; + bool has_argument; const char* info; - const char* argument_info; + const char* argument_info; }; static constexpr Table Terminator = { nullptr, nullptr, 0, false, nullptr, nullptr }; @@ -50,7 +51,7 @@ class Args { /// Parses command-line options given by \a argc and \a argv. /// \return \a argv[] index of first unknown option argument - int parse(int argc, const char* const argv[], int pos = 1); + int parse(int argc, const char* const argv[], int pos = 1); // --- Retrieval @@ -62,11 +63,11 @@ class Args { std::vector options() const; std::vector arguments() const; - // --- Info + // --- Info void print_available_options(std::ostream& os) const; }; -} +} // namespace util #endif diff --git a/scripts/formatting/Dockerfile.check b/scripts/formatting/Dockerfile.check new file mode 100644 index 000000000..c12a704df --- /dev/null +++ b/scripts/formatting/Dockerfile.check @@ -0,0 +1,32 @@ +FROM ubuntu:24.04 + +USER root + +ENV LOCAL_USER=jovyan \ + LOCAL_UID=1001 \ + HOME=/home/jovyan + +RUN apt-get update \ + && apt-get -qq install -y --no-install-recommends \ + adduser \ + build-essential \ + clang-format-17 \ + && rm -rf /var/lib/apt/lists/* + +RUN adduser \ + --disabled-password \ + --gecos "Default user" \ + --uid ${LOCAL_UID} \ + --home ${HOME} \ + --force-badname \ + ${LOCAL_USER} + +COPY --chown=${LOCAL_USER} --chmod=777 ./scripts /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./src /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./include /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./examples /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./test /home/jovyan/ + +USER ${LOCAL_USER} + +ENTRYPOINT [ "/home/jovyan/scripts/formatting/check-formatting.sh", "/usr/bin/clang-format-17" ] diff --git a/scripts/formatting/Dockerfile.format b/scripts/formatting/Dockerfile.format new file mode 100644 index 000000000..524809a0d --- /dev/null +++ b/scripts/formatting/Dockerfile.format @@ -0,0 +1,32 @@ +FROM ubuntu:24.04 + +USER root + +ENV LOCAL_USER=jovyan \ + LOCAL_UID=1001 \ + HOME=/home/jovyan + +RUN apt-get update \ + && apt-get -qq install -y --no-install-recommends \ + adduser \ + build-essential \ + clang-format-17 \ + && rm -rf /var/lib/apt/lists/* + +RUN adduser \ + --disabled-password \ + --gecos "Default user" \ + --uid ${LOCAL_UID} \ + --home ${HOME} \ + --force-badname \ + ${LOCAL_USER} + +COPY --chown=${LOCAL_USER} --chmod=777 ./scripts /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./src /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./include /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./examples /home/jovyan/ +COPY --chown=${LOCAL_USER} --chmod=666 ./test /home/jovyan/ + +USER ${LOCAL_USER} + +ENTRYPOINT [ "/home/jovyan/scripts/formatting/autoformat.sh", "/usr/bin/clang-format-17" ] diff --git a/scripts/formatting/autoformat-docker.sh b/scripts/formatting/autoformat-docker.sh new file mode 100755 index 000000000..15bea2c36 --- /dev/null +++ b/scripts/formatting/autoformat-docker.sh @@ -0,0 +1,24 @@ +#! /bin/bash + +if [ command -v docker >/dev/null 2>&1 ]; then + echo "Autoformatting with Docker requires Docker to be available" + exit 1 +fi + +set -e + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +curr_dir=$(pwd) + +cd $SCRIPT_DIR +cd .. +cd .. + +echo "Bulding Docker image" +docker build -t caliper-autoformat -f ./scripts/formatting/Dockerfile.format . + +echo "Running Docker container to autoformat code with clang-format 17.0.6" +docker run --rm -v $(pwd):/home/jovyan --name caliper-autoformat-container caliper-autoformat + +cd $curr_dir \ No newline at end of file diff --git a/scripts/formatting/autoformat.sh b/scripts/formatting/autoformat.sh new file mode 100755 index 000000000..84c42956c --- /dev/null +++ b/scripts/formatting/autoformat.sh @@ -0,0 +1,59 @@ +#! /bin/bash + +clang_format_exe="clang-format" +if [ $# -ge 1 ]; then + clang_format_exe="$1" +fi + +SUPPORTED_CLANG_FORMAT_VERSION="17.0.6" + +if [ command -v $clang_format_exe >/dev/null 2>&1 ]; then + echo "You must have 'clang-format' in PATH to use 'autoformat.sh'" + exit 1 +fi + +clang_format_version_str=$($clang_format_exe --version) +clang_format_version=$(echo "$clang_format_version_str" | grep -oP 'clang-format version \K\d+(\.\d+)+') + +if [ "$clang_format_version" != "$SUPPORTED_CLANG_FORMAT_VERSION" ]; then + echo "WARNING: the .clang-format file in this repo is designed for version 16.0.0." + echo " You are running with clang-format v$clang_format_version." + echo " The resulting formatting is highly likely to be incorrect." +fi + +if [ command -v find >/dev/null 2>&1 ]; then + echo "You must have 'find' in PATH to use 'autoformat.sh'" + exit 1 +fi + +if [ command -v dirname >/dev/null 2>&1 ]; then + echo "You must have 'dirname' in PATH to use 'autoformat.sh'" + exit 1 +fi + +if [ command -v xargs >/dev/null 2>&1 ]; then + echo "You must have 'dirname' in PATH to use 'autoformat.sh'" + exit 1 +fi + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +curr_dir=$(pwd) + +cd $SCRIPT_DIR +cd .. +cd .. + +echo "Formatting C/C++ code in 'src'" +find src -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe -i + +echo "Formatting C/C++ code in 'include'" +find include -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe -i + +echo "Formatting C/C++ code in 'examples'" +find examples -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe -i + +echo "Formatting C/C++ code in 'test'" +find test -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe -i + +cd $curr_dir \ No newline at end of file diff --git a/scripts/formatting/check-formatting-docker.sh b/scripts/formatting/check-formatting-docker.sh new file mode 100755 index 000000000..bc3b97b1d --- /dev/null +++ b/scripts/formatting/check-formatting-docker.sh @@ -0,0 +1,24 @@ +#! /bin/bash + +if [ command -v docker >/dev/null 2>&1 ]; then + echo "Formatting check with Docker requires Docker to be available" + exit 1 +fi + +set -e + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +curr_dir=$(pwd) + +cd $SCRIPT_DIR +cd .. +cd .. + +echo "Bulding Docker image" +docker build -t caliper-check-format -f ./scripts/formatting/Dockerfile.check . + +echo "Running Docker container to check formatting of code with clang-format 17.0.6" +docker run --rm -v $(pwd):/home/jovyan --name caliper-check-format-container caliper-check-format + +cd $curr_dir \ No newline at end of file diff --git a/scripts/formatting/check-formatting.sh b/scripts/formatting/check-formatting.sh new file mode 100755 index 000000000..8b65203f9 --- /dev/null +++ b/scripts/formatting/check-formatting.sh @@ -0,0 +1,59 @@ +#! /bin/bash + +clang_format_exe="clang-format" +if [ $# -ge 1 ]; then + clang_format_exe="$1" +fi + +SUPPORTED_CLANG_FORMAT_VERSION="17.0.6" + +if [ command -v $clang_format_exe >/dev/null 2>&1 ]; then + echo "You must have 'clang-format' in PATH to use 'check-formatting.sh'" + exit 1 +fi + +clang_format_version_str=$($clang_format_exe --version) +clang_format_version=$(echo "$clang_format_version_str" | grep -oP 'clang-format version \K\d+(\.\d+)+') + +if [ "$clang_format_version" != "$SUPPORTED_CLANG_FORMAT_VERSION" ]; then + echo "WARNING: the .clang-format file in this repo is designed for version $SUPPORTED_CLANG_FORMAT_VERSION." + echo " You are running with clang-format v$clang_format_version." + echo " The resulting check is highly likely to be incorrect." +fi + +if [ command -v find >/dev/null 2>&1 ]; then + echo "You must have 'find' in PATH to use 'check-formatting.sh'" + exit 1 +fi + +if [ command -v dirname >/dev/null 2>&1 ]; then + echo "You must have 'dirname' in PATH to use 'check-formatting.sh'" + exit 1 +fi + +if [ command -v xargs >/dev/null 2>&1 ]; then + echo "You must have 'dirname' in PATH to use 'check-formatting.sh'" + exit 1 +fi + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +curr_dir=$(pwd) + +cd $SCRIPT_DIR +cd .. +cd .. + +echo "Check formatting of C/C++ code in 'src'" +find src -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe --dry-run -Werror + +echo "Check formatting of C/C++ code in 'include'" +find include -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe --dry-run -Werror + +echo "Check formatting of C/C++ code in 'examples'" +find examples -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe --dry-run -Werror + +echo "Check formatting of C/C++ code in 'test'" +find test -name "*.c" -or -name "*.cpp" -or -name "*.h" -or -name "*.hpp" | xargs $clang_format_exe --dry-run -Werror + +cd $curr_dir \ No newline at end of file diff --git a/src/caliper/Annotation.cpp b/src/caliper/Annotation.cpp index 25cdb3cb2..e0275862f 100644 --- a/src/caliper/Annotation.cpp +++ b/src/caliper/Annotation.cpp @@ -25,7 +25,7 @@ extern Attribute class_iteration_attr; extern Attribute loop_attr; extern Attribute region_attr; -} +} // namespace cali // --- Pre-defined Function annotation class @@ -58,20 +58,22 @@ struct Loop::Impl { std::atomic level; std::atomic refcount; - Impl(const char* name) - : level(0), refcount(1) { + Impl(const char* name) : level(0), refcount(1) + { Variant v_true(true); - iter_attr = - Caliper().create_attribute(std::string("iteration#") + name, - CALI_TYPE_INT, - CALI_ATTR_ASVALUE, - 1, &class_iteration_attr, &v_true); + iter_attr = Caliper().create_attribute( + std::string("iteration#") + name, + CALI_TYPE_INT, + CALI_ATTR_ASVALUE, + 1, + &class_iteration_attr, + &v_true + ); } }; -Loop::Iteration::Iteration(const Impl* p, int i) - : pI(p) +Loop::Iteration::Iteration(const Impl* p, int i) : pI(p) { Caliper().begin(p->iter_attr, Variant(i)); } @@ -81,15 +83,13 @@ Loop::Iteration::~Iteration() Caliper().end(pI->iter_attr); } -Loop::Loop(const char* name) - : pI(new Impl(name)) +Loop::Loop(const char* name) : pI(new Impl(name)) { Caliper().begin(loop_attr, Variant(CALI_TYPE_STRING, name, strlen(name))); ++pI->level; } -Loop::Loop(const Loop& loop) - : pI(loop.pI) +Loop::Loop(const Loop& loop) : pI(loop.pI) { ++pI->refcount; } @@ -102,14 +102,12 @@ Loop::~Loop() } } -Loop::Iteration -Loop::iteration(int i) const +Loop::Iteration Loop::iteration(int i) const { return Iteration(pI, i); } -void -Loop::end() +void Loop::end() { if (pI->level > 0) { Caliper().end(loop_attr); @@ -127,38 +125,30 @@ struct Annotation::Impl { std::vector m_metadata_values; int m_opt; - std::atomic m_refcount; + std::atomic m_refcount; Impl(const std::string& name, MetadataListType metadata, int opt) - : m_attr_node(nullptr), - m_name(name), - m_opt(opt), - m_refcount(1) - { - Caliper c; - Attribute attr = c.get_attribute(name); - - if (!attr) { - for(auto kv : metadata) { - m_metadata_keys.push_back(c.create_attribute(kv.first,kv.second.type(),0)); - m_metadata_values.push_back(kv.second); - } - } else { - m_attr_node.store(attr.node()); + : m_attr_node(nullptr), m_name(name), m_opt(opt), m_refcount(1) + { + Caliper c; + Attribute attr = c.get_attribute(name); + + if (!attr) { + for (auto kv : metadata) { + m_metadata_keys.push_back(c.create_attribute(kv.first, kv.second.type(), 0)); + m_metadata_values.push_back(kv.second); } + } else { + m_attr_node.store(attr.node()); } + } - Impl(const std::string& name, int opt) - : m_attr_node(nullptr), - m_name(name), - m_opt(opt), - m_refcount(1) - { } + Impl(const std::string& name, int opt) : m_attr_node(nullptr), m_name(name), m_opt(opt), m_refcount(1) {} - ~Impl() - { } + ~Impl() {} - void begin(const Variant& data) { + void begin(const Variant& data) + { Caliper c; Attribute attr = get_attribute(c, data.type()); @@ -166,7 +156,8 @@ struct Annotation::Impl { c.begin(attr, data); } - void set(const Variant& data) { + void set(const Variant& data) + { Caliper c; Attribute attr = get_attribute(c, data.type()); @@ -174,23 +165,28 @@ struct Annotation::Impl { c.set(attr, data); } - void end() { - Caliper c; + void end() + { + Caliper c; Attribute attr = Attribute::make_attribute(m_attr_node.load()); if (attr) c.end(attr); } - Attribute get_attribute(Caliper& c, cali_attr_type type) { + Attribute get_attribute(Caliper& c, cali_attr_type type) + { cali::Node* attr_node = m_attr_node.load(); if (!attr_node) { - Attribute attr = - c.create_attribute(m_name, type, m_opt, - m_metadata_keys.size(), - m_metadata_keys.data(), - m_metadata_values.data()); + Attribute attr = c.create_attribute( + m_name, + type, + m_opt, + m_metadata_keys.size(), + m_metadata_keys.data(), + m_metadata_values.data() + ); attr_node = attr.node(); m_attr_node.store(attr_node); @@ -199,12 +195,14 @@ struct Annotation::Impl { return Attribute::make_attribute(attr_node); } - Impl* attach() { + Impl* attach() + { ++m_refcount; return this; } - void detach() { + void detach() + { --m_refcount; if (m_refcount == 0) @@ -212,12 +210,10 @@ struct Annotation::Impl { } }; - // --- Guard subclass -Annotation::Guard::Guard(Annotation& a) - : pI(a.pI->attach()) -{ } +Annotation::Guard::Guard(Annotation& a) : pI(a.pI->attach()) +{} Annotation::Guard::~Guard() { @@ -226,24 +222,21 @@ Annotation::Guard::~Guard() } // --- Constructors / destructor -Annotation::Annotation(const char* name, const MetadataListType& metadata, int opt) - : pI(new Impl(name, metadata, opt)) -{ } +Annotation::Annotation(const char* name, const MetadataListType& metadata, int opt) : pI(new Impl(name, metadata, opt)) +{} -Annotation::Annotation(const char* name, int opt) - : pI(new Impl(name, opt)) -{ } +Annotation::Annotation(const char* name, int opt) : pI(new Impl(name, opt)) +{} Annotation::~Annotation() { pI->detach(); } -Annotation::Annotation(const Annotation& a) - : pI(a.pI->attach()) -{ } +Annotation::Annotation(const Annotation& a) : pI(a.pI->attach()) +{} -Annotation& Annotation::operator = (const Annotation& a) +Annotation& Annotation::operator= (const Annotation& a) { if (pI == a.pI) return *this; @@ -254,7 +247,6 @@ Annotation& Annotation::operator = (const Annotation& a) return *this; } - // --- begin() overloads Annotation& Annotation::begin() diff --git a/src/caliper/AnnotationBinding.cpp b/src/caliper/AnnotationBinding.cpp index 619f00ca9..f8e707d34 100644 --- a/src/caliper/AnnotationBinding.cpp +++ b/src/caliper/AnnotationBinding.cpp @@ -18,24 +18,31 @@ using namespace cali; // const cali::ConfigSet::Entry AnnotationBinding::s_configdata[] = { - { "include_regions", CALI_TYPE_STRING, "", - "Region filter specifying regions to include", - "Region filter specifying regions to include", + { + "include_regions", + CALI_TYPE_STRING, + "", + "Region filter specifying regions to include", + "Region filter specifying regions to include", }, - { "exclude_regions", CALI_TYPE_STRING, "", - "Region filter specifying regions to exclude", - "Region filter specifying regions to exclude", + { + "exclude_regions", + CALI_TYPE_STRING, + "", + "Region filter specifying regions to exclude", + "Region filter specifying regions to exclude", }, - { "trigger_attributes", CALI_TYPE_STRING, "", + { "trigger_attributes", + CALI_TYPE_STRING, + "", "List of attributes that trigger the annotation binding", - "List of attributes that trigger the annotation binding" - }, + "List of attributes that trigger the annotation binding" }, cali::ConfigSet::Terminator }; namespace cali { - extern Attribute subscription_event_attr; +extern Attribute subscription_event_attr; } namespace @@ -52,23 +59,21 @@ bool has_marker(const Attribute& attr, const Attribute& marker_attr) return false; } -} +} // namespace // // --- Implementation // -bool -AnnotationBinding::is_subscription_attribute(const Attribute& attr) +bool AnnotationBinding::is_subscription_attribute(const Attribute& attr) { return attr.get(subscription_event_attr).to_bool(); } AnnotationBinding::~AnnotationBinding() -{ } +{} -void -AnnotationBinding::mark_attribute(Caliper *c, Channel* chn, const Attribute& attr) +void AnnotationBinding::mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) { // Add the binding marker for this attribute @@ -79,13 +84,11 @@ AnnotationBinding::mark_attribute(Caliper *c, Channel* chn, const Attribute& att on_mark_attribute(c, chn, attr); - Log(2).stream() << "Adding " << this->service_tag() - << " bindings for attribute \"" << attr.name() - << "\" in " << chn->name() << " channel" << std::endl; + Log(2).stream() << "Adding " << this->service_tag() << " bindings for attribute \"" << attr.name() << "\" in " + << chn->name() << " channel" << std::endl; } -void -AnnotationBinding::check_attribute(Caliper* c, Channel* chn, const Attribute& attr) +void AnnotationBinding::check_attribute(Caliper* c, Channel* chn, const Attribute& attr) { int prop = attr.properties(); @@ -97,16 +100,15 @@ AnnotationBinding::check_attribute(Caliper* c, Channel* chn, const Attribute& at if (!(prop & CALI_ATTR_NESTED)) return; } else { - if (std::find(m_trigger_attr_names.begin(), m_trigger_attr_names.end(), - attr.name()) == m_trigger_attr_names.end()) + if (std::find(m_trigger_attr_names.begin(), m_trigger_attr_names.end(), attr.name()) + == m_trigger_attr_names.end()) return; } mark_attribute(c, chn, attr); } -void -AnnotationBinding::begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) +void AnnotationBinding::begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { if (!::has_marker(attr, m_marker_attr)) return; @@ -116,8 +118,7 @@ AnnotationBinding::begin_cb(Caliper* c, Channel* chn, const Attribute& attr, con this->on_begin(c, chn, attr, value); } -void -AnnotationBinding::end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) +void AnnotationBinding::end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { if (!::has_marker(attr, m_marker_attr)) return; @@ -127,46 +128,37 @@ AnnotationBinding::end_cb(Caliper* c, Channel* chn, const Attribute& attr, const this->on_end(c, chn, attr, value); } -void -AnnotationBinding::base_pre_initialize(Caliper* c, Channel* chn) +void AnnotationBinding::base_pre_initialize(Caliper* c, Channel* chn) { - const char* tag = service_tag(); + const char* tag = service_tag(); std::string cfgname = std::string(tag) + "_binding"; m_config = chn->config().init(cfgname.c_str(), s_configdata); { - std::string i_filter = - m_config.get("include_regions").to_string(); - std::string e_filter = - m_config.get("exclude_regions").to_string(); + std::string i_filter = m_config.get("include_regions").to_string(); + std::string e_filter = m_config.get("exclude_regions").to_string(); auto p = RegionFilter::from_config(i_filter, e_filter); if (!p.second.empty()) { - Log(0).stream() << chn->name() << ": event: filter parse error: " - << p.second - << std::endl; + Log(0).stream() << chn->name() << ": event: filter parse error: " << p.second << std::endl; } else if (p.first.has_filters()) { m_filter = std::unique_ptr(new RegionFilter(std::move(p.first))); } } - m_trigger_attr_names = - m_config.get("trigger_attributes").to_stringlist(",:"); + m_trigger_attr_names = m_config.get("trigger_attributes").to_stringlist(",:"); std::string marker_attr_name("cali.binding."); marker_attr_name.append(tag); marker_attr_name.append("#"); marker_attr_name.append(std::to_string(chn->id())); - m_marker_attr = - c->create_attribute(marker_attr_name, CALI_TYPE_BOOL, - CALI_ATTR_HIDDEN | CALI_ATTR_SKIP_EVENTS); + m_marker_attr = c->create_attribute(marker_attr_name, CALI_TYPE_BOOL, CALI_ATTR_HIDDEN | CALI_ATTR_SKIP_EVENTS); } -void -AnnotationBinding::base_post_initialize(Caliper* c, Channel* chn) +void AnnotationBinding::base_post_initialize(Caliper* c, Channel* chn) { // check and mark existing attributes @@ -178,4 +170,4 @@ AnnotationBinding::base_post_initialize(Caliper* c, Channel* chn) } AnnotationBinding::AnnotationBinding() -{ } \ No newline at end of file +{} \ No newline at end of file diff --git a/src/caliper/Blackboard.cpp b/src/caliper/Blackboard.cpp index eeb5b2ac7..b3425e219 100644 --- a/src/caliper/Blackboard.cpp +++ b/src/caliper/Blackboard.cpp @@ -10,36 +10,39 @@ #ifdef _WIN32 #include #pragma intrinsic(_BitScanForward) -namespace { + +namespace +{ int first_high_bit(int x) { unsigned long const mask = static_cast(x); - unsigned long index; - unsigned char is_non_zero = _BitScanForward(&index, mask); + unsigned long index; + unsigned char is_non_zero = _BitScanForward(&index, mask); if (is_non_zero) { return index + 1; } return 0; } -} +} // namespace #else #include -namespace { + +namespace +{ int first_high_bit(int x) { return ffs(x); } -} +} // namespace #endif using namespace cali; -void -Blackboard::add(cali_id_t key, const Entry& value, bool include_in_snapshots) +void Blackboard::add(cali_id_t key, const Entry& value, bool include_in_snapshots) { size_t I = find_free_slot(key); - if (num_entries + (Nmax/10+10) > Nmax) { + if (num_entries + (Nmax / 10 + 10) > Nmax) { ++num_skipped; // Uh oh, we're full return; } @@ -48,19 +51,17 @@ Blackboard::add(cali_id_t key, const Entry& value, bool include_in_snapshots) hashtable[I].value = value; if (include_in_snapshots) { - toc[I/32] |= (1 << (I%32)); - toctoc |= (1 << (I/32)); + toc[I / 32] |= (1 << (I % 32)); + toctoc |= (1 << (I / 32)); } ++num_entries; max_num_entries = std::max(num_entries, max_num_entries); } -void -Blackboard::set(cali_id_t key, const Entry& value, bool include_in_snapshots) +void Blackboard::set(cali_id_t key, const Entry& value, bool include_in_snapshots) { - std::lock_guard - g(lock); + std::lock_guard g(lock); size_t I = find_existing_entry(key); @@ -72,11 +73,9 @@ Blackboard::set(cali_id_t key, const Entry& value, bool include_in_snapshots) ++ucount; } -void -Blackboard::del(cali_id_t key) +void Blackboard::del(cali_id_t key) { - std::lock_guard - g(lock); + std::lock_guard g(lock); size_t I = find_existing_entry(key); @@ -86,13 +85,13 @@ Blackboard::del(cali_id_t key) { size_t j = I; while (true) { - j = (j+1) % Nmax; + j = (j + 1) % Nmax; if (hashtable[j].key == CALI_INV_ID) break; size_t k = hashtable[j].key % Nmax; if ((j > I && (k <= I || k > j)) || (j < I && (k <= I && k > j))) { hashtable[I] = hashtable[j]; - I = j; + I = j; } } } @@ -103,22 +102,20 @@ Blackboard::del(cali_id_t key) --num_entries; ++ucount; - toc[I/32] &= ~(1 << (I%32)); - if (toc[I/32] == 0) - toctoc &= ~(1 << (I/32)); + toc[I / 32] &= ~(1 << (I % 32)); + if (toc[I / 32] == 0) + toctoc &= ~(1 << (I / 32)); } -Entry -Blackboard::exchange(cali_id_t key, const Entry& value, bool include_in_snapshots) +Entry Blackboard::exchange(cali_id_t key, const Entry& value, bool include_in_snapshots) { - std::lock_guard - g(lock); + std::lock_guard g(lock); - size_t I = find_existing_entry(key); - Entry ret; + size_t I = find_existing_entry(key); + Entry ret; if (hashtable[I].key == key) { - ret = hashtable[I].value; + ret = hashtable[I].value; hashtable[I].value = value; } else add(key, value, include_in_snapshots); @@ -128,11 +125,9 @@ Blackboard::exchange(cali_id_t key, const Entry& value, bool include_in_snapshot return ret; } -void -Blackboard::snapshot(SnapshotBuilder& rec) const +void Blackboard::snapshot(SnapshotBuilder& rec) const { - std::lock_guard - g(lock); + std::lock_guard g(lock); int tmptoc = toctoc; @@ -145,16 +140,14 @@ Blackboard::snapshot(SnapshotBuilder& rec) const int j = first_high_bit(tmp) - 1; tmp &= ~(1 << j); - rec.append(hashtable[i*32+j].value); + rec.append(hashtable[i * 32 + j].value); } } } -std::ostream& -Blackboard::print_statistics(std::ostream& os) const +std::ostream& Blackboard::print_statistics(std::ostream& os) const { - os << "max " << max_num_entries - << " entries (" << 100.0*max_num_entries/Nmax << "% occupancy)."; + os << "max " << max_num_entries << " entries (" << 100.0 * max_num_entries / Nmax << "% occupancy)."; if (num_skipped > 0) os << " " << num_skipped << " entries skipped!"; diff --git a/src/caliper/Blackboard.h b/src/caliper/Blackboard.h index 087812c11..32ebfc98b 100644 --- a/src/caliper/Blackboard.h +++ b/src/caliper/Blackboard.h @@ -25,12 +25,13 @@ namespace cali class SnapshotBuilder; -class Blackboard { +class Blackboard +{ constexpr static size_t Nmax = 1021; struct blackboard_entry_t { - cali_id_t key { CALI_INV_ID }; - Entry value { }; + cali_id_t key { CALI_INV_ID }; + Entry value {}; }; blackboard_entry_t hashtable[Nmax]; @@ -40,32 +41,34 @@ class Blackboard { // it to speed up iterating over all entries in snapshot(). // Similarly, toctoc indicates which elements in toc are // occupied. - int toc[(Nmax+31)/32]; - int toctoc; + int toc[(Nmax + 31) / 32]; + int toctoc; - size_t num_entries; - size_t max_num_entries; + size_t num_entries; + size_t max_num_entries; - size_t num_skipped; + size_t num_skipped; - std::atomic ucount; // update count + std::atomic ucount; // update count mutable util::spinlock lock; - inline size_t find_existing_entry(cali_id_t key) const { + inline size_t find_existing_entry(cali_id_t key) const + { size_t I = key % Nmax; while (hashtable[I].key != key && hashtable[I].key != CALI_INV_ID) - I = (I+1) % Nmax; + I = (I + 1) % Nmax; return I; } - inline size_t find_free_slot(cali_id_t key) const { + inline size_t find_free_slot(cali_id_t key) const + { size_t I = key % Nmax; while (hashtable[I].key != CALI_INV_ID) - I = (I+1) % Nmax; + I = (I + 1) % Nmax; return I; } @@ -75,19 +78,18 @@ class Blackboard { public: Blackboard() - : hashtable { }, - toc { 0 }, - toctoc { 0 }, - num_entries { 0 }, + : hashtable {}, + toc { 0 }, + toctoc { 0 }, + num_entries { 0 }, max_num_entries { 0 }, - num_skipped { 0 }, - ucount { 0 } - { } + num_skipped { 0 }, + ucount { 0 } + {} - inline Entry - get(cali_id_t key) const { - std::lock_guard - g(lock); + inline Entry get(cali_id_t key) const + { + std::lock_guard g(lock); size_t I = find_existing_entry(key); @@ -97,16 +99,16 @@ class Blackboard { return hashtable[I].value; } - void set(cali_id_t key, const Entry& value, bool include_in_snapshots); - void del(cali_id_t key); + void set(cali_id_t key, const Entry& value, bool include_in_snapshots); + void del(cali_id_t key); - Entry exchange(cali_id_t key, const Entry& value, bool include_in_snapshots); + Entry exchange(cali_id_t key, const Entry& value, bool include_in_snapshots); - void snapshot(SnapshotBuilder& rec) const; + void snapshot(SnapshotBuilder& rec) const; - size_t num_skipped_entries() const { return num_skipped; } + size_t num_skipped_entries() const { return num_skipped; } - int count() const { return ucount.load(); } + int count() const { return ucount.load(); } std::ostream& print_statistics(std::ostream& os) const; }; diff --git a/src/caliper/Caliper.cpp b/src/caliper/Caliper.cpp index d354cc905..ec22cf89d 100644 --- a/src/caliper/Caliper.cpp +++ b/src/caliper/Caliper.cpp @@ -59,42 +59,37 @@ extern void init_builtin_configmanager(Caliper* c); } -} +} // namespace cali namespace { // --- Siglock -class siglock { +class siglock +{ volatile sig_atomic_t m_lock; public: - siglock() - : m_lock(0) - { } + siglock() : m_lock(0) {} + + inline void lock() { ++m_lock; } - inline void lock() { ++m_lock; } inline void unlock() { --m_lock; } - inline bool is_locked() const { - return (m_lock > 0); - } + inline bool is_locked() const { return (m_lock > 0); } }; // --- helper functions -void -log_invalid_cfg_value(const char* var, const char* value, const char* prefix = nullptr) +void log_invalid_cfg_value(const char* var, const char* value, const char* prefix = nullptr) { - Log(0).stream() << (prefix ? std::string(prefix)+": " : std::string("")) - << "Invalid value \"" << value << "\" for " << var - << std::endl; + Log(0).stream() << (prefix ? std::string(prefix) + ": " : std::string("")) << "Invalid value \"" << value + << "\" for " << var << std::endl; } -std::ostream& -print_available_services(std::ostream& os) +std::ostream& print_available_services(std::ostream& os) { std::vector services = services::get_available_services(); @@ -108,13 +103,12 @@ print_available_services(std::ostream& os) return os; } -std::vector -get_globals_from_blackboard(Caliper* c, const Blackboard& blackboard) +std::vector get_globals_from_blackboard(Caliper* c, const Blackboard& blackboard) { FixedSizeSnapshotRecord rec; blackboard.snapshot(rec.builder()); - std::vector ret; + std::vector ret; std::vector nodes; for (const Entry& e : rec.view()) { @@ -139,12 +133,9 @@ void make_default_channel() { // Creates default channel (which reads env vars and/or caliper.config) // and initializes builtin ConfigManager during initialization - RuntimeConfig cfg = RuntimeConfig::get_default_config(); - const RuntimeConfig::config_entry_list_t configdata { - { "enable", "" } - }; - std::vector services = - cfg.init("services", configdata).get("enable").to_stringlist(",:"); + RuntimeConfig cfg = RuntimeConfig::get_default_config(); + const RuntimeConfig::config_entry_list_t configdata { { "enable", "" } }; + std::vector services = cfg.init("services", configdata).get("enable").to_stringlist(",:"); Caliper c; @@ -158,97 +149,84 @@ void make_default_channel() internal::init_builtin_configmanager(&c); } -} // namespace [anonymous] +} // namespace // // Caliper channel data // -struct Channel::ChannelImpl -{ - static const ConfigSet::Entry s_configdata[]; +struct Channel::ChannelImpl { + static const ConfigSet::Entry s_configdata[]; - cali_id_t id; - std::string name; - bool is_active; + cali_id_t id; + std::string name; + bool is_active; - RuntimeConfig config; - Events events; ///< callbacks + RuntimeConfig config; + Events events; ///< callbacks - bool flush_on_exit; + bool flush_on_exit; - Blackboard channel_blackboard; + Blackboard channel_blackboard; ChannelImpl(cali_id_t _id, const char* _name, const RuntimeConfig& cfg) - : id(_id), - name(_name), - is_active(false), - config(cfg) - { - ConfigSet cali_cfg = - config.init("channel", s_configdata); + : id(_id), name(_name), is_active(false), config(cfg) + { + ConfigSet cali_cfg = config.init("channel", s_configdata); - flush_on_exit = - cali_cfg.get("flush_on_exit").to_bool(); - } + flush_on_exit = cali_cfg.get("flush_on_exit").to_bool(); + } ~ChannelImpl() - { - if (Log::verbosity() >= 2) { - channel_blackboard.print_statistics( Log(2).stream() << name << " channel blackboard: " ) - << std::endl; - } + { + if (Log::verbosity() >= 2) { + channel_blackboard.print_statistics(Log(2).stream() << name << " channel blackboard: ") << std::endl; } + } }; const ConfigSet::Entry Channel::ChannelImpl::s_configdata[] = { // key, type, value, short description, long description - { "config_check", CALI_TYPE_BOOL, "true", + { "config_check", + CALI_TYPE_BOOL, + "true", "Perform configuration sanity check at initialization", - "Perform configuration sanity check at initialization" - }, - { "flush_on_exit", CALI_TYPE_BOOL, "true", + "Perform configuration sanity check at initialization" }, + { "flush_on_exit", + CALI_TYPE_BOOL, + "true", "Flush Caliper buffers at program exit", - "Flush Caliper buffers at program exit" - }, + "Flush Caliper buffers at program exit" }, ConfigSet::Terminator }; -Channel::Channel(cali_id_t id, const char* name, const RuntimeConfig& cfg) - : mP(new ChannelImpl(id, name, cfg)) -{ -} +Channel::Channel(cali_id_t id, const char* name, const RuntimeConfig& cfg) : mP(new ChannelImpl(id, name, cfg)) +{} Channel::~Channel() -{ -} +{} -Channel::Events& -Channel::events() +Channel::Events& Channel::events() { return mP->events; } -RuntimeConfig -Channel::config() +RuntimeConfig Channel::config() { return mP->config; } -std::string -Channel::name() const +std::string Channel::name() const { return mP->name; } -bool -Channel::is_active() const +bool Channel::is_active() const { return mP && mP->is_active; } -cali_id_t -Channel::id() const +cali_id_t Channel::id() const { return mP->id; } @@ -258,42 +236,40 @@ Channel::id() const // /// \brief Per-thread data for the Caliper object -struct Caliper::ThreadData -{ - MetadataTree tree; - ::siglock lock; +struct Caliper::ThreadData { + MetadataTree tree; + ::siglock lock; SnapshotRecord snapshot; // This thread's blackboard - Blackboard thread_blackboard; + Blackboard thread_blackboard; // copy of the last process blackboard snapshot SnapshotRecord process_snapshot; // version of the last process blackboard snapshot - int process_bb_count; + int process_bb_count; - bool is_initial_thread; - bool stack_error; + bool is_initial_thread; + bool stack_error; ThreadData(bool initial_thread = false) - : process_bb_count(-1), - is_initial_thread(initial_thread), - stack_error(false) - { } + : process_bb_count(-1), is_initial_thread(initial_thread), stack_error(false) + {} - ~ThreadData() { + ~ThreadData() + { if (Log::verbosity() >= 2) - print_detailed_stats( Log(2).stream() ); + print_detailed_stats(Log(2).stream()); } - void print_detailed_stats(std::ostream& os) { - tree.print_statistics( os << "Releasing Caliper thread data: \n" ) - << std::endl; - thread_blackboard.print_statistics( os << " Thread blackboard: " ) - << std::endl; + void print_detailed_stats(std::ostream& os) + { + tree.print_statistics(os << "Releasing Caliper thread data: \n") << std::endl; + thread_blackboard.print_statistics(os << " Thread blackboard: ") << std::endl; } - void update_process_snapshot(Blackboard& process_blackboard) { + void update_process_snapshot(Blackboard& process_blackboard) + { // Check if the process or channel blackboards have been updated // since the last snapshot on this thread. // We keep a copy of the last process/channel snapshot data in our @@ -310,76 +286,66 @@ struct Caliper::ThreadData } }; - // // --- Caliper Global Data // -struct Caliper::GlobalData -{ +struct Caliper::GlobalData { // --- static data - static volatile sig_atomic_t s_init_lock; - static std::mutex s_init_mutex; + static volatile sig_atomic_t s_init_lock; + static std::mutex s_init_mutex; - static const ConfigSet::Entry s_configdata[]; + static const ConfigSet::Entry s_configdata[]; // --- data - mutable std::mutex attribute_lock; - map attribute_map; + mutable std::mutex attribute_lock; + map attribute_map; - map attribute_prop_presets; - int attribute_default_scope; + map attribute_prop_presets; + int attribute_default_scope; - Blackboard process_blackboard; + Blackboard process_blackboard; - vector< Channel > all_channels; - vector< Channel > active_channels; + vector all_channels; + vector active_channels; - size_t max_active_channels; + size_t max_active_channels; - vector< ThreadData* > thread_data; - std::mutex thread_data_lock; + vector thread_data; + std::mutex thread_data_lock; // --- constructor - GlobalData(ThreadData* sT) - : attribute_default_scope { CALI_ATTR_SCOPE_THREAD }, - max_active_channels { 0 } + GlobalData(ThreadData* sT) : attribute_default_scope { CALI_ATTR_SCOPE_THREAD }, max_active_channels { 0 } { // put the attribute [name,type,prop] attributes in the map - Attribute name_attr = - Attribute::make_attribute(sT->tree.node(Attribute::NAME_ATTR_ID)); - Attribute type_attr = - Attribute::make_attribute(sT->tree.node(Attribute::TYPE_ATTR_ID)); - Attribute prop_attr = - Attribute::make_attribute(sT->tree.node(Attribute::PROP_ATTR_ID)); + Attribute name_attr = Attribute::make_attribute(sT->tree.node(Attribute::NAME_ATTR_ID)); + Attribute type_attr = Attribute::make_attribute(sT->tree.node(Attribute::TYPE_ATTR_ID)); + Attribute prop_attr = Attribute::make_attribute(sT->tree.node(Attribute::PROP_ATTR_ID)); attribute_map.insert(make_pair(name_attr.name(), name_attr)); attribute_map.insert(make_pair(prop_attr.name(), prop_attr)); attribute_map.insert(make_pair(type_attr.name(), type_attr)); } - ~GlobalData() { + ~GlobalData() + { // prevent re-initialization s_init_lock = 2; if (Log::verbosity() >= 2) { Log(2).stream() << "Releasing Caliper global data.\n" - << " Max active channels: " << max_active_channels << std::endl; - process_blackboard.print_statistics( Log(2).stream() << "Process blackboard: " ) - << std::endl; + << " Max active channels: " << max_active_channels << std::endl; + process_blackboard.print_statistics(Log(2).stream() << "Process blackboard: ") << std::endl; } { - std::lock_guard - g(thread_data_lock); + std::lock_guard g(thread_data_lock); - std::for_each(thread_data.begin(), thread_data.end(), [](ThreadData* d){ - delete d; - }); + std::for_each(thread_data.begin(), thread_data.end(), [](ThreadData* d) { delete d; }); thread_data.clear(); } @@ -392,7 +358,8 @@ struct Caliper::GlobalData Log::fini(); } - void parse_attribute_config(const ConfigSet& config) { + void parse_attribute_config(const ConfigSet& config) + { auto preset_cfg = config.get("attribute_properties").to_stringlist(); for (const string& s : preset_cfg) { @@ -401,7 +368,7 @@ struct Caliper::GlobalData if (p == string::npos) continue; - int prop = cali_string2prop(s.substr(p+1).c_str()); + int prop = cali_string2prop(s.substr(p + 1).c_str()); attribute_prop_presets.insert(make_pair(s.substr(0, p), prop)); } @@ -416,31 +383,33 @@ struct Caliper::GlobalData log_invalid_cfg_value("CALI_CALIPER_ATTRIBUTE_DEFAULT_SCOPE", scope_str.c_str()); } - void init() { + void init() + { init_submodules(); parse_attribute_config(RuntimeConfig::get_default_config().init("caliper", s_configdata)); if (Log::verbosity() >= 2) - print_available_services( Log(2).stream() << "Available services: " ) << std::endl; + print_available_services(Log(2).stream() << "Available services: ") << std::endl; Caliper c(this, tObj.t_ptr, false); init_attribute_classes(&c); init_api_attributes(&c); - c.set(c.create_attribute("cali.caliper.version", CALI_TYPE_STRING, - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_GLOBAL), - Variant(CALIPER_VERSION)); + c.set( + c.create_attribute("cali.caliper.version", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_GLOBAL), + Variant(CALIPER_VERSION) + ); Log(1).stream() << "Initialized" << std::endl; } - ThreadData* add_thread_data(ThreadData* t) { + ThreadData* add_thread_data(ThreadData* t) + { tObj.t_ptr = t; - std::lock_guard - g(thread_data_lock); + std::lock_guard g(thread_data_lock); thread_data.push_back(t); return t; @@ -457,11 +426,10 @@ struct Caliper::GlobalData struct S_TLSObject { ThreadData* t_ptr; - S_TLSObject() - : t_ptr(nullptr) - { } + S_TLSObject() : t_ptr(nullptr) {} - ~S_TLSObject() { + ~S_TLSObject() + { // Only use if we're still active if (t_ptr && s_init_lock == 0) { Caliper c(gObj.g_ptr, t_ptr, false); @@ -489,7 +457,8 @@ struct Caliper::GlobalData // : g_ptr(nullptr) // { } - ~S_GObject() { + ~S_GObject() + { // Only use if we're still active if (g_ptr && s_init_lock == 0) { Caliper c(g_ptr, tObj.t_ptr, false); @@ -508,15 +477,17 @@ struct Caliper::GlobalData // --- static member initialization -volatile sig_atomic_t Caliper::GlobalData::s_init_lock = 1; -mutex Caliper::GlobalData::s_init_mutex; +volatile sig_atomic_t Caliper::GlobalData::s_init_lock = 1; +mutex Caliper::GlobalData::s_init_mutex; -Caliper::GlobalData::S_GObject Caliper::GlobalData::gObj; +Caliper::GlobalData::S_GObject Caliper::GlobalData::gObj; thread_local Caliper::GlobalData::S_TLSObject Caliper::GlobalData::tObj; const ConfigSet::Entry Caliper::GlobalData::s_configdata[] = { // key, type, value, short description, long description - { "attribute_properties", CALI_TYPE_STRING, "", + { "attribute_properties", + CALI_TYPE_STRING, + "", "List of attribute property presets", "List of attribute property presets, in the form\n" " attr=prop1:prop2,attr2=prop1:prop2:prop3,attr3=prop1,...\n" @@ -528,14 +499,14 @@ const ConfigSet::Entry Caliper::GlobalData::s_configdata[] = { " task_scope: Task-scope attribute (currently not supported)\n" " skip_events: Do not invoke callback functions for updates\n" " hidden: Do not include this attribute in snapshots\n" - " nested: Values are properly nested with the call stack and other nested attributes\n" - }, - { "attribute_default_scope", CALI_TYPE_STRING, "thread", + " nested: Values are properly nested with the call stack and other nested attributes\n" }, + { "attribute_default_scope", + CALI_TYPE_STRING, + "thread", "Default scope for attributes", "Default scope for attributes. Possible values are\n" " process: Process scope\n" - " thread: Thread scope" - }, + " thread: Thread scope" }, ConfigSet::Terminator }; @@ -543,7 +514,7 @@ const ConfigSet::Entry Caliper::GlobalData::s_configdata[] = { namespace { -constexpr cali_id_t REGION_KEY { 1 }; +constexpr cali_id_t REGION_KEY { 1 }; constexpr cali_id_t UNALIGNED_KEY { 2 }; // Get the blackboard key, which determines the blackboard slot for each @@ -552,8 +523,7 @@ constexpr cali_id_t UNALIGNED_KEY { 2 }; // set-type attributes. We skip stack nesting checks for unaligned // attributes. Immediate (as_value) and nomerge attributes get // their own slots. -inline cali_id_t -get_blackboard_key(cali_id_t attr_id, int prop) +inline cali_id_t get_blackboard_key(cali_id_t attr_id, int prop) { if (prop & CALI_ATTR_ASVALUE) return attr_id; @@ -563,23 +533,19 @@ get_blackboard_key(cali_id_t attr_id, int prop) return REGION_KEY; } -inline cali_id_t -get_blackboard_key_for_reference_entry(int prop) +inline cali_id_t get_blackboard_key_for_reference_entry(int prop) { return prop & CALI_ATTR_UNALIGNED ? UNALIGNED_KEY : REGION_KEY; } - -void -log_stack_error(const Node* stack, const Attribute& attr) +void log_stack_error(const Node* stack, const Attribute& attr) { std::string stackstr; std::string helpstr; if (stack) { - stackstr = - "\n but current region is\n \""; -/* + stackstr = "\n but current region is\n \""; + /* const Node* attr_node = tree.node(stack->attribute()); if (attr_node) @@ -593,18 +559,13 @@ log_stack_error(const Node* stack, const Attribute& attr) "\n Run program with CALI_SERVICES_ENABLE=validator to examine nesting errors, or" "\n run with CALI_CALIPER_ALLOW_REGION_OVERLAP=true to continue region tracking."; } else - stackstr = - "\n but region stack is empty!"; + stackstr = "\n but region stack is empty!"; - Log(0).stream() << "Region stack mismatch: Trying to end\n \"" << attr.name() << "\"" - << stackstr - << "\n Ceasing region tracking!" - << helpstr - << std::endl; + Log(0).stream() << "Region stack mismatch: Trying to end\n \"" << attr.name() << "\"" << stackstr + << "\n Ceasing region tracking!" << helpstr << std::endl; } -void -log_stack_value_error(const Entry& current, Attribute attr, const Variant& expect) +void log_stack_value_error(const Entry& current, Attribute attr, const Variant& expect) { std::string error; if (current.empty()) @@ -616,23 +577,19 @@ log_stack_value_error(const Entry& current, Attribute attr, const Variant& expec error.append(current.value().to_string()); } - Log(0).stream() << "Stack value mismatch: Trying to end " - << attr.name() << "=" << expect.to_string() - << " but " << error - << std::endl; + Log(0).stream() << "Stack value mismatch: Trying to end " << attr.name() << "=" << expect.to_string() << " but " + << error << std::endl; } -struct BlackboardEntry -{ +struct BlackboardEntry { Entry merged_entry; Entry entry; }; -inline BlackboardEntry -load_current_entry(const Attribute& attr, cali_id_t key, Blackboard& blackboard) +inline BlackboardEntry load_current_entry(const Attribute& attr, cali_id_t key, Blackboard& blackboard) { Entry merged_entry = blackboard.get(key); - Entry entry = merged_entry.get(attr); + Entry entry = merged_entry.get(attr); if (merged_entry.attribute() != attr.id()) { if (entry.empty()) { @@ -648,20 +605,31 @@ load_current_entry(const Attribute& attr, cali_id_t key, Blackboard& blackboard) return { merged_entry, entry }; } -inline void -handle_begin(const Attribute& attr, const Variant& value, int prop, Blackboard& blackboard, MetadataTree& tree) +inline void handle_begin( + const Attribute& attr, + const Variant& value, + int prop, + Blackboard& blackboard, + MetadataTree& tree +) { if (prop & CALI_ATTR_ASVALUE) { blackboard.set(attr.id(), Entry(attr, value), !(prop & CALI_ATTR_HIDDEN)); } else { - cali_id_t key = get_blackboard_key_for_reference_entry(prop); - Entry entry = Entry(tree.get_child(attr, value, blackboard.get(key).node())); + cali_id_t key = get_blackboard_key_for_reference_entry(prop); + Entry entry = Entry(tree.get_child(attr, value, blackboard.get(key).node())); blackboard.set(key, entry, !(prop & CALI_ATTR_HIDDEN)); } } -inline void -handle_end(const Attribute& attr, int prop, const BlackboardEntry& current, cali_id_t key, Blackboard& blackboard, MetadataTree& tree) +inline void handle_end( + const Attribute& attr, + int prop, + const BlackboardEntry& current, + cali_id_t key, + Blackboard& blackboard, + MetadataTree& tree +) { if (prop & CALI_ATTR_ASVALUE) blackboard.del(key); @@ -679,20 +647,24 @@ handle_end(const Attribute& attr, int prop, const BlackboardEntry& current, cali } } -inline void -handle_set(const Attribute& attr, const Variant& value, int prop, Blackboard& blackboard, MetadataTree& tree) +inline void handle_set( + const Attribute& attr, + const Variant& value, + int prop, + Blackboard& blackboard, + MetadataTree& tree +) { if (prop & CALI_ATTR_ASVALUE) blackboard.set(attr.id(), Entry(attr, value), !(prop & CALI_ATTR_HIDDEN)); else { - cali_id_t key = get_blackboard_key_for_reference_entry(prop); - Node* node = blackboard.get(key).node(); + cali_id_t key = get_blackboard_key_for_reference_entry(prop); + Node* node = blackboard.get(key).node(); blackboard.set(key, tree.replace_first_in_path(node, attr, value), !(prop & CALI_ATTR_HIDDEN)); } } -} // namespace [anonymous] - +} // namespace // // Caliper class definition @@ -700,19 +672,22 @@ handle_set(const Attribute& attr, const Variant& value, int prop, Blackboard& bl // --- Attribute interface -Attribute -Caliper::create_attribute(const std::string& name, cali_attr_type type, int prop, - int n_meta, const Attribute* meta_attr, const Variant* meta_val) +Attribute Caliper::create_attribute( + const std::string& name, + cali_attr_type type, + int prop, + int n_meta, + const Attribute* meta_attr, + const Variant* meta_val +) { assert(sG != 0); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); // Check if an attribute with this name already exists { - std::lock_guard - ga(sG->attribute_lock); + std::lock_guard ga(sG->attribute_lock); auto it = sG->attribute_map.find(name); if (it != sG->attribute_map.end()) @@ -761,8 +736,7 @@ Caliper::create_attribute(const std::string& name, cali_attr_type type, int prop // another thread in the meantime. // We've created some redundant nodes then, but that's fine - std::lock_guard - ga(sG->attribute_lock); + std::lock_guard ga(sG->attribute_lock); auto it = sG->attribute_map.lower_bound(name); @@ -782,45 +756,35 @@ Caliper::create_attribute(const std::string& name, cali_attr_type type, int prop return attr; } -bool -Caliper::attribute_exists(const std::string& name) const +bool Caliper::attribute_exists(const std::string& name) const { - std::lock_guard<::siglock> - gs(sT->lock); - std::lock_guard - ga(sG->attribute_lock); + std::lock_guard<::siglock> gs(sT->lock); + std::lock_guard ga(sG->attribute_lock); return (sG->attribute_map.find(name) != sG->attribute_map.end()); } -Attribute -Caliper::get_attribute(const std::string& name) const +Attribute Caliper::get_attribute(const std::string& name) const { - std::lock_guard<::siglock> - gs(sT->lock); - std::lock_guard - ga(sG->attribute_lock); + std::lock_guard<::siglock> gs(sT->lock); + std::lock_guard ga(sG->attribute_lock); auto it = sG->attribute_map.find(name); return it != sG->attribute_map.end() ? it->second : Attribute(); } -Attribute -Caliper::get_attribute(cali_id_t id) const +Attribute Caliper::get_attribute(cali_id_t id) const { // no signal lock necessary return Attribute::make_attribute(sT->tree.node(id)); } -std::vector -Caliper::get_all_attributes() const +std::vector Caliper::get_all_attributes() const { - std::lock_guard<::siglock> - g(sT->lock); - std::lock_guard - g_a(sG->attribute_lock); + std::lock_guard<::siglock> g(sT->lock); + std::lock_guard g_a(sG->attribute_lock); std::vector ret; ret.reserve(sG->attribute_map.size()); @@ -836,17 +800,23 @@ Caliper::get_all_attributes() const // /// Dispatch memory region annotation across all active channels -void -Caliper::memory_region_begin(const void* ptr, const char* label, size_t elem_size, size_t ndim, const size_t dims[], - size_t nextra, const Attribute* extra_attrs, const Variant* extra_vals) +void Caliper::memory_region_begin( + const void* ptr, + const char* label, + size_t elem_size, + size_t ndim, + const size_t dims[], + size_t nextra, + const Attribute* extra_attrs, + const Variant* extra_vals +) { for (auto& channel : sG->active_channels) memory_region_begin(&channel, ptr, label, elem_size, ndim, dims, nextra, extra_attrs, extra_vals); } /// Dispatch memory region annotation end across all active channels -void -Caliper::memory_region_end(const void* ptr) +void Caliper::memory_region_end(const void* ptr) { for (auto& channel : sG->active_channels) memory_region_end(&channel, ptr); @@ -854,27 +824,21 @@ Caliper::memory_region_end(const void* ptr) /// Returns all entries with CALI_ATTR_GLOBAL set from the process /// blackboard. -std::vector -Caliper::get_globals() +std::vector Caliper::get_globals() { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return get_globals_from_blackboard(this, sG->process_blackboard); } /// Returns all entries with CALI_ATTR_GLOBAL set from the given channel's /// and the process blackboard. -std::vector -Caliper::get_globals(const Channel& channel) +std::vector Caliper::get_globals(const Channel& channel) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); - std::vector ret = - get_globals_from_blackboard(this, sG->process_blackboard); - std::vector tmp = - get_globals_from_blackboard(this, channel.mP->channel_blackboard); + std::vector ret = get_globals_from_blackboard(this, sG->process_blackboard); + std::vector tmp = get_globals_from_blackboard(this, channel.mP->channel_blackboard); ret.insert(ret.end(), tmp.begin(), tmp.end()); @@ -883,11 +847,9 @@ Caliper::get_globals(const Channel& channel) // --- Snapshot interface -void -Caliper::pull_context(SnapshotBuilder& rec) +void Caliper::pull_context(SnapshotBuilder& rec) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); // Get thread blackboard data sT->thread_blackboard.snapshot(rec); @@ -897,11 +859,9 @@ Caliper::pull_context(SnapshotBuilder& rec) rec.append(sT->process_snapshot.view()); } -void -Caliper::pull_snapshot(Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec) +void Caliper::pull_snapshot(Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); rec.append(trigger_info); channel->mP->events.snapshot(this, channel, trigger_info, rec); @@ -912,11 +872,9 @@ Caliper::pull_snapshot(Channel* channel, SnapshotView trigger_info, SnapshotBuil rec.append(sT->process_snapshot.view()); } -void -Caliper::push_snapshot(Channel* channel, SnapshotView trigger_info) +void Caliper::push_snapshot(Channel* channel, SnapshotView trigger_info) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); sT->snapshot.reset(); @@ -931,33 +889,34 @@ Caliper::push_snapshot(Channel* channel, SnapshotView trigger_info) channel->mP->events.process_snapshot(this, channel, trigger_info, sT->snapshot.view()); } -void -Caliper::flush(Channel* chn, SnapshotView flush_info, SnapshotFlushFn proc_fn) +void Caliper::flush(Channel* chn, SnapshotView flush_info, SnapshotFlushFn proc_fn) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); chn->mP->events.pre_flush_evt(this, chn, flush_info); if (chn->mP->events.postprocess_snapshot.empty()) { chn->mP->events.flush_evt(this, chn, flush_info, proc_fn); } else { - chn->mP->events.flush_evt(this, chn, flush_info, [this,chn,proc_fn](CaliperMetadataAccessInterface&, const std::vector& rec) { + chn->mP->events.flush_evt( + this, + chn, + flush_info, + [this, chn, proc_fn](CaliperMetadataAccessInterface&, const std::vector& rec) { std::vector mrec(rec); chn->mP->events.postprocess_snapshot(this, chn, mrec); proc_fn(*this, mrec); - }); + } + ); } chn->mP->events.post_flush_evt(this, chn, flush_info); } -void -Caliper::flush_and_write(Channel* channel, SnapshotView input_flush_info) +void Caliper::flush_and_write(Channel* channel, SnapshotView input_flush_info) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); SnapshotRecord flush_info; flush_info.builder().append(input_flush_info); @@ -971,20 +930,16 @@ Caliper::flush_and_write(Channel* channel, SnapshotView input_flush_info) channel->mP->events.write_output_evt(this, channel, flush_info.view()); } -void -Caliper::clear(Channel* chn) +void Caliper::clear(Channel* chn) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); chn->mP->events.clear_evt(this, chn); } - // --- Annotation interface -void -Caliper::begin(const Attribute& attr, const Variant& data) +void Caliper::begin(const Attribute& attr, const Variant& data) { if (sT->stack_error) return; @@ -994,8 +949,7 @@ Caliper::begin(const Attribute& attr, const Variant& data) bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); // invoke callbacks if (run_events) @@ -1013,8 +967,7 @@ Caliper::begin(const Attribute& attr, const Variant& data) channel.mP->events.post_begin_evt(this, &channel, attr, data); } -void -Caliper::end(const Attribute& attr) +void Caliper::end(const Attribute& attr) { if (sT->stack_error) return; @@ -1024,7 +977,7 @@ Caliper::end(const Attribute& attr) bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); - cali_id_t key = get_blackboard_key(attr.id(), prop); + cali_id_t key = get_blackboard_key(attr.id(), prop); Blackboard* blackboard = nullptr; if (scope == CALI_ATTR_SCOPE_THREAD) @@ -1034,8 +987,7 @@ Caliper::end(const Attribute& attr) else return; - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); auto current = load_current_entry(attr, key, *blackboard); @@ -1052,8 +1004,7 @@ Caliper::end(const Attribute& attr) handle_end(attr, prop, current, key, *blackboard, sT->tree); } -void -Caliper::end_with_value_check(const Attribute& attr, const Variant& data) +void Caliper::end_with_value_check(const Attribute& attr, const Variant& data) { if (sT->stack_error) return; @@ -1063,7 +1014,7 @@ Caliper::end_with_value_check(const Attribute& attr, const Variant& data) bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); - cali_id_t key = get_blackboard_key(attr.id(), prop); + cali_id_t key = get_blackboard_key(attr.id(), prop); Blackboard* blackboard = nullptr; if (scope == CALI_ATTR_SCOPE_THREAD) @@ -1073,8 +1024,7 @@ Caliper::end_with_value_check(const Attribute& attr, const Variant& data) else return; - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); auto current = load_current_entry(attr, key, *blackboard); @@ -1092,8 +1042,7 @@ Caliper::end_with_value_check(const Attribute& attr, const Variant& data) handle_end(attr, prop, current, key, *blackboard, sT->tree); } -void -Caliper::set(const Attribute& attr, const Variant& data) +void Caliper::set(const Attribute& attr, const Variant& data) { if (sT->stack_error) return; @@ -1103,8 +1052,7 @@ Caliper::set(const Attribute& attr, const Variant& data) bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); // invoke callbacks if (run_events) @@ -1117,14 +1065,12 @@ Caliper::set(const Attribute& attr, const Variant& data) handle_set(attr, data, prop, sG->process_blackboard, sT->tree); } -void -Caliper::begin(Channel* channel, const Attribute& attr, const Variant& data) +void Caliper::begin(Channel* channel, const Attribute& attr, const Variant& data) { - int prop = attr.properties(); + int prop = attr.properties(); bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); // invoke callbacks if (run_events && channel->is_active()) @@ -1137,19 +1083,16 @@ Caliper::begin(Channel* channel, const Attribute& attr, const Variant& data) channel->mP->events.post_begin_evt(this, channel, attr, data); } -void -Caliper::end(Channel* channel, const Attribute& attr) +void Caliper::end(Channel* channel, const Attribute& attr) { - int prop = attr.properties(); + int prop = attr.properties(); bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); cali_id_t key = get_blackboard_key(attr.id(), prop); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); - BlackboardEntry current = - load_current_entry(attr, key, channel->mP->channel_blackboard); + BlackboardEntry current = load_current_entry(attr, key, channel->mP->channel_blackboard); if (current.entry.empty()) { sT->stack_error = true; @@ -1163,14 +1106,12 @@ Caliper::end(Channel* channel, const Attribute& attr) handle_end(attr, prop, current, key, channel->mP->channel_blackboard, sT->tree); } -void -Caliper::set(Channel* channel, const Attribute& attr, const Variant& data) +void Caliper::set(Channel* channel, const Attribute& attr, const Variant& data) { - int prop = attr.properties(); + int prop = attr.properties(); bool run_events = !(prop & CALI_ATTR_SKIP_EVENTS); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); // invoke callbacks if (run_events && channel->is_active()) @@ -1181,8 +1122,7 @@ Caliper::set(Channel* channel, const Attribute& attr, const Variant& data) // --- Query -Entry -Caliper::get(const Attribute& attr) +Entry Caliper::get(const Attribute& attr) { int prop = attr.properties(); int scope = prop & CALI_ATTR_SCOPE_MASK; @@ -1199,31 +1139,26 @@ Caliper::get(const Attribute& attr) cali_id_t key = get_blackboard_key(attr.id(), prop); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return blackboard->get(key).get(attr); } -Entry -Caliper::get(Channel* channel, const Attribute& attr) +Entry Caliper::get(Channel* channel, const Attribute& attr) { cali_id_t key = get_blackboard_key(attr.id(), attr.properties()); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return channel->mP->channel_blackboard.get(key).get(attr); } -Entry -Caliper::get_path_node() +Entry Caliper::get_path_node() { Entry e; { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); e = sT->thread_blackboard.get(REGION_KEY); @@ -1240,32 +1175,35 @@ Caliper::get_path_node() // --- Memory region tracking -void -Caliper::memory_region_begin(Channel* channel, const void* ptr, const char* label, size_t elem_size, size_t ndims, const size_t dims[], - size_t n, const Attribute* extra_attrs, const Variant* extra_vals) +void Caliper::memory_region_begin( + Channel* channel, + const void* ptr, + const char* label, + size_t elem_size, + size_t ndims, + const size_t dims[], + size_t n, + const Attribute* extra_attrs, + const Variant* extra_vals +) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); channel->mP->events.track_mem_evt(this, channel, ptr, label, elem_size, ndims, dims, n, extra_attrs, extra_vals); } -void -Caliper::memory_region_end(Channel* channel, const void* ptr) +void Caliper::memory_region_end(Channel* channel, const void* ptr) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); channel->mP->events.untrack_mem_evt(this, channel, ptr); } // --- Generic entry API -void -Caliper::make_record(size_t n, const Attribute attr[], const Variant value[], SnapshotBuilder& rec, Node* parent) +void Caliper::make_record(size_t n, const Attribute attr[], const Variant value[], SnapshotBuilder& rec, Node* parent) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); Node* node = parent; @@ -1279,42 +1217,34 @@ Caliper::make_record(size_t n, const Attribute attr[], const Variant value[], Sn rec.append(Entry(node)); } -Node* -Caliper::make_tree_entry(size_t n, const Node* nodelist[], Node* parent) +Node* Caliper::make_tree_entry(size_t n, const Node* nodelist[], Node* parent) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return sT->tree.get_path(n, nodelist, parent); } -Node* -Caliper::make_tree_entry(const Attribute& attr, const Variant& data, Node* parent) +Node* Caliper::make_tree_entry(const Attribute& attr, const Variant& data, Node* parent) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return sT->tree.get_child(attr, data, parent); } -Node* -Caliper::make_tree_entry(const Attribute& attr, size_t n, const Variant data[], Node* parent) +Node* Caliper::make_tree_entry(const Attribute& attr, size_t n, const Variant data[], Node* parent) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return sT->tree.get_path(attr, n, data, parent); } -Node* -Caliper::node(cali_id_t id) const +Node* Caliper::node(cali_id_t id) const { // no siglock necessary return sT->tree.node(id); } -Variant -Caliper::exchange(const Attribute& attr, const Variant& data) +Variant Caliper::exchange(const Attribute& attr, const Variant& data) { int prop = attr.properties(); int scope = prop & CALI_ATTR_SCOPE_MASK; @@ -1329,8 +1259,7 @@ Caliper::exchange(const Attribute& attr, const Variant& data) cali_id_t key = get_blackboard_key(attr.id(), prop); - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); return blackboard->exchange(key, Entry(attr, data), !(prop & CALI_ATTR_HIDDEN)).value(); } @@ -1339,11 +1268,9 @@ Caliper::exchange(const Attribute& attr, const Variant& data) // --- Channel API // -Channel -Caliper::create_channel(const char* name, const RuntimeConfig& cfg) +Channel Caliper::create_channel(const char* name, const RuntimeConfig& cfg) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); Log(1).stream() << "Creating channel " << name << std::endl; static cali_id_t next_id = 0; @@ -1352,43 +1279,41 @@ Caliper::create_channel(const char* name, const RuntimeConfig& cfg) sG->all_channels.emplace_back(channel); // Create and set key & version attributes - begin(&channel, create_attribute("cali.channel", CALI_TYPE_STRING, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_GLOBAL), - Variant(name)); + begin( + &channel, + create_attribute("cali.channel", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_GLOBAL), + Variant(name) + ); services::register_configured_services(this, &channel); if (channel.config().get("channel", "config_check").to_bool()) config_sanity_check(name, channel.config()); if (Log::verbosity() >= 3) - channel.config().print( Log(3).stream() << "Configuration:\n" ); + channel.config().print(Log(3).stream() << "Configuration:\n"); channel.mP->events.post_init_evt(this, &channel); return channel; } -Channel -Caliper::get_channel(cali_id_t id) +Channel Caliper::get_channel(cali_id_t id) { - auto it = std::find_if(sG->all_channels.begin(), sG->all_channels.end(), - [id](const Channel& channel){ return id == channel.id(); }); + auto it = std::find_if(sG->all_channels.begin(), sG->all_channels.end(), [id](const Channel& channel) { + return id == channel.id(); + }); return it == sG->all_channels.end() ? Channel() : *it; } -std::vector -Caliper::get_all_channels() +std::vector Caliper::get_all_channels() { return sG->all_channels; } -void -Caliper::delete_channel(Channel& channel) +void Caliper::delete_channel(Channel& channel) { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); channel.mP->events.pre_finish_evt(this, &channel); @@ -1404,8 +1329,7 @@ Caliper::delete_channel(Channel& channel) channel.mP->events.finish_evt(this, &channel); } -void -Caliper::activate_channel(Channel& channel) +void Caliper::activate_channel(Channel& channel) { channel.mP->is_active = true; @@ -1413,12 +1337,10 @@ Caliper::activate_channel(Channel& channel) if (it == sG->active_channels.end()) sG->active_channels.emplace_back(channel); - sG->max_active_channels = - std::max(sG->max_active_channels, sG->active_channels.size()); + sG->max_active_channels = std::max(sG->max_active_channels, sG->active_channels.size()); } -void -Caliper::deactivate_channel(Channel& channel) +void Caliper::deactivate_channel(Channel& channel) { auto it = std::find(sG->active_channels.begin(), sG->active_channels.end(), channel); if (it != sG->active_channels.end()) @@ -1428,26 +1350,22 @@ Caliper::deactivate_channel(Channel& channel) } /// \brief Release current thread -void -Caliper::release_thread() +void Caliper::release_thread() { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); - for (auto &channel : sG->all_channels) + for (auto& channel : sG->all_channels) channel.mP->events.release_thread_evt(this, &channel); } -void -Caliper::finalize() +void Caliper::finalize() { - std::lock_guard<::siglock> - g(sT->lock); + std::lock_guard<::siglock> g(sT->lock); Log(1).stream() << "Finalizing ... " << std::endl; auto channels_copy = sG->all_channels; - for (auto &channel : channels_copy) { + for (auto& channel : channels_copy) { if (channel.mP->flush_on_exit) flush_and_write(&channel, SnapshotView()); delete_channel(channel); @@ -1458,14 +1376,12 @@ Caliper::finalize() // --- Caliper constructor & singleton API // -Caliper::Caliper() - : m_is_signal(false) +Caliper::Caliper() : m_is_signal(false) { *this = Caliper::instance(); } -Caliper -Caliper::instance() +Caliper Caliper::instance() { GlobalData* gPtr = nullptr; ThreadData* tPtr = nullptr; @@ -1478,8 +1394,7 @@ Caliper::instance() // Caliper had been initialized previously; we're past the static destructor return Caliper(nullptr, nullptr, true); - std::lock_guard - g(GlobalData::s_init_mutex); + std::lock_guard g(GlobalData::s_init_mutex); Log::init(); @@ -1513,19 +1428,17 @@ Caliper::instance() return Caliper(gPtr, tPtr, false); } -Caliper -Caliper::sigsafe_instance() +Caliper Caliper::sigsafe_instance() { return Caliper(GlobalData::gObj.g_ptr, GlobalData::tObj.t_ptr, true); } -Caliper::operator bool() const +Caliper::operator bool () const { return (sG && sT && !(m_is_signal && sT->lock.is_locked())); } -void -Caliper::release() +void Caliper::release() { Caliper c; @@ -1536,14 +1449,12 @@ Caliper::release() } } -bool -Caliper::is_initialized() +bool Caliper::is_initialized() { return GlobalData::s_init_lock == 0; } -void -Caliper::add_services(const CaliperService* s) +void Caliper::add_services(const CaliperService* s) { services::add_service_specs(s); } diff --git a/src/caliper/ChannelController.cpp b/src/caliper/ChannelController.cpp index 8aa16a0c3..6f4bba561 100644 --- a/src/caliper/ChannelController.cpp +++ b/src/caliper/ChannelController.cpp @@ -10,22 +10,20 @@ using namespace cali; -struct ChannelController::ChannelControllerImpl -{ +struct ChannelController::ChannelControllerImpl { std::string name; int flags; config_map_t config; info_map_t metadata; - Channel channel; + Channel channel; ChannelControllerImpl(const char* cname, int cflags, const config_map_t& cfg) - : name(cname), - flags(cflags), - config(cfg) - { } + : name(cname), flags(cflags), config(cfg) + {} - ~ChannelControllerImpl() { + ~ChannelControllerImpl() + { if (channel) { Caliper c; c.delete_channel(channel); @@ -38,43 +36,40 @@ namespace void add_channel_metadata(Caliper& c, Channel& channel, const info_map_t& metadata) { - for (const auto &entry : metadata) { - auto attr = - c.create_attribute(entry.first, CALI_TYPE_STRING, - CALI_ATTR_GLOBAL | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_UNALIGNED); + for (const auto& entry : metadata) { + auto attr = c.create_attribute( + entry.first, + CALI_TYPE_STRING, + CALI_ATTR_GLOBAL | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_UNALIGNED + ); c.set(&channel, attr, Variant(entry.second.c_str())); } } -} // namespace [anonymous] +} // namespace -Channel -ChannelController::channel() +Channel ChannelController::channel() { return mP->channel; } -config_map_t& -ChannelController::config() +config_map_t& ChannelController::config() { return mP->config; } -config_map_t -ChannelController::copy_config() const +config_map_t ChannelController::copy_config() const { return mP->config; } -info_map_t& -ChannelController::metadata() +info_map_t& ChannelController::metadata() { return mP->metadata; } -Channel -ChannelController::create() +Channel ChannelController::create() { if (mP->channel) return mP->channel; @@ -89,8 +84,7 @@ ChannelController::create() mP->channel = c.create_channel(mP->name.c_str(), cfg); if (!mP->channel) { - Log(0).stream() << "ChannelController::create(): Could not create channel " - << mP->name << std::endl; + Log(0).stream() << "ChannelController::create(): Could not create channel " << mP->name << std::endl; return Channel(); } @@ -99,15 +93,12 @@ ChannelController::create() // Reset the object's channel pointer if the channel is destroyed // behind our back (e.g., in Caliper::release()) - mP->channel.events().finish_evt.connect([this](Caliper*, Channel*){ - mP->channel = Channel(); - }); + mP->channel.events().finish_evt.connect([this](Caliper*, Channel*) { mP->channel = Channel(); }); return mP->channel; } -void -ChannelController::start() +void ChannelController::start() { Caliper c; @@ -117,38 +108,32 @@ ChannelController::start() c.activate_channel(mP->channel); } -void -ChannelController::stop() +void ChannelController::stop() { if (mP->channel) Caliper().deactivate_channel(mP->channel); } -bool -ChannelController::is_active() const +bool ChannelController::is_active() const { return mP->channel && mP->channel.is_active(); } -void -ChannelController::flush() +void ChannelController::flush() { Channel chn = channel(); if (chn) Caliper().flush_and_write(&chn, SnapshotView()); } -std::string -ChannelController::name() const +std::string ChannelController::name() const { return mP->name; } ChannelController::ChannelController(const char* name, int flags, const config_map_t& cfg) : mP { new ChannelControllerImpl(name, flags, cfg) } -{ -} +{} ChannelController::~ChannelController() -{ -} +{} diff --git a/src/caliper/CollectiveOutputChannel.cpp b/src/caliper/CollectiveOutputChannel.cpp index 525531ad5..fc3eb298e 100644 --- a/src/caliper/CollectiveOutputChannel.cpp +++ b/src/caliper/CollectiveOutputChannel.cpp @@ -23,7 +23,6 @@ using namespace cali; - namespace { @@ -32,13 +31,14 @@ std::string remove_from_stringlist(const std::string& in, const std::string& ele auto vec = StringConverter(in).to_stringlist(); // delete all occurences of element in the vector - for (auto pos = std::find(vec.begin(), vec.end(), element); pos != vec.end(); pos = std::find(vec.begin(), vec.end(), element)) + for (auto pos = std::find(vec.begin(), vec.end(), element); pos != vec.end(); + pos = std::find(vec.begin(), vec.end(), element)) vec.erase(pos); std::string ret; - int count = 0; + int count = 0; - for (const auto &s : vec) { + for (const auto& s : vec) { if (count++ > 0) ret.append(","); ret.append(s); @@ -58,25 +58,24 @@ class MpiReportWrapper : public CollectiveOutputChannel /// \brief Create channel controller with given queries, name, flags, /// and config. - MpiReportWrapper(const std::string& local_query, - const std::string& cross_query, - const char* name, - int flags, - const config_map_t& cfg) + MpiReportWrapper( + const std::string& local_query, + const std::string& cross_query, + const char* name, + int flags, + const config_map_t& cfg + ) : m_channel(std::make_shared(name, flags, cfg)), m_local_query(local_query), m_cross_query(cross_query) - { } + {} - void start() override { - m_channel->start(); - } + void start() override { m_channel->start(); } - void stop() override { - m_channel->stop(); - } + void stop() override { m_channel->stop(); } - void collective_flush(OutputStream& stream, MPI_Comm comm) override { + void collective_flush(OutputStream& stream, MPI_Comm comm) override + { Channel chn = m_channel->channel(); if (!chn) @@ -89,9 +88,7 @@ class MpiReportWrapper : public CollectiveOutputChannel CalQLParser p(m_cross_query.c_str()); if (p.error()) { - Log(0).stream() << "CollectiveOutputChannel: cross query parse error: " - << p.error_msg() - << std::endl; + Log(0).stream() << "CollectiveOutputChannel: cross query parse error: " << p.error_msg() << std::endl; return; } else { cross_spec = p.spec(); @@ -102,9 +99,7 @@ class MpiReportWrapper : public CollectiveOutputChannel CalQLParser p(m_local_query.c_str()); if (p.error()) { - Log(0).stream() << "CollectiveOutputChannel: local query parse error: " - << p.error_msg() - << std::endl; + Log(0).stream() << "CollectiveOutputChannel: local query parse error: " << p.error_msg() << std::endl; return; } else { local_spec = p.spec(); @@ -115,11 +110,12 @@ class MpiReportWrapper : public CollectiveOutputChannel cali::collective_flush(stream, c, chn, SnapshotView(), local_spec, cross_spec, comm); } - void collective_flush(MPI_Comm comm) override { + void collective_flush(MPI_Comm comm) override + { OutputStream stream; auto cfg = m_channel->copy_config(); - auto it = cfg.find("CALI_MPIREPORT_FILENAME"); + auto it = cfg.find("CALI_MPIREPORT_FILENAME"); if (it == cfg.end()) { stream.set_stream(OutputStream::StdOut); } else { @@ -137,26 +133,20 @@ class CustomOutputControllerWrapper : public CollectiveOutputChannel public: - CustomOutputControllerWrapper(std::shared_ptr& from) - : m_channel(from) - { } + CustomOutputControllerWrapper(std::shared_ptr& from) : m_channel(from) {} - void start() override { - m_channel->start(); - } + void start() override { m_channel->start(); } - void stop() override { - m_channel->stop(); - } + void stop() override { m_channel->stop(); } - void collective_flush(OutputStream& stream, MPI_Comm mpi_comm) override { + void collective_flush(OutputStream& stream, MPI_Comm mpi_comm) override + { OutputCommMpi comm(mpi_comm); m_channel->collective_flush(stream, comm); } }; -} // namespace [anonymous] - +} // namespace std::ostream& CollectiveOutputChannel::collective_flush(std::ostream& os, MPI_Comm comm) { @@ -172,8 +162,7 @@ void CollectiveOutputChannel::collective_flush(MPI_Comm comm) collective_flush(stream, comm); } -std::shared_ptr -CollectiveOutputChannel::from(const std::shared_ptr& from) +std::shared_ptr CollectiveOutputChannel::from(const std::shared_ptr& from) { // if from is already a CollectiveOutputChannel, just return it @@ -189,8 +178,8 @@ CollectiveOutputChannel::from(const std::shared_ptr& from) config_map_t cfg(from->copy_config()); - cfg["CALI_SERVICES_ENABLE"] = ::remove_from_stringlist(cfg["CALI_SERVICES_ENABLE"], "mpireport"); - cfg["CALI_CHANNEL_CONFIG_CHECK" ] = "false"; + cfg["CALI_SERVICES_ENABLE"] = ::remove_from_stringlist(cfg["CALI_SERVICES_ENABLE"], "mpireport"); + cfg["CALI_CHANNEL_CONFIG_CHECK"] = "false"; cfg["CALI_CHANNEL_FLUSH_AT_EXIT"] = "false"; std::string cross_query = cfg["CALI_MPIREPORT_CONFIG"]; @@ -207,16 +196,15 @@ CollectiveOutputChannel::from(const std::shared_ptr& from) } CollectiveOutputChannel::~CollectiveOutputChannel() -{ } +{} namespace cali { -std::pair< std::shared_ptr, std::string > -make_collective_output_channel(const char* config_str) +std::pair, std::string> make_collective_output_channel(const char* config_str) { ConfigManager mgr; - auto configs = mgr.parse(config_str); + auto configs = mgr.parse(config_str); if (mgr.error()) return std::make_pair(nullptr, mgr.error_msg()); diff --git a/src/caliper/ConfigManager.cpp b/src/caliper/ConfigManager.cpp index e1eff46e5..bc67f421a 100644 --- a/src/caliper/ConfigManager.cpp +++ b/src/caliper/ConfigManager.cpp @@ -23,35 +23,40 @@ namespace cali // defined in controllers/controllers.cpp extern const ConfigManager::ConfigInfo* builtin_controllers_table[]; -extern const char* builtin_option_specs; +extern const char* builtin_option_specs; extern void add_submodule_controllers_and_services(); -} +} // namespace cali namespace { -ChannelController* make_basic_channel_controller(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) +ChannelController* make_basic_channel_controller( + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts +) { class BasicController : public ChannelController { public: + BasicController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) : ChannelController(name, 0, initial_cfg) - { - // Hacky way to handle "output" option - if (opts.is_set("output")) { - std::string output = opts.get("output").to_string(); - - config()["CALI_RECORDER_FILENAME" ] = output; - config()["CALI_REPORT_FILENAME" ] = output; - config()["CALI_MPIREPORT_FILENAME"] = output; - } + { + // Hacky way to handle "output" option + if (opts.is_set("output")) { + std::string output = opts.get("output").to_string(); - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + config()["CALI_RECORDER_FILENAME"] = output; + config()["CALI_REPORT_FILENAME"] = output; + config()["CALI_MPIREPORT_FILENAME"] = output; } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; return new BasicController(name, initial_cfg, opts); @@ -63,16 +68,16 @@ class ConfigSpecManager public: - void add_controller_specs(const ConfigManager::ConfigInfo** specs) { + void add_controller_specs(const ConfigManager::ConfigInfo** specs) + { for (const ConfigManager::ConfigInfo** s = specs; s && *s; s++) m_configs.push_back(**s); } - std::vector get_config_specs() const { - return m_configs; - } + std::vector get_config_specs() const { return m_configs; } - static ConfigSpecManager* instance() { + static ConfigSpecManager* instance() + { static std::unique_ptr mP { new ConfigSpecManager }; if (mP->m_configs.empty()) @@ -82,14 +87,12 @@ class ConfigSpecManager } }; -ConfigManager::arglist_t -merge_new_elements(ConfigManager::arglist_t& to, const ConfigManager::arglist_t& from) +ConfigManager::arglist_t merge_new_elements(ConfigManager::arglist_t& to, const ConfigManager::arglist_t& from) { - for (auto &p : from) { - auto it = std::find_if(to.begin(), to.end(), - [p](const std::pair& v){ - return p.first == v.first; - }); + for (auto& p : from) { + auto it = std::find_if(to.begin(), to.end(), [p](const std::pair& v) { + return p.first == v.first; + }); if (it == to.end() || p.first == "metadata") // hacky but we want to allow multiple entries for metadata to.push_back(p); @@ -98,8 +101,7 @@ merge_new_elements(ConfigManager::arglist_t& to, const ConfigManager::arglist_t& return to; } -std::vector -to_stringlist(const std::vector& list) +std::vector to_stringlist(const std::vector& list) { std::vector ret; ret.reserve(list.size()); @@ -110,11 +112,10 @@ to_stringlist(const std::vector& list) return ret; } -std::string -join_stringlist(const std::vector& list) +std::string join_stringlist(const std::vector& list) { std::string ret; - int c = 0; + int c = 0; for (const std::string& s : list) { if (c++ > 0) @@ -125,27 +126,24 @@ join_stringlist(const std::vector& list) return ret; } -void -join_stringlist(std::string& in, const std::vector& list) +void join_stringlist(std::string& in, const std::vector& list) { - for (const auto &s : list) { + for (const auto& s : list) { if (!in.empty()) in.append(","); in.append(s); } } -std::string -find_or(const std::map& m, const std::string& k, const std::string v = "") +std::string find_or(const std::map& m, const std::string& k, const std::string v = "") { auto it = m.find(k); return it != m.end() ? it->second : v; } -std::string -expand_variables(const std::string& in, const std::string& val) +std::string expand_variables(const std::string& in, const std::string& val) { - std::string ret; + std::string ret; std::istringstream is(in); bool esc = false; @@ -180,11 +178,10 @@ expand_variables(const std::string& in, const std::string& val) return ret; } -ConfigManager::arglist_t -parse_keyval_list(std::istream& is) +ConfigManager::arglist_t parse_keyval_list(std::istream& is) { ConfigManager::arglist_t ret; - char c = 0; + char c = 0; do { std::string key = util::read_word(is, "="); @@ -208,8 +205,10 @@ parse_keyval_list(std::istream& is) return ret; } -std::pair -find_key_in_json(const std::vector& path, const std::map& dict) +std::pair find_key_in_json( + const std::vector& path, + const std::map& dict +) { if (path.empty()) return std::make_pair(false, StringConverter()); @@ -222,9 +221,9 @@ find_key_in_json(const std::vector& path, const std::mapsecond); StringConverter ret = it->second; - for (auto path_it = path.begin()+1; path_it != path.end(); ++path_it) { + for (auto path_it = path.begin() + 1; path_it != path.end(); ++path_it) { auto sub_dict = ret.rec_dict(); - it = sub_dict.find(*path_it); + it = sub_dict.find(*path_it); if (it == sub_dict.end()) return std::make_pair(false, StringConverter()); ret = it->second; @@ -233,13 +232,12 @@ find_key_in_json(const std::vector& path, const std::map path = StringConverter(key).to_stringlist("."); - auto ret = find_key_in_json(path, top); + auto ret = find_key_in_json(path, top); if (ret.first) add_metadata_entries(key, ret.second, info); else - Log(1).stream() << "read_metadata_from_json_file(): Key " - << key << " not found\n"; + Log(1).stream() << "read_metadata_from_json_file(): Key " << key << " not found\n"; } } -void -add_metadata(const std::string& args, info_map_t& info) +void add_metadata(const std::string& args, info_map_t& info) { std::istringstream is(args); - auto arglist = parse_keyval_list(is); + auto arglist = parse_keyval_list(is); - auto it = std::find_if(arglist.begin(), arglist.end(), [](const std::pair& p){ - return p.first == "file"; - }); + auto it = std::find_if(arglist.begin(), arglist.end(), [](const std::pair& p) { + return p.first == "file"; + }); if (it != arglist.end()) { std::string filename = it->second; std::string keys; - it = std::find_if(arglist.begin(), arglist.end(), [](const std::pair& p){ - return p.first == "keys"; - }); + it = std::find_if(arglist.begin(), arglist.end(), [](const std::pair& p) { + return p.first == "keys"; + }); if (it != arglist.end()) keys = it->second; read_metadata_from_json_file(filename, keys, info); @@ -324,8 +317,7 @@ add_metadata(const std::string& args, info_map_t& info) } } -} // namespace [anonymous] - +} // namespace // // --- OptionSpec @@ -359,15 +351,17 @@ class ConfigManager::OptionSpec bool m_error; std::string m_error_msg; - void set_error(const std::string& msg) { - m_error = true; + void set_error(const std::string& msg) + { + m_error = true; m_error_msg = msg; } - void parse_select(const std::vector& list, query_arg_t& qarg) { + void parse_select(const std::vector& list, query_arg_t& qarg) + { for (const StringConverter& sc : list) { - bool is_a_dict = false; - std::map dict = sc.rec_dict(&is_a_dict); + bool is_a_dict = false; + std::map dict = sc.rec_dict(&is_a_dict); // The deprecated syntax for a select spec is a list of // { "expr": "expression", "as": "alias", "unit": "unit" } @@ -376,7 +370,7 @@ class ConfigManager::OptionSpec // strings. Determine which we have and parse accordingly. if (is_a_dict) { std::string str = dict["expr"].to_string(); - auto it = dict.find("as"); + auto it = dict.find("as"); if (it != dict.end()) { str.append(" as \""); str.append(it->second.to_string()); @@ -395,10 +389,11 @@ class ConfigManager::OptionSpec } } - void parse_query_args(const std::vector& list, option_spec_t& opt) { + void parse_query_args(const std::vector& list, option_spec_t& opt) + { for (const StringConverter& sc : list) { std::map dict = sc.rec_dict(); - query_arg_t qarg; + query_arg_t qarg; auto it = dict.find("group by"); if (it != dict.end()) @@ -434,14 +429,16 @@ class ConfigManager::OptionSpec } } - void parse_config(const std::map& dict, option_spec_t& opt) { + void parse_config(const std::map& dict, option_spec_t& opt) + { for (auto p : dict) opt.config[p.first] = p.second.to_string(); } - void parse_spec(const std::map& dict) { + void parse_spec(const std::map& dict) + { option_spec_t opt; - bool ok = true; + bool ok = true; auto it = dict.find("category"); if (it != dict.end()) @@ -477,7 +474,8 @@ class ConfigManager::OptionSpec data[it->second.to_string()] = opt; } - std::vector recursive_get_services_list(const std::string& cfg) { + std::vector recursive_get_services_list(const std::string& cfg) + { std::vector ret; auto it = data.find(cfg); @@ -500,65 +498,57 @@ class ConfigManager::OptionSpec public: - OptionSpec() - : m_error(false) - { } + OptionSpec() : m_error(false) {} OptionSpec(const OptionSpec&) = default; - OptionSpec(OptionSpec&&) = default; + OptionSpec(OptionSpec&&) = default; - OptionSpec& operator = (const OptionSpec&) = default; - OptionSpec& operator = (OptionSpec&&) = default; + OptionSpec& operator= (const OptionSpec&) = default; + OptionSpec& operator= (OptionSpec&&) = default; - ~OptionSpec() - { } + ~OptionSpec() {} - bool error() const { - return m_error; - } + bool error() const { return m_error; } - std::string error_msg() const { - return m_error_msg; - } + std::string error_msg() const { return m_error_msg; } - void add(const OptionSpec& other, const std::vector& categories) { - for (const auto &p : other.data) + void add(const OptionSpec& other, const std::vector& categories) + { + for (const auto& p : other.data) if (std::find(categories.begin(), categories.end(), p.second.category) != categories.end()) data.insert(p); } - void add(const std::vector& list) { + void add(const std::vector& list) + { if (m_error) return; - for (auto &p : list) { + for (auto& p : list) { parse_spec(p.rec_dict()); if (m_error) { - m_error_msg = std::string("option spec: ") - + util::clamp_string(p.to_string(), 32) - + m_error_msg; + m_error_msg = std::string("option spec: ") + util::clamp_string(p.to_string(), 32) + m_error_msg; break; } } } - bool contains(const std::string& name) const { - return data.find(name) != data.end(); - } + bool contains(const std::string& name) const { return data.find(name) != data.end(); } - std::map< std::string, std::string > - get_option_descriptions() { - std::map< std::string, std::string > ret; + std::map get_option_descriptions() + { + std::map ret; - for (auto &p : data) + for (auto& p : data) ret.insert(std::make_pair(p.first, p.second.description)); return ret; } // throw out options requiring unavailable services - void filter_unavailable_options(const std::vector& in) { + void filter_unavailable_options(const std::vector& in) + { std::vector available(in); std::sort(available.begin(), available.end()); available.erase(std::unique(available.begin(), available.end()), available.end()); @@ -578,25 +568,22 @@ class ConfigManager::OptionSpec friend class ConfigManager::Options; }; - // // --- Options // -struct ConfigManager::Options::OptionsImpl -{ +struct ConfigManager::Options::OptionsImpl { OptionSpec spec; arglist_t args; std::vector enabled_options; - - std::string - check() const { + std::string check() const + { // // Check if option value has the correct datatype // - for (const auto &arg : args) { + for (const auto& arg : args) { auto it = spec.data.find(arg.first); if (it == spec.data.end()) @@ -606,10 +593,7 @@ struct ConfigManager::Options::OptionsImpl bool ok = false; StringConverter(arg.second).to_bool(&ok); if (!ok) - return std::string("Invalid value \"") - + arg.second - + std::string("\" for ") - + arg.first; + return std::string("Invalid value \"") + arg.second + std::string("\" for ") + arg.first; } } @@ -621,24 +605,22 @@ struct ConfigManager::Options::OptionsImpl add_submodule_controllers_and_services(); auto slist = services::get_available_services(); - for (const std::string &opt : enabled_options) { + for (const std::string& opt : enabled_options) { auto o_it = spec.data.find(opt); if (o_it == spec.data.end()) continue; for (const std::string& required_service : o_it->second.services) if (std::find(slist.begin(), slist.end(), required_service) == slist.end()) - return required_service - + std::string(" service required for ") - + o_it->first - + std::string(" option is not available"); + return required_service + std::string(" service required for ") + o_it->first + + std::string(" option is not available"); } return ""; } - std::string - services(const std::string& in) { + std::string services(const std::string& in) + { std::vector vec = StringConverter(in).to_stringlist(); for (const std::string& opt : enabled_options) { @@ -657,34 +639,33 @@ struct ConfigManager::Options::OptionsImpl return ::join_stringlist(vec); } - void - append_config(config_map_t& config) { - for (const std::string &opt : enabled_options) { + void append_config(config_map_t& config) + { + for (const std::string& opt : enabled_options) { auto spec_it = spec.data.find(opt); if (spec_it == spec.data.end()) continue; - for (const auto &kv_p : spec_it->second.config) { - auto it = std::find_if(args.begin(), args.end(), - [&opt](const std::pair& p){ - return opt == p.first; - }); + for (const auto& kv_p : spec_it->second.config) { + auto it = std::find_if(args.begin(), args.end(), [&opt](const std::pair& p) { + return opt == p.first; + }); if (it != args.end()) - // replace "{}" variable placeholders in spec with argument, if any + // replace "{}" variable placeholders in spec with argument, if any config[kv_p.first] = ::expand_variables(kv_p.second, it->second); } } } - void - update_channel_config(config_map_t& config) { + void update_channel_config(config_map_t& config) + { config["CALI_SERVICES_ENABLE"] = services(config["CALI_SERVICES_ENABLE"]); append_config(config); } - void - update_channel_metadata(info_map_t& info) { - for (const auto &p : args) { + void update_channel_metadata(info_map_t& info) + { + for (const auto& p : args) { if (p.first == "metadata") { ::add_metadata(p.second, info); } else { @@ -695,24 +676,24 @@ struct ConfigManager::Options::OptionsImpl } } - std::string - build_query(const char* level, const std::map& in) const { - std::string q_let = ::find_or(in, "let"); - std::string q_select = ::find_or(in, "select"); - std::string q_groupby = ::find_or(in, "group by"); - std::string q_where = ::find_or(in, "where"); + std::string build_query(const char* level, const std::map& in) const + { + std::string q_let = ::find_or(in, "let"); + std::string q_select = ::find_or(in, "select"); + std::string q_groupby = ::find_or(in, "group by"); + std::string q_where = ::find_or(in, "where"); std::string q_aggregate = ::find_or(in, "aggregate"); - std::string q_orderby = ::find_or(in, "order by"); - std::string q_format = ::find_or(in, "format"); + std::string q_orderby = ::find_or(in, "order by"); + std::string q_format = ::find_or(in, "format"); - for (const std::string &opt : enabled_options) { + for (const std::string& opt : enabled_options) { auto s_it = spec.data.find(opt); if (s_it == spec.data.end()) continue; auto l_it = s_it->second.query_args.find(level); if (l_it != s_it->second.query_args.end()) { - const auto &q = l_it->second; + const auto& q = l_it->second; ::join_stringlist(q_let, q.let); ::join_stringlist(q_select, q.select); ::join_stringlist(q_groupby, q.groupby); @@ -742,8 +723,8 @@ struct ConfigManager::Options::OptionsImpl return ret; } - std::vector - get_inherited_specs(const std::string& name) { + std::vector get_inherited_specs(const std::string& name) + { std::vector ret; auto it = spec.data.find(name); @@ -759,11 +740,11 @@ struct ConfigManager::Options::OptionsImpl return ret; } - void - find_enabled_options() { + void find_enabled_options() + { std::vector vec; - for (const auto &argp : args) { + for (const auto& argp : args) { auto s_it = spec.data.find(argp.first); if (s_it == spec.data.end()) continue; @@ -791,41 +772,35 @@ struct ConfigManager::Options::OptionsImpl enabled_options = std::move(ret); } - OptionsImpl(const OptionSpec& s, const arglist_t& a) - : spec(s), args(a) - { - find_enabled_options(); - } + OptionsImpl(const OptionSpec& s, const arglist_t& a) : spec(s), args(a) { find_enabled_options(); } }; -ConfigManager::Options::Options(const OptionSpec& spec, const arglist_t& args) - : mP(new OptionsImpl(spec, args)) -{ } +ConfigManager::Options::Options(const OptionSpec& spec, const arglist_t& args) : mP(new OptionsImpl(spec, args)) +{} ConfigManager::Options::~Options() { mP.reset(); } -bool -ConfigManager::Options::is_set(const char* option) const +bool ConfigManager::Options::is_set(const char* option) const { std::string ostr(option); - return std::find_if(mP->args.begin(), mP->args.end(), - [&ostr](const std::pair& p){ - return ostr == p.first; - }) != mP->args.end(); + return std::find_if( + mP->args.begin(), + mP->args.end(), + [&ostr](const std::pair& p) { return ostr == p.first; } + ) + != mP->args.end(); } -bool -ConfigManager::Options::is_enabled(const char* option) const +bool ConfigManager::Options::is_enabled(const char* option) const { - return std::find(mP->enabled_options.begin(), mP->enabled_options.end(), - std::string(option)) != mP->enabled_options.end(); + return std::find(mP->enabled_options.begin(), mP->enabled_options.end(), std::string(option)) + != mP->enabled_options.end(); } -std::vector -ConfigManager::Options::enabled_options() const +std::vector ConfigManager::Options::enabled_options() const { std::vector ret; @@ -840,14 +815,12 @@ ConfigManager::Options::enabled_options() const return ret; } -StringConverter -ConfigManager::Options::get(const char* option, const char* default_val) const +StringConverter ConfigManager::Options::get(const char* option, const char* default_val) const { std::string ostr(option); - auto it = std::find_if(mP->args.begin(), mP->args.end(), - [&ostr](const std::pair& p){ - return ostr == p.first; - }); + auto it = std::find_if(mP->args.begin(), mP->args.end(), [&ostr](const std::pair& p) { + return ostr == p.first; + }); if (it != mP->args.end()) return StringConverter(it->second); @@ -855,26 +828,22 @@ ConfigManager::Options::get(const char* option, const char* default_val) const return StringConverter(default_val); } -std::string -ConfigManager::Options::check() const +std::string ConfigManager::Options::check() const { return mP->check(); } -void -ConfigManager::Options::update_channel_config(config_map_t& config) const +void ConfigManager::Options::update_channel_config(config_map_t& config) const { mP->update_channel_config(config); } -void -ConfigManager::Options::update_channel_metadata(info_map_t& metadata) const +void ConfigManager::Options::update_channel_metadata(info_map_t& metadata) const { mP->update_channel_metadata(metadata); } -std::string -ConfigManager::Options::build_query(const char* level, const std::map& in) const +std::string ConfigManager::Options::build_query(const char* level, const std::map& in) const { return mP->build_query(level, in); } @@ -883,45 +852,42 @@ ConfigManager::Options::build_query(const char* level, const std::map - m_default_parameters_for_spec; + std::map m_default_parameters_for_spec; - arglist_t m_default_parameters; - argmap_t m_extra_vars; + arglist_t m_default_parameters; + argmap_t m_extra_vars; - OptionSpec m_global_opts; + OptionSpec m_global_opts; struct config_spec_t { - std::string json; // the json spec - CreateConfigFn create; - CheckArgsFn check_args; - std::string name; + std::string json; // the json spec + CreateConfigFn create; + CheckArgsFn check_args; + std::string name; std::vector categories; - std::string description; - config_map_t initial_cfg; - OptionSpec opts; - arglist_t defaults; // default options, if any + std::string description; + config_map_t initial_cfg; + OptionSpec opts; + arglist_t defaults; // default options, if any }; - std::map< std::string, std::shared_ptr > - m_spec; + std::map> m_spec; - void - set_error(const std::string& msg) { - m_error = true; + void set_error(const std::string& msg) + { + m_error = true; m_error_msg = msg; } - void - set_error(const std::string& msg, std::istream& is) { - m_error = true; + void set_error(const std::string& msg, std::istream& is) + { + m_error = true; m_error_msg = msg; size_t maxctx = 16; @@ -938,18 +904,18 @@ struct ConfigManager::ConfigManagerImpl } // sets error if err is set - void - check_error(const std::string& err) { + void check_error(const std::string& err) + { if (err.empty()) return; set_error(err); } - void - add_config_spec(const char* jsonspec, CreateConfigFn create, CheckArgsFn check, bool ignore_existing) { + void add_config_spec(const char* jsonspec, CreateConfigFn create, CheckArgsFn check, bool ignore_existing) + { config_spec_t spec; - bool ok = false; + bool ok = false; spec.json = jsonspec; spec.create = (create == nullptr) ? ::make_basic_channel_controller : create; @@ -1005,7 +971,7 @@ struct ConfigManager::ConfigManagerImpl spec.opts.add(it->second.rec_list(&ok)); it = dict.find("config"); if (ok && !m_error && it != dict.end()) - for (const auto &p : it->second.rec_dict(&ok)) + for (const auto& p : it->second.rec_dict(&ok)) spec.initial_cfg[p.first] = p.second.to_string(); if (cfg_srvcs.size() > 0) @@ -1013,8 +979,9 @@ struct ConfigManager::ConfigManagerImpl it = dict.find("defaults"); if (ok && !m_error && it != dict.end()) - for (const auto &p : it->second.rec_dict(&ok)) - spec.defaults.push_back(std::make_pair(p.first, p.second.to_string()));; + for (const auto& p : it->second.rec_dict(&ok)) + spec.defaults.push_back(std::make_pair(p.first, p.second.to_string())); + ; if (!ok) set_error(std::string("spec parse error: ") + util::clamp_string(spec.json, 48)); @@ -1022,8 +989,8 @@ struct ConfigManager::ConfigManagerImpl m_spec.emplace(spec.name, std::make_shared(spec)); } - void - add_global_option_specs(const char* json) { + void add_global_option_specs(const char* json) + { bool ok = false; m_global_opts.add(StringConverter(json).rec_list(&ok)); @@ -1033,21 +1000,21 @@ struct ConfigManager::ConfigManagerImpl set_error(std::string("parse error: ") + util::clamp_string(builtin_option_specs, 48)); } - void - import_builtin_config_specs() { + void import_builtin_config_specs() + { add_submodule_controllers_and_services(); for (const ConfigInfo& s : ::ConfigSpecManager::instance()->get_config_specs()) - add_config_spec(s.spec, s.create, s.check_args, true /* ignore existing */ ); + add_config_spec(s.spec, s.create, s.check_args, true /* ignore existing */); } // Parse "=value" or "(value)" // Returns an empty string if there is no '=', otherwise the string after '='. // Sets error if there is a '=' but no word afterwards. - std::string - read_value(std::istream& is, const std::string& key) { + std::string read_value(std::istream& is, const std::string& key) + { std::string val; - char c = util::read_char(is); + char c = util::read_char(is); if (c == '=') { val = util::read_word(is, ",=()\n"); @@ -1056,7 +1023,7 @@ struct ConfigManager::ConfigManagerImpl set_error("Expected value after \"" + key + "=\"", is); } else if (c == '(') { val = util::read_nested_text(is, '(', ')'); - c = util::read_char(is); + c = util::read_char(is); if (c != ')') set_error("Missing ')' after \"" + key + "\"(", is); } else @@ -1065,8 +1032,8 @@ struct ConfigManager::ConfigManagerImpl return val; } - arglist_t - parse_arglist(std::istream& is, const OptionSpec& opts) { + arglist_t parse_arglist(std::istream& is, const OptionSpec& opts) + { arglist_t args; char c = util::read_char(is); @@ -1114,8 +1081,8 @@ struct ConfigManager::ConfigManagerImpl } // Return true if key is an option in any config - bool - is_option(const std::string& key) { + bool is_option(const std::string& key) + { if (m_global_opts.contains(key)) return true; @@ -1126,9 +1093,9 @@ struct ConfigManager::ConfigManagerImpl return false; } - void - parse_json_content(const std::string& json) { - bool ok = false; + void parse_json_content(const std::string& json) + { + bool ok = false; auto dict = StringConverter(json).rec_dict(&ok); if (ok) { @@ -1150,14 +1117,14 @@ struct ConfigManager::ConfigManagerImpl set_error(std::string("parse error: ") + util::clamp_string(it->second.to_string(), 48)); } - it = dict.find("configs");\ + it = dict.find("configs"); if (it != dict.end()) { auto configs = it->second.rec_list(&ok); if (!ok) { set_error(std::string("parse error: ") + util::clamp_string(it->second.to_string(), 48)); return; } - for (auto &s : configs) { + for (auto& s : configs) { std::string buf = s.to_string(); add_config_spec(buf.c_str(), nullptr, nullptr, false); @@ -1171,7 +1138,7 @@ struct ConfigManager::ConfigManagerImpl auto list = StringConverter(json).rec_list(&ok); if (ok) { - for (auto &s : list) { + for (auto& s : list) { add_config_spec(s.to_string().c_str(), nullptr, nullptr, false); if (m_error) return; @@ -1183,10 +1150,10 @@ struct ConfigManager::ConfigManagerImpl } // Load config and/or option specs from file - void - load_file(const std::string& filename) { + void load_file(const std::string& filename) + { if (std::ifstream is { filename, std::ios::ate }) { - auto size = is.tellg(); + auto size = is.tellg(); std::string str(size, '\0'); is.seekg(0); if (is.read(&str[0], size)) @@ -1196,10 +1163,10 @@ struct ConfigManager::ConfigManagerImpl } } - void - handle_load_command(std::istream& is) { + void handle_load_command(std::istream& is) + { std::vector ret; - char c = util::read_char(is); + char c = util::read_char(is); if (c != '(') { set_error("Expected '(' after \"load\"", is); @@ -1227,14 +1194,15 @@ struct ConfigManager::ConfigManagerImpl // Returns found configs with their args. Also updates the default parameters list // and extra variables list. - std::vector< std::pair, arglist_t> > - parse_configstring(const char* config_string) { + std::vector, arglist_t>> parse_configstring(const char* config_string + ) + { import_builtin_config_specs(); - std::vector< std::pair, arglist_t> > ret; + std::vector, arglist_t>> ret; std::istringstream is(config_string); - char c = 0; + char c = 0; // Return if string is only whitespace. // Prevents empty strings being marked as errors. @@ -1280,8 +1248,7 @@ struct ConfigManager::ConfigManagerImpl val = "true"; m_default_parameters.push_back(std::make_pair(key, val)); - } - else + } else m_extra_vars[key] = val; } } @@ -1297,7 +1264,8 @@ struct ConfigManager::ConfigManagerImpl return ret; } - arglist_t add_default_parameters(arglist_t& args, const config_spec_t& spec) const { + arglist_t add_default_parameters(arglist_t& args, const config_spec_t& spec) const + { auto it = m_default_parameters_for_spec.find(spec.name); if (it != m_default_parameters_for_spec.end()) @@ -1309,13 +1277,15 @@ struct ConfigManager::ConfigManagerImpl return args; } - OptionSpec options_for_config(const config_spec_t& config) const { + OptionSpec options_for_config(const config_spec_t& config) const + { OptionSpec opts(config.opts); opts.add(m_global_opts, config.categories); return opts; } - ChannelList parse(const char* config_string) { + ChannelList parse(const char* config_string) + { auto configs = parse_configstring(config_string); if (m_error) @@ -1335,26 +1305,27 @@ struct ConfigManager::ConfigManagerImpl if (m_error) return ChannelList(); else - ret.emplace_back( (cfg.first->create)(cfg.first->name.c_str(), cfg.first->initial_cfg, opts) ); + ret.emplace_back((cfg.first->create)(cfg.first->name.c_str(), cfg.first->initial_cfg, opts)); } return ret; } - bool add(const char* config_string) { + bool add(const char* config_string) + { auto channels = parse(config_string); m_channels.insert(m_channels.end(), channels.begin(), channels.end()); return !m_error; } - std::string - get_description_for_spec(const char* name) const { + std::string get_description_for_spec(const char* name) const + { auto it = m_spec.find(name); return it != m_spec.end() ? it->second->description : std::string(); } - std::string - get_documentation_for_spec(const char* name) const { + std::string get_documentation_for_spec(const char* name) const + { std::ostringstream out; out << name; @@ -1367,11 +1338,11 @@ struct ConfigManager::ConfigManagerImpl if (!optdescrmap.empty()) { size_t len = 0; - for (const auto &op : optdescrmap) + for (const auto& op : optdescrmap) len = std::max(len, op.first.size()); out << "\n Options:"; - for (const auto &op : optdescrmap) + for (const auto& op : optdescrmap) util::pad_right(out << "\n ", op.first, len) << op.second; } } @@ -1379,29 +1350,23 @@ struct ConfigManager::ConfigManagerImpl return out.str(); } - std::vector - get_docstrings() const { + std::vector get_docstrings() const + { std::vector ret; - for (const auto &p : m_spec) + for (const auto& p : m_spec) ret.push_back(get_documentation_for_spec(p.first.c_str())); return ret; } - ConfigManagerImpl() - { - add_global_option_specs(builtin_option_specs); - } + ConfigManagerImpl() { add_global_option_specs(builtin_option_specs); } }; +ConfigManager::ConfigManager() : mP(new ConfigManagerImpl) +{} -ConfigManager::ConfigManager() - : mP(new ConfigManagerImpl) -{ } - -ConfigManager::ConfigManager(const char* config_string) - : mP(new ConfigManagerImpl) +ConfigManager::ConfigManager(const char* config_string) : mP(new ConfigManagerImpl) { mP->add(config_string); } @@ -1411,39 +1376,33 @@ ConfigManager::~ConfigManager() mP.reset(); } -void -ConfigManager::add_config_spec(const ConfigInfo& info) +void ConfigManager::add_config_spec(const ConfigInfo& info) { mP->add_config_spec(info.spec, info.create, info.check_args, false /* treat existing spec as error */); } -void -ConfigManager::add_config_spec(const char* json) +void ConfigManager::add_config_spec(const char* json) { ConfigInfo info { json, nullptr, nullptr }; add_config_spec(info); } -void -ConfigManager::add_option_spec(const char* json) +void ConfigManager::add_option_spec(const char* json) { mP->add_global_option_specs(json); } -ConfigManager::ChannelList -ConfigManager::parse(const char* config_str) +ConfigManager::ChannelList ConfigManager::parse(const char* config_str) { return mP->parse(config_str); } -void -ConfigManager::load(const char* filename) +void ConfigManager::load(const char* filename) { mP->load_file(filename); } -bool -ConfigManager::add(const char* config_str) +bool ConfigManager::add(const char* config_str) { mP->add(config_str); @@ -1453,8 +1412,7 @@ ConfigManager::add(const char* config_str) return !mP->m_error; } -bool -ConfigManager::add(const char* config_string, argmap_t& extra_kv_pairs) +bool ConfigManager::add(const char* config_string, argmap_t& extra_kv_pairs) { mP->add(config_string); @@ -1463,38 +1421,32 @@ ConfigManager::add(const char* config_string, argmap_t& extra_kv_pairs) return !mP->m_error; } -bool -ConfigManager::error() const +bool ConfigManager::error() const { return mP->m_error; } -std::string -ConfigManager::error_msg() const +std::string ConfigManager::error_msg() const { return mP->m_error_msg; } -void -ConfigManager::set_default_parameter(const char* key, const char* value) +void ConfigManager::set_default_parameter(const char* key, const char* value) { mP->m_default_parameters.push_back(std::make_pair(key, value)); } -void -ConfigManager::set_default_parameter_for_config(const char* config, const char* key, const char* value) +void ConfigManager::set_default_parameter_for_config(const char* config, const char* key, const char* value) { mP->m_default_parameters_for_spec[config].push_back(std::make_pair(key, value)); } -ConfigManager::ChannelList -ConfigManager::get_all_channels() const +ConfigManager::ChannelList ConfigManager::get_all_channels() const { return mP->m_channels; } -ConfigManager::ChannelPtr -ConfigManager::get_channel(const char* name) const +ConfigManager::ChannelPtr ConfigManager::get_channel(const char* name) const { for (ChannelPtr& chn : mP->m_channels) if (chn->name() == name) @@ -1503,33 +1455,29 @@ ConfigManager::get_channel(const char* name) const return ChannelPtr(); } -void -ConfigManager::start() +void ConfigManager::start() { for (ChannelPtr& chn : mP->m_channels) chn->start(); } -void -ConfigManager::stop() +void ConfigManager::stop() { for (ChannelPtr& chn : mP->m_channels) chn->stop(); } -void -ConfigManager::flush() +void ConfigManager::flush() { for (ChannelPtr& chn : mP->m_channels) chn->flush(); } -std::string -ConfigManager::check(const char* configstr, bool allow_extra_kv_pairs) const +std::string ConfigManager::check(const char* configstr, bool allow_extra_kv_pairs) const { // Make a copy of our data because parsing the string modifies its state ConfigManagerImpl tmpP(*mP); - auto configs = tmpP.parse_configstring(configstr); + auto configs = tmpP.parse_configstring(configstr); for (auto cfg : configs) { Options opts(tmpP.options_for_config(*cfg.first), tmpP.add_default_parameters(cfg.second, *cfg.first)); @@ -1549,54 +1497,47 @@ ConfigManager::check(const char* configstr, bool allow_extra_kv_pairs) const return tmpP.m_error_msg; } -std::vector -ConfigManager::available_config_specs() const +std::vector ConfigManager::available_config_specs() const { mP->import_builtin_config_specs(); std::vector ret; - for (const auto &p : mP->m_spec) + for (const auto& p : mP->m_spec) ret.push_back(p.first); return ret; } -std::string -ConfigManager::get_description_for_spec(const char* name) const +std::string ConfigManager::get_description_for_spec(const char* name) const { mP->import_builtin_config_specs(); return mP->get_description_for_spec(name); } -std::string -ConfigManager::get_documentation_for_spec(const char* name) const +std::string ConfigManager::get_documentation_for_spec(const char* name) const { mP->import_builtin_config_specs(); return mP->get_documentation_for_spec(name); } -std::vector -ConfigManager::available_configs() +std::vector ConfigManager::available_configs() { return ConfigManager().available_config_specs(); } -std::vector -ConfigManager::get_config_docstrings() +std::vector ConfigManager::get_config_docstrings() { ConfigManagerImpl mgr; mgr.import_builtin_config_specs(); return mgr.get_docstrings(); } -std::string -ConfigManager::check_config_string(const char* configstr, bool allow_extra_kv_pairs) +std::string ConfigManager::check_config_string(const char* configstr, bool allow_extra_kv_pairs) { return ConfigManager().check(configstr, allow_extra_kv_pairs); } -void -cali::add_global_config_specs(const ConfigManager::ConfigInfo** configs) +void cali::add_global_config_specs(const ConfigManager::ConfigInfo** configs) { ::ConfigSpecManager::instance()->add_controller_specs(configs); } diff --git a/src/caliper/CustomOutputController.cpp b/src/caliper/CustomOutputController.cpp index 6905d065d..4dfece3e8 100644 --- a/src/caliper/CustomOutputController.cpp +++ b/src/caliper/CustomOutputController.cpp @@ -13,8 +13,7 @@ using namespace cali::internal; CustomOutputController::FlushFn CustomOutputController::s_flush_fn { nullptr }; -void -CustomOutputController::set_flush_fn(FlushFn flush_fn) +void CustomOutputController::set_flush_fn(FlushFn flush_fn) { s_flush_fn = flush_fn; } @@ -24,8 +23,7 @@ CustomOutputController::set_flush_fn(FlushFn flush_fn) // CustomOutputController::Comm::~Comm() -{ -} +{} int CustomOutputController::Comm::rank() const { @@ -57,11 +55,9 @@ void CustomOutputController::flush() if (s_flush_fn) { (*s_flush_fn)(this); } else { - Log(2).stream() << chn.name() - << ": CustomOutputController::flush(): using serial flush" - << std::endl; + Log(2).stream() << chn.name() << ": CustomOutputController::flush(): using serial flush" << std::endl; - Comm comm; + Comm comm; OutputStream stream; collective_flush(stream, comm); @@ -70,4 +66,4 @@ void CustomOutputController::flush() CustomOutputController::CustomOutputController(const char* name, int flags, const config_map_t& initial_cfg) : ChannelController(name, flags, initial_cfg) -{ } +{} diff --git a/src/caliper/CustomOutputController.h b/src/caliper/CustomOutputController.h index 5583fb52a..8f421498d 100644 --- a/src/caliper/CustomOutputController.h +++ b/src/caliper/CustomOutputController.h @@ -41,7 +41,8 @@ class CustomOutputController : public cali::ChannelController /// \brief Base class for the communication protocol. Implements serial /// (no-op) communication. - class Comm { + class Comm + { public: virtual ~Comm(); diff --git a/src/caliper/MemoryPool.cpp b/src/caliper/MemoryPool.cpp index 95e41d195..bd98fc42c 100644 --- a/src/caliper/MemoryPool.cpp +++ b/src/caliper/MemoryPool.cpp @@ -13,13 +13,10 @@ #include #include - using namespace cali; using namespace std; - -struct MemoryPool::MemoryPoolImpl -{ +struct MemoryPool::MemoryPoolImpl { // --- data const size_t chunksize = 64 * 1024; @@ -28,37 +25,38 @@ struct MemoryPool::MemoryPoolImpl struct Chunk { unsigned char* ptr; - size_t wmark; - size_t size; + size_t wmark; + size_t size; }; - ConfigSet m_config; + ConfigSet m_config; util::spinlock m_lock; - vector m_chunks; - bool m_can_expand; + vector m_chunks; + bool m_can_expand; - size_t m_total_reserved; - size_t m_total_used; + size_t m_total_reserved; + size_t m_total_used; // --- interface - void expand(size_t bytes) { + void expand(size_t bytes) + { size_t len = max(bytes, chunksize); unsigned char* ptr = new unsigned char[len]; std::fill_n(ptr, len, 0); - m_chunks.push_back( { ptr, 0, len } ); + m_chunks.push_back({ ptr, 0, len }); m_total_reserved += len; } - void* allocate(size_t bytes, size_t alignment, bool can_expand) { - std::lock_guard - g(m_lock); + void* allocate(size_t bytes, size_t alignment, bool can_expand) + { + std::lock_guard g(m_lock); if (m_chunks.empty() || m_chunks.back().wmark + bytes + alignment > m_chunks.back().size) { if (can_expand) @@ -67,10 +65,10 @@ struct MemoryPool::MemoryPoolImpl return nullptr; } - unsigned char *ptr = m_chunks.back().ptr + m_chunks.back().wmark; - std::uintptr_t pn = reinterpret_cast(ptr); - std::uintptr_t aligned = (pn + alignment-1) & - alignment; - std::size_t n = bytes + (aligned - pn); + unsigned char* ptr = m_chunks.back().ptr + m_chunks.back().wmark; + std::uintptr_t pn = reinterpret_cast(ptr); + std::uintptr_t aligned = (pn + alignment - 1) & -alignment; + std::size_t n = bytes + (aligned - pn); m_chunks.back().wmark += n; m_total_used += n; @@ -78,9 +76,9 @@ struct MemoryPool::MemoryPoolImpl return reinterpret_cast(aligned); } - void merge(MemoryPoolImpl& other) { - std::lock_guard - g(m_lock); + void merge(MemoryPoolImpl& other) + { + std::lock_guard g(m_lock); m_chunks.insert(m_chunks.begin(), other.m_chunks.begin(), other.m_chunks.end()); other.m_chunks.clear(); @@ -89,25 +87,24 @@ struct MemoryPool::MemoryPoolImpl m_total_used += other.m_total_used; other.m_total_reserved = 0; - other.m_total_used = 0; + other.m_total_used = 0; } - std::ostream& print_statistics(std::ostream& os) const { - unitfmt_result bytes_reserved - = unitfmt(m_total_reserved, unitfmt_bytes); - unitfmt_result bytes_used - = unitfmt(m_total_used, unitfmt_bytes); + std::ostream& print_statistics(std::ostream& os) const + { + unitfmt_result bytes_reserved = unitfmt(m_total_reserved, unitfmt_bytes); + unitfmt_result bytes_used = unitfmt(m_total_used, unitfmt_bytes); - os << "Metadata memory pool: " - << bytes_reserved.val << " " << bytes_reserved.symbol << " reserved, " - << bytes_used.val << " " << bytes_used.symbol << " used"; + os << "Metadata memory pool: " << bytes_reserved.val << " " << bytes_reserved.symbol << " reserved, " + << bytes_used.val << " " << bytes_used.symbol << " used"; return os; } MemoryPoolImpl() : m_config { RuntimeConfig::get_default_config().init("memory", s_configdata) }, - m_total_reserved { 0 }, m_total_used { 0 } + m_total_reserved { 0 }, + m_total_used { 0 } { m_can_expand = m_config.get("can_expand").to_bool(); size_t s = m_config.get("pool_size").to_uint(); @@ -115,8 +112,9 @@ struct MemoryPool::MemoryPoolImpl expand(s); } - ~MemoryPoolImpl() { - for ( auto &c : m_chunks ) + ~MemoryPoolImpl() + { + for (auto& c : m_chunks) delete[] c.ptr; m_chunks.clear(); @@ -127,26 +125,25 @@ struct MemoryPool::MemoryPoolImpl const ConfigSet::Entry MemoryPool::MemoryPoolImpl::s_configdata[] = { // key, type, value, short description, long description - { "pool_size", CALI_TYPE_UINT, "1048576", + { "pool_size", + CALI_TYPE_UINT, + "1048576", "Initial size of the Caliper memory pool (in bytes)", - "Initial size of the Caliper memory pool (in bytes)" - }, - { "can_expand", CALI_TYPE_BOOL, "true", + "Initial size of the Caliper memory pool (in bytes)" }, + { "can_expand", + CALI_TYPE_BOOL, + "true", "Allow memory pool to expand at runtime", - "Allow memory pool to expand at runtime" - }, + "Allow memory pool to expand at runtime" }, ConfigSet::Terminator }; - // --- MemoryPool public interface -MemoryPool::MemoryPool() - : mP { new MemoryPoolImpl } -{ } +MemoryPool::MemoryPool() : mP { new MemoryPoolImpl } +{} -MemoryPool::MemoryPool(size_t bytes) - : mP { new MemoryPoolImpl } +MemoryPool::MemoryPool(size_t bytes) : mP { new MemoryPoolImpl } { mP->expand(bytes); } diff --git a/src/caliper/MemoryPool.h b/src/caliper/MemoryPool.h index 75533fe56..22ae09da0 100644 --- a/src/caliper/MemoryPool.h +++ b/src/caliper/MemoryPool.h @@ -27,7 +27,6 @@ class MemoryPool std::shared_ptr mP; - public: MemoryPool(); @@ -40,16 +39,17 @@ class MemoryPool void* allocate(std::size_t bytes); void* allocate(std::size_t bytes, std::size_t alignment); - template - T* aligned_alloc(std::size_t n = 1, std::size_t a = alignof(T)) { -/* + template + T* aligned_alloc(std::size_t n = 1, std::size_t a = alignof(T)) + { + /* // ancient gcc 4.9-based STL versions don't have std::align :-(( std::size_t size = n * sizeof(T); std::size_t space = n * sizeof(T) + a; void* p = allocate(space); return reinterpret_cast(std::align(a, size, p, space)); */ - return reinterpret_cast(allocate(n*sizeof(T), a)); + return reinterpret_cast(allocate(n * sizeof(T), a)); } /// \brief Move \a other's data into this mempool. diff --git a/src/caliper/MetadataTree.cpp b/src/caliper/MetadataTree.cpp index 64f6073e5..988311a51 100644 --- a/src/caliper/MetadataTree.cpp +++ b/src/caliper/MetadataTree.cpp @@ -29,7 +29,7 @@ MetadataTree::GlobalData::GlobalData(MemoryPool& pool) node_blocks(0), g_mempool(pool) { - num_blocks = config.get("num_blocks").to_uint(); + num_blocks = config.get("num_blocks").to_uint(); nodes_per_block = std::min(config.get("nodes_per_block").to_uint(), 256); node_blocks = new NodeBlock[num_blocks]; @@ -41,25 +41,22 @@ MetadataTree::GlobalData::GlobalData(MemoryPool& pool) cali_id_t attr_id; Variant data; cali_id_t parent; - } bootstrap_nodes[] = { - { 0, 9, { CALI_TYPE_USR }, CALI_INV_ID }, - { 1, 9, { CALI_TYPE_INT }, CALI_INV_ID }, - { 2, 9, { CALI_TYPE_UINT }, CALI_INV_ID }, - { 3, 9, { CALI_TYPE_STRING }, CALI_INV_ID }, - { 4, 9, { CALI_TYPE_ADDR }, CALI_INV_ID }, - { 5, 9, { CALI_TYPE_DOUBLE }, CALI_INV_ID }, - { 6, 9, { CALI_TYPE_BOOL }, CALI_INV_ID }, - { 7, 9, { CALI_TYPE_TYPE }, CALI_INV_ID }, - { 8, 8, { CALI_TYPE_STRING, "cali.attribute.name", 19 }, 3 }, - { 9, 8, { CALI_TYPE_STRING, "cali.attribute.type", 19 }, 7 }, - { 10, 8, { CALI_TYPE_STRING, "cali.attribute.prop", 19 }, 1 }, - { 11, 9, { CALI_TYPE_PTR }, CALI_INV_ID }, - { CALI_INV_ID, CALI_INV_ID, { }, CALI_INV_ID } - }; + } bootstrap_nodes[] = { { 0, 9, { CALI_TYPE_USR }, CALI_INV_ID }, + { 1, 9, { CALI_TYPE_INT }, CALI_INV_ID }, + { 2, 9, { CALI_TYPE_UINT }, CALI_INV_ID }, + { 3, 9, { CALI_TYPE_STRING }, CALI_INV_ID }, + { 4, 9, { CALI_TYPE_ADDR }, CALI_INV_ID }, + { 5, 9, { CALI_TYPE_DOUBLE }, CALI_INV_ID }, + { 6, 9, { CALI_TYPE_BOOL }, CALI_INV_ID }, + { 7, 9, { CALI_TYPE_TYPE }, CALI_INV_ID }, + { 8, 8, { CALI_TYPE_STRING, "cali.attribute.name", 19 }, 3 }, + { 9, 8, { CALI_TYPE_STRING, "cali.attribute.type", 19 }, 7 }, + { 10, 8, { CALI_TYPE_STRING, "cali.attribute.prop", 19 }, 1 }, + { 11, 9, { CALI_TYPE_PTR }, CALI_INV_ID }, + { CALI_INV_ID, CALI_INV_ID, {}, CALI_INV_ID } }; for (const NodeInfo* info = bootstrap_nodes; info->id != CALI_INV_ID; ++info) { - Node* node = new(chunk + info->id) - Node(info->id, info->attr_id, info->data); + Node* node = new (chunk + info->id) Node(info->id, info->attr_id, info->data); if (info->parent != CALI_INV_ID) chunk[info->parent].append(node); @@ -79,11 +76,7 @@ MetadataTree::GlobalData::~GlobalData() delete[] node_blocks; } - -MetadataTree::MetadataTree() - : m_nodeblock(nullptr), - m_num_nodes(0), - m_num_blocks(0) +MetadataTree::MetadataTree() : m_nodeblock(nullptr), m_num_nodes(0), m_num_blocks(0) { GlobalData* g = mG.load(); @@ -108,7 +101,6 @@ MetadataTree::~MetadataTree() g->g_mempool.merge(m_mempool); } - bool MetadataTree::have_free_nodeblock(size_t n) { GlobalData* g = mG.load(); @@ -144,8 +136,7 @@ bool MetadataTree::have_free_nodeblock(size_t n) // --- Modifying tree operations // -Node* -MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data, Node* parent = nullptr) +Node* MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data, Node* parent = nullptr) { // Get a node block with sufficient free space @@ -154,17 +145,17 @@ MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data, // Calculate and allocate required memory - const size_t align = 8; - size_t data_size = 0; + const size_t align = 8; + size_t data_size = 0; cali_attr_type type = attr.type(); - bool copy = (type == CALI_TYPE_STRING || type == CALI_TYPE_USR); - char* ptr = nullptr; + bool copy = (type == CALI_TYPE_STRING || type == CALI_TYPE_USR); + char* ptr = nullptr; if (copy) { for (size_t i = 0; i < n; ++i) { // ensure all allocations are aligned and have 0-padding so we can safely hand out string ptrs - data_size += data[i].size() + (align - (data[i].size()+1)%align); + data_size += data[i].size() + (align - (data[i].size() + 1) % align); } ptr = static_cast(m_mempool.allocate(data_size)); @@ -181,16 +172,16 @@ MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data, for (size_t i = 0; i < n; ++i) { const void* dptr { data[i].data() }; - size_t size { data[i].size() }; + size_t size { data[i].size() }; if (copy) { dptr = memcpy(ptr, dptr, size); - ptr += size + (align-(size+1)%align); + ptr += size + (align - (size + 1) % align); } size_t index = m_nodeblock->index++; - node = new(m_nodeblock->chunk + index) + node = new (m_nodeblock->chunk + index) Node((m_nodeblock - g->node_blocks) * g->nodes_per_block + index, attr.id(), Variant(type, dptr, size)); if (parent) @@ -204,8 +195,7 @@ MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data, return node; } -Node* -MetadataTree::create_child(const Attribute& attr, const Variant& value, Node* parent) +Node* MetadataTree::create_child(const Attribute& attr, const Variant& value, Node* parent) { // Get a node block with sufficient free space @@ -217,10 +207,10 @@ MetadataTree::create_child(const Attribute& attr, const Variant& value, Node* pa if (value.has_unmanaged_data()) ptr = m_mempool.allocate(value.size() + 1 /* ensure 0-padding so we can safely hand out string ptrs */); - size_t index = m_nodeblock->index++; - GlobalData* g = mG.load(); + size_t index = m_nodeblock->index++; + GlobalData* g = mG.load(); - Node* node = new(m_nodeblock->chunk + index) + Node* node = new (m_nodeblock->chunk + index) Node((m_nodeblock - g->node_blocks) * g->nodes_per_block + index, attr.id(), value.copy(ptr)); if (parent) @@ -231,8 +221,7 @@ MetadataTree::create_child(const Attribute& attr, const Variant& value, Node* pa return node; } -Node* -MetadataTree::get_path(const Attribute& attr, size_t n, const Variant* data, Node* parent = nullptr) +Node* MetadataTree::get_path(const Attribute& attr, size_t n, const Variant* data, Node* parent = nullptr) { Node* node = parent ? parent : &(mG.load()->root); size_t base = 0; @@ -250,13 +239,13 @@ MetadataTree::get_path(const Attribute& attr, size_t n, const Variant* data, Nod } if (!node) - node = create_path(attr, n-base, data+base, parent); + node = create_path(attr, n - base, data + base, parent); return node; } -Node* -MetadataTree::get_path(size_t n, const Node* nodelist[], Node* parent = nullptr) { +Node* MetadataTree::get_path(size_t n, const Node* nodelist[], Node* parent = nullptr) +{ Node* node = parent; for (size_t i = 0; i < n; ++i) @@ -266,8 +255,7 @@ MetadataTree::get_path(size_t n, const Node* nodelist[], Node* parent = nullptr) return node; } -Node* -MetadataTree::get_or_copy_node(const Node* from, Node* parent) +Node* MetadataTree::get_or_copy_node(const Node* from, Node* parent) { GlobalData* g = mG.load(); @@ -276,7 +264,8 @@ MetadataTree::get_or_copy_node(const Node* from, Node* parent) Node* node = nullptr; - for ( node = parent->first_child(); node && !node->equals(from->attribute(), from->data()); node = node->next_sibling()) + for (node = parent->first_child(); node && !node->equals(from->attribute(), from->data()); + node = node->next_sibling()) ; if (!node) { @@ -285,7 +274,7 @@ MetadataTree::get_or_copy_node(const Node* from, Node* parent) size_t index = m_nodeblock->index++; - node = new(m_nodeblock->chunk + index) + node = new (m_nodeblock->chunk + index) Node((m_nodeblock - g->node_blocks) * g->nodes_per_block + index, from->attribute(), from->data()); parent->append(node); @@ -296,8 +285,7 @@ MetadataTree::get_or_copy_node(const Node* from, Node* parent) return node; } -Node* -MetadataTree::copy_path_without_attribute(const Attribute& attr, Node* node, Node* parent) +Node* MetadataTree::copy_path_without_attribute(const Attribute& attr, Node* node, Node* parent) { if (!parent) parent = root(); @@ -312,12 +300,11 @@ MetadataTree::copy_path_without_attribute(const Attribute& attr, Node* node, Nod return tmp; } -Node* -MetadataTree::remove_first_in_path(Node* path, const Attribute& attr) +Node* MetadataTree::remove_first_in_path(Node* path, const Attribute& attr) { Node* node = path; - for ( ; node && node->attribute() != attr.id(); node = node->parent()) + for (; node && node->attribute() != attr.id(); node = node->parent()) ; if (node) @@ -326,8 +313,7 @@ MetadataTree::remove_first_in_path(Node* path, const Attribute& attr) return copy_path_without_attribute(attr, path, node); } -Node* -MetadataTree::replace_first_in_path(Node* path, const Attribute& attr, const Variant& data) +Node* MetadataTree::replace_first_in_path(Node* path, const Attribute& attr, const Variant& data) { if (path) path = remove_first_in_path(path, attr); @@ -335,8 +321,7 @@ MetadataTree::replace_first_in_path(Node* path, const Attribute& attr, const Var return get_child(attr, data, path); } -Node* -MetadataTree::replace_all_in_path(Node* path, const Attribute& attr, size_t n, const Variant data[]) +Node* MetadataTree::replace_all_in_path(Node* path, const Attribute& attr, size_t n, const Variant data[]) { Node* parent = path; @@ -349,8 +334,7 @@ MetadataTree::replace_all_in_path(Node* path, const Attribute& attr, size_t n, c return get_path(attr, n, data, copy_path_without_attribute(attr, path, parent)); } -Node* -MetadataTree::get_child(const Attribute& attr, const Variant& val, Node* parent) +Node* MetadataTree::get_child(const Attribute& attr, const Variant& val, Node* parent) { if (!parent) parent = root(); @@ -374,27 +358,30 @@ void MetadataTree::release() // --- I/O // -std::ostream& -MetadataTree::print_statistics(std::ostream& os) const +std::ostream& MetadataTree::print_statistics(std::ostream& os) const { m_mempool.print_statistics( - os << " Metadata tree: " << m_num_blocks << " blocks, " << m_num_nodes << " nodes\n " - ); + os << " Metadata tree: " << m_num_blocks << " blocks, " << m_num_nodes << " nodes\n " + ); - return os; + return os; } std::atomic MetadataTree::mG; const ConfigSet::Entry MetadataTree::GlobalData::s_configdata[] = { // key, type, value, short description, long description - { "nodes_per_block", CALI_TYPE_UINT, "256", - "Number of context tree nodes in a node block", - "Number of context tree nodes in a node block", + { + "nodes_per_block", + CALI_TYPE_UINT, + "256", + "Number of context tree nodes in a node block", + "Number of context tree nodes in a node block", }, - { "num_blocks", CALI_TYPE_UINT, "16384", + { "num_blocks", + CALI_TYPE_UINT, + "16384", "Maximum number of context tree node blocks", - "Maximum number of context tree node blocks" - }, + "Maximum number of context tree node blocks" }, ConfigSet::Terminator }; diff --git a/src/caliper/MetadataTree.h b/src/caliper/MetadataTree.h index 294972cdd..0e3205e7c 100644 --- a/src/caliper/MetadataTree.h +++ b/src/caliper/MetadataTree.h @@ -29,20 +29,20 @@ class MetadataTree struct GlobalData { static const ConfigSet::Entry s_configdata[]; - ConfigSet config; + ConfigSet config; - Node root; - std::atomic next_block; - NodeBlock* node_blocks; + Node root; + std::atomic next_block; + NodeBlock* node_blocks; - size_t num_blocks; - size_t nodes_per_block; + size_t num_blocks; + size_t nodes_per_block; - Node* type_nodes[CALI_MAXTYPE+1]; + Node* type_nodes[CALI_MAXTYPE + 1]; // Shared copy of the initial thread's mempool. // Used to merge in the pools of deleted threads. - MemoryPool g_mempool; + MemoryPool g_mempool; explicit GlobalData(MemoryPool&); ~GlobalData(); @@ -50,14 +50,13 @@ class MetadataTree static std::atomic mG; - MemoryPool m_mempool; - NodeBlock* m_nodeblock; + MemoryPool m_mempool; + NodeBlock* m_nodeblock; - unsigned m_num_nodes; - unsigned m_num_blocks; + unsigned m_num_nodes; + unsigned m_num_blocks; - - bool have_free_nodeblock(size_t n); + bool have_free_nodeblock(size_t n); Node* create_path(const Attribute& attr, size_t n, const Variant data[], Node* parent); Node* create_child(const Attribute& attr, const Variant& value, Node* parent); @@ -70,8 +69,8 @@ class MetadataTree ~MetadataTree(); - MetadataTree(const MetadataTree&) = delete; - MetadataTree& operator = (const MetadataTree&) = delete; + MetadataTree(const MetadataTree&) = delete; + MetadataTree& operator= (const MetadataTree&) = delete; static void release(); @@ -79,30 +78,24 @@ class MetadataTree /// \brief Get or construct a path in the tree under parent with /// the attribute \a attr and the list of values in \a data - Node* - get_path(const Attribute& attr, size_t n, const Variant data[], Node* parent); + Node* get_path(const Attribute& attr, size_t n, const Variant data[], Node* parent); /// \brief Get or construct a path in the tree under parent with /// the data of the nodes given in the nodelist in the order of that list - Node* - get_path(size_t n, const Node* nodelist[], Node* parent); + Node* get_path(size_t n, const Node* nodelist[], Node* parent); /// \brief Get or construct a node with \a attr, \a val under \a parent - Node* - get_child(const Attribute& attr, const Variant& val, Node* parent); + Node* get_child(const Attribute& attr, const Variant& val, Node* parent); - Node* - remove_first_in_path(Node* path, const Attribute& attr); + Node* remove_first_in_path(Node* path, const Attribute& attr); - Node* - replace_first_in_path(Node* path, const Attribute& attr, const Variant& data); - Node* - replace_all_in_path(Node* path, const Attribute& attr, size_t n, const Variant data[]); + Node* replace_first_in_path(Node* path, const Attribute& attr, const Variant& data); + Node* replace_all_in_path(Node* path, const Attribute& attr, size_t n, const Variant data[]); // --- Data access --- - Node* - node(cali_id_t id) const { + Node* node(cali_id_t id) const + { GlobalData* g = mG.load(); size_t block = id / g->nodes_per_block; @@ -114,18 +107,13 @@ class MetadataTree return g->node_blocks[block].chunk + index; } - Node* root() const { - return &(mG.load()->root); - } + Node* root() const { return &(mG.load()->root); } - Node* type_node(cali_attr_type type) const { - return mG.load()->type_nodes[type]; - }; + Node* type_node(cali_attr_type type) const { return mG.load()->type_nodes[type]; }; // --- I/O --- - std::ostream& - print_statistics(std::ostream& os) const; + std::ostream& print_statistics(std::ostream& os) const; }; } // namespace internal diff --git a/src/caliper/MpiChannelManager.cpp b/src/caliper/MpiChannelManager.cpp index b656709bc..ee48522ad 100644 --- a/src/caliper/MpiChannelManager.cpp +++ b/src/caliper/MpiChannelManager.cpp @@ -10,18 +10,17 @@ using namespace cali; - -struct MpiChannelManager::MpiChannelManagerImpl -{ - MPI_Comm comm { MPI_COMM_NULL }; +struct MpiChannelManager::MpiChannelManagerImpl { + MPI_Comm comm { MPI_COMM_NULL }; bool is_in_comm { false }; // mpi channels are those using collective_flush() - std::vector< std::shared_ptr > mpi_channels; + std::vector> mpi_channels; // serial channels just use flush() - std::vector< std::shared_ptr > ser_channels; + std::vector> ser_channels; - void add(const std::shared_ptr& src) { + void add(const std::shared_ptr& src) + { if (!src) return; @@ -33,7 +32,8 @@ struct MpiChannelManager::MpiChannelManagerImpl ser_channels.push_back(src); } - void start() const { + void start() const + { if (!is_in_comm) return; @@ -43,7 +43,8 @@ struct MpiChannelManager::MpiChannelManagerImpl channel->start(); } - void stop() const { + void stop() const + { if (!is_in_comm) return; @@ -53,7 +54,8 @@ struct MpiChannelManager::MpiChannelManagerImpl channel->stop(); } - void flush() const { + void flush() const + { if (!is_in_comm) return; @@ -63,8 +65,7 @@ struct MpiChannelManager::MpiChannelManagerImpl channel->flush(); } - MpiChannelManagerImpl(MPI_Comm comm_) - : comm { comm_ } + MpiChannelManagerImpl(MPI_Comm comm_) : comm { comm_ } { MPI_Group group; MPI_Comm_group(comm, &group); @@ -74,13 +75,11 @@ struct MpiChannelManager::MpiChannelManagerImpl } }; - -MpiChannelManager::MpiChannelManager(MPI_Comm comm) - : mP { new MpiChannelManagerImpl(comm) } -{ } +MpiChannelManager::MpiChannelManager(MPI_Comm comm) : mP { new MpiChannelManagerImpl(comm) } +{} MpiChannelManager::~MpiChannelManager() -{ } +{} void MpiChannelManager::add(const std::shared_ptr& src) { diff --git a/src/caliper/MpiEvents.h b/src/caliper/MpiEvents.h index f8e7f16d6..5032d16f0 100644 --- a/src/caliper/MpiEvents.h +++ b/src/caliper/MpiEvents.h @@ -14,12 +14,11 @@ class Channel; /// \brief The MPI callbacks struct MpiEvents { - typedef util::callback - mpi_env_cbvec; + typedef util::callback mpi_env_cbvec; - /// \brief MPI has been initialized. - /// - /// This callback will be called by the MPI wrapper service (mpi) once + /// \brief MPI has been initialized. + /// + /// This callback will be called by the MPI wrapper service (mpi) once /// when MPI is initialized. This may happen during or after MPI_Init(). mpi_env_cbvec mpi_init_evt; @@ -30,4 +29,4 @@ struct MpiEvents { /// \brief Return the MpiEvents instance for the given channel. MpiEvents* mpiwrap_get_events(Channel* chn); // defined in mpiwrap/Wrapper.w -} +} // namespace cali diff --git a/src/caliper/OutputCommMpi.cpp b/src/caliper/OutputCommMpi.cpp index 21dc2a657..094aec368 100644 --- a/src/caliper/OutputCommMpi.cpp +++ b/src/caliper/OutputCommMpi.cpp @@ -11,14 +11,11 @@ using namespace cali; -struct OutputCommMpi::OutputCommMpiImpl -{ +struct OutputCommMpi::OutputCommMpiImpl { MPI_Comm comm { MPI_COMM_NULL }; int rank { 0 }; - bool active() const { - return comm != MPI_COMM_NULL; - } + bool active() const { return comm != MPI_COMM_NULL; } OutputCommMpiImpl(MPI_Comm comm_) { @@ -38,7 +35,7 @@ struct OutputCommMpi::OutputCommMpiImpl OutputCommMpi::OutputCommMpi() { int initialized = 0; - int finalized = 0; + int finalized = 0; MPI_Initialized(&initialized); MPI_Finalized(&finalized); @@ -55,12 +52,11 @@ OutputCommMpi::OutputCommMpi() mP.reset(new OutputCommMpiImpl(comm)); } -OutputCommMpi::OutputCommMpi(MPI_Comm comm) - : mP { new OutputCommMpiImpl(comm) } -{ } +OutputCommMpi::OutputCommMpi(MPI_Comm comm) : mP { new OutputCommMpiImpl(comm) } +{} OutputCommMpi::~OutputCommMpi() -{ } +{} int OutputCommMpi::rank() const { diff --git a/src/caliper/OutputCommMpi.h b/src/caliper/OutputCommMpi.h index aea7e2ab4..766d1fce2 100644 --- a/src/caliper/OutputCommMpi.h +++ b/src/caliper/OutputCommMpi.h @@ -37,12 +37,12 @@ class OutputCommMpi : public internal::CustomOutputController::Comm int rank() const override; - int bcast_int(int val) const override; + int bcast_int(int val) const override; std::string bcast_str(const std::string& str) const override; void cross_aggregate(CaliperMetadataDB& db, Aggregator& agg) const override; }; -} +} // namespace cali #endif diff --git a/src/caliper/RegionFilter.cpp b/src/caliper/RegionFilter.cpp index 69afe8e0f..c4555bdf7 100644 --- a/src/caliper/RegionFilter.cpp +++ b/src/caliper/RegionFilter.cpp @@ -18,20 +18,20 @@ namespace class ArgumentListParser { std::string m_error_msg; - bool m_error; + bool m_error; public: - ArgumentListParser() - : m_error_msg {}, m_error { false } - { } + ArgumentListParser() : m_error_msg {}, m_error { false } {} bool error() const { return m_error; } + std::string error_msg() const { return m_error_msg; } - std::vector parse(std::istream& is) { + std::vector parse(std::istream& is) + { std::vector ret; - char c = util::read_char(is); + char c = util::read_char(is); if (c != '(') { is.unget(); @@ -48,7 +48,7 @@ class ArgumentListParser } while (!m_error && is.good() && c == ','); if (c != ')') { - m_error = true; + m_error = true; m_error_msg = "missing \')\'"; } @@ -56,14 +56,13 @@ class ArgumentListParser } }; -} +} // namespace -std::pair, std::string> -RegionFilter::parse_filter_config(std::istream& is) +std::pair, std::string> RegionFilter::parse_filter_config(std::istream& is) { Filter ret; - bool error = false; + bool error = false; std::string error_msg; char c = 0; @@ -73,35 +72,35 @@ RegionFilter::parse_filter_config(std::istream& is) if (word == "match") { ::ArgumentListParser argparse; - auto args = argparse.parse(is); + auto args = argparse.parse(is); if (!argparse.error()) { ret.match.insert(ret.match.end(), args.begin(), args.end()); } else { - error = true; + error = true; error_msg = std::string("in match(): ") + argparse.error_msg(); } } else if (word == "startswith") { ::ArgumentListParser argparse; - auto args = argparse.parse(is); + auto args = argparse.parse(is); if (!argparse.error()) { ret.startswith.insert(ret.startswith.end(), args.begin(), args.end()); } else { - error = true; + error = true; error_msg = std::string("in startswith(): ") + argparse.error_msg(); } } else if (word == "regex") { ::ArgumentListParser argparse; - auto args = argparse.parse(is); + auto args = argparse.parse(is); if (!argparse.error()) { try { - for (const auto &s : args) + for (const auto& s : args) ret.regex.push_back(std::regex(s)); } catch (const std::regex_error& e) { - error = true; + error = true; error_msg = e.what(); } } else { - error = true; + error = true; error_msg = std::string("in regex(): ") + argparse.error_msg(); } } else if (!word.empty()) { @@ -121,37 +120,35 @@ RegionFilter::parse_filter_config(std::istream& is) return std::make_pair(retp, error_msg); } -bool -RegionFilter::match(const Variant& val, const Filter& filter) +bool RegionFilter::match(const Variant& val, const Filter& filter) { // We assume val is a string. Variant strings aren't // 0-terminated, hence the more complicated comparisons const char* strp = static_cast(val.data()); - for (const auto &w : filter.startswith) + for (const auto& w : filter.startswith) if (val.size() >= w.size() && w.compare(0, w.size(), strp, w.size()) == 0) return true; - for (const auto &w : filter.match) + for (const auto& w : filter.match) if (val.size() == w.size() && w.compare(0, w.size(), strp, w.size()) == 0) return true; - for (const auto &r : filter.regex) + for (const auto& r : filter.regex) if (std::regex_match(std::string(strp, val.size()), r) == true) return true; return false; } -std::pair -RegionFilter::from_config(const std::string& include, const std::string& exclude) +std::pair RegionFilter::from_config(const std::string& include, const std::string& exclude) { std::shared_ptr icfg; std::shared_ptr ecfg; { std::istringstream is(include); - auto p = parse_filter_config(is); + auto p = parse_filter_config(is); if (!p.second.empty()) return std::make_pair(RegionFilter(), p.second); @@ -161,7 +158,7 @@ RegionFilter::from_config(const std::string& include, const std::string& exclude { std::istringstream is(exclude); - auto p = parse_filter_config(is); + auto p = parse_filter_config(is); if (!p.second.empty()) return std::make_pair(RegionFilter(), p.second); diff --git a/src/caliper/RegionFilter.h b/src/caliper/RegionFilter.h index 1fcf5e822..b3845ad51 100644 --- a/src/caliper/RegionFilter.h +++ b/src/caliper/RegionFilter.h @@ -20,9 +20,9 @@ class Variant; class RegionFilter { struct Filter { - std::vector< std::string > startswith; - std::vector< std::string > match; - std::vector< std::regex > regex; + std::vector startswith; + std::vector match; + std::vector regex; }; std::shared_ptr m_include_filters; @@ -33,13 +33,13 @@ class RegionFilter static bool match(const Variant& val, const Filter&); RegionFilter(std::shared_ptr iflt, std::shared_ptr eflt) - : m_include_filters { iflt }, - m_exclude_filters { eflt } - { } + : m_include_filters { iflt }, m_exclude_filters { eflt } + {} public: - bool pass(const Variant& val) const { + bool pass(const Variant& val) const + { if (m_exclude_filters) if (match(val, *m_exclude_filters)) return false; @@ -49,12 +49,9 @@ class RegionFilter return true; } - bool has_filters() const { - return m_exclude_filters || m_include_filters; - } + bool has_filters() const { return m_exclude_filters || m_include_filters; } - RegionFilter() - { } + RegionFilter() {} static std::pair from_config(const std::string& include, const std::string& exclude); }; diff --git a/src/caliper/RegionProfile.cpp b/src/caliper/RegionProfile.cpp index e47b29d65..e41fce4ae 100644 --- a/src/caliper/RegionProfile.cpp +++ b/src/caliper/RegionProfile.cpp @@ -16,78 +16,69 @@ using namespace cali; - -struct RegionProfile::RegionProfileImpl -{ - -}; - +struct RegionProfile::RegionProfileImpl {}; RegionProfile::RegionProfile() - : ChannelController("region-profile", 0, { - { "CALI_SERVICES_ENABLE", "aggregate,event,timestamp" }, - { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, - { "CALI_CHANNEL_CONFIG_CHECK", "false" }, - { "CALI_EVENT_ENABLE_SNAPSHOT_INFO", "false" }, - { "CALI_TIMER_INCLUSIVE_DURATION", "false" }, - { "CALI_TIMER_UNIT", "sec" } - }) - , mP { new RegionProfileImpl } -{ } + : ChannelController( + "region-profile", + 0, + { { "CALI_SERVICES_ENABLE", "aggregate,event,timestamp" }, + { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, + { "CALI_CHANNEL_CONFIG_CHECK", "false" }, + { "CALI_EVENT_ENABLE_SNAPSHOT_INFO", "false" }, + { "CALI_TIMER_INCLUSIVE_DURATION", "false" }, + { "CALI_TIMER_UNIT", "sec" } } + ), + mP { new RegionProfileImpl } +{} RegionProfile::~RegionProfile() -{ } +{} -RegionProfile::region_profile_t -RegionProfile::exclusive_region_times(const std::string& region_type) +RegionProfile::region_profile_t RegionProfile::exclusive_region_times(const std::string& region_type) { - Caliper c; - Channel chn = channel(); + Caliper c; + Channel chn = channel(); FlatExclusiveRegionProfile rp(c, "sum#time.duration.ns", region_type.c_str()); if (chn) c.flush(&chn, SnapshotView(), rp); else - Log(1).stream() << "RegionProfile::exclusive_region_times(): channel is not enabled" - << std::endl; + Log(1).stream() << "RegionProfile::exclusive_region_times(): channel is not enabled" << std::endl; auto res = rp.result(); // convert from nanosec to sec std::get<1>(res) *= 1e-9; std::get<2>(res) *= 1e-9; - for (auto &p : std::get<0>(res)) + for (auto& p : std::get<0>(res)) p.second *= 1e-9; return res; } - -RegionProfile::region_profile_t -RegionProfile::inclusive_region_times(const std::string& region_type) +RegionProfile::region_profile_t RegionProfile::inclusive_region_times(const std::string& region_type) { - Caliper c; - Channel chn = channel(); + Caliper c; + Channel chn = channel(); FlatInclusiveRegionProfile rp(c, "sum#time.duration.ns", region_type.c_str()); if (chn) c.flush(&chn, SnapshotView(), rp); else - Log(1).stream() << "RegionProfile::inclusive_region_times(): channel is not enabled" - << std::endl; + Log(1).stream() << "RegionProfile::inclusive_region_times(): channel is not enabled" << std::endl; auto res = rp.result(); std::get<1>(res) *= 1e-9; std::get<2>(res) *= 1e-9; - for (auto &p : std::get<0>(res)) + for (auto& p : std::get<0>(res)) p.second *= 1e-9; return res; } -void -RegionProfile::clear() +void RegionProfile::clear() { Channel chn = channel(); diff --git a/src/caliper/aggregate_over_mpi.cpp b/src/caliper/aggregate_over_mpi.cpp index 6b4967ab0..7096a7085 100644 --- a/src/caliper/aggregate_over_mpi.cpp +++ b/src/caliper/aggregate_over_mpi.cpp @@ -16,14 +16,15 @@ using namespace cali; - namespace { -void recursive_append_path(const CaliperMetadataAccessInterface& db, - const Node* node, - NodeBuffer& buf, - std::set& written_nodes) +void recursive_append_path( + const CaliperMetadataAccessInterface& db, + const Node* node, + NodeBuffer& buf, + std::set& written_nodes +) { if (!node || node->id() == CALI_INV_ID) return; @@ -40,7 +41,7 @@ void recursive_append_path(const CaliperMetadataAccessInterface& db, written_nodes.insert(node->id()); buf.append(node); -} +} void pack_and_send(int dest, CaliperMetadataAccessInterface& db, Aggregator& aggregator, MPI_Comm comm) { @@ -49,91 +50,84 @@ void pack_and_send(int dest, CaliperMetadataAccessInterface& db, Aggregator& agg std::set written_nodes; - aggregator.flush(db, - [&nodebuf,&snapbuf,&written_nodes](CaliperMetadataAccessInterface& db, - const EntryList& list) - { - for (const Entry& e : list) - if (e.node()) - recursive_append_path(db, e.node(), nodebuf, written_nodes); - else if (e.is_immediate()) - recursive_append_path(db, db.node(e.attribute()), nodebuf, written_nodes); + aggregator.flush( + db, + [&nodebuf, &snapbuf, &written_nodes](CaliperMetadataAccessInterface& db, const EntryList& list) { + for (const Entry& e : list) + if (e.node()) + recursive_append_path(db, e.node(), nodebuf, written_nodes); + else if (e.is_immediate()) + recursive_append_path(db, db.node(e.attribute()), nodebuf, written_nodes); - snapbuf.append(CompressedSnapshotRecord(list.size(), list.data())); - }); + snapbuf.append(CompressedSnapshotRecord(list.size(), list.data())); + } + ); { unsigned nodecount = nodebuf.count(); - MPI_Send(&nodecount, 1, MPI_UNSIGNED, - dest, 1, comm); + MPI_Send(&nodecount, 1, MPI_UNSIGNED, dest, 1, comm); // Work with pre-3.0 MPIs that take non-const void* :-/ - MPI_Send(const_cast(nodebuf.data()), nodebuf.size(), MPI_BYTE, - dest, 2, comm); + MPI_Send(const_cast(nodebuf.data()), nodebuf.size(), MPI_BYTE, dest, 2, comm); } { unsigned snapcount = snapbuf.count(); - MPI_Send(&snapcount, 1, MPI_UNSIGNED, - dest, 3, comm); - MPI_Send(const_cast(snapbuf.data()), snapbuf.size(), MPI_BYTE, - dest, 4, comm); - } + MPI_Send(&snapcount, 1, MPI_UNSIGNED, dest, 3, comm); + MPI_Send(const_cast(snapbuf.data()), snapbuf.size(), MPI_BYTE, dest, 4, comm); + } } - size_t receive_and_merge_nodes(int source, CaliperMetadataDB& db, IdMap& idmap, MPI_Comm comm) { unsigned count; - MPI_Recv(&count, 1, MPI_UNSIGNED, - source, 1, comm, MPI_STATUS_IGNORE); + MPI_Recv(&count, 1, MPI_UNSIGNED, source, 1, comm, MPI_STATUS_IGNORE); MPI_Status status; - int size; + int size; MPI_Probe(source, 2, comm, &status); MPI_Get_count(&status, MPI_BYTE, &size); NodeBuffer nodebuf; - MPI_Recv(nodebuf.import(size, count), size, MPI_BYTE, - source, 2, comm, MPI_STATUS_IGNORE); + MPI_Recv(nodebuf.import(size, count), size, MPI_BYTE, source, 2, comm, MPI_STATUS_IGNORE); - nodebuf.for_each([&db,&idmap](const NodeBuffer::NodeInfo& info) - { - db.merge_node(info.node_id, info.attr_id, info.parent_id, info.value, idmap); - }); + nodebuf.for_each([&db, &idmap](const NodeBuffer::NodeInfo& info) { + db.merge_node(info.node_id, info.attr_id, info.parent_id, info.value, idmap); + }); return nodebuf.size(); } -size_t receive_and_merge_snapshots(int source, - CaliperMetadataDB& db, const IdMap& idmap, - SnapshotProcessFn snap_fn, - MPI_Comm comm) +size_t receive_and_merge_snapshots( + int source, + CaliperMetadataDB& db, + const IdMap& idmap, + SnapshotProcessFn snap_fn, + MPI_Comm comm +) { unsigned count; - MPI_Recv(&count, 3, MPI_UNSIGNED, - source, 3, comm, MPI_STATUS_IGNORE); + MPI_Recv(&count, 3, MPI_UNSIGNED, source, 3, comm, MPI_STATUS_IGNORE); MPI_Status status; - int size; + int size; MPI_Probe(source, 4, comm, &status); MPI_Get_count(&status, MPI_BYTE, &size); SnapshotBuffer snapbuf; - MPI_Recv(snapbuf.import(size, count), size, MPI_BYTE, - source, 4, comm, MPI_STATUS_IGNORE); + MPI_Recv(snapbuf.import(size, count), size, MPI_BYTE, source, 4, comm, MPI_STATUS_IGNORE); size_t pos = 0; for (size_t i = 0; i < snapbuf.count(); ++i) { - CompressedSnapshotRecordView view(snapbuf.data()+pos, &pos); + CompressedSnapshotRecordView view(snapbuf.data() + pos, &pos); // currently 127 entries is the max for compressed snapshots cali_id_t node_ids[128]; @@ -143,9 +137,7 @@ size_t receive_and_merge_snapshots(int source, view.unpack_nodes(128, node_ids); view.unpack_immediate(128, attr_ids, values); - snap_fn(db, db.merge_snapshot(view.num_nodes(), node_ids, - view.num_immediates(), attr_ids, values, - idmap)); + snap_fn(db, db.merge_snapshot(view.num_nodes(), node_ids, view.num_immediates(), attr_ids, values, idmap)); } return snapbuf.size(); @@ -162,30 +154,28 @@ size_t receive_and_merge(int source, CaliperMetadataDB& db, SnapshotProcessFn sn return bytes; } - -} // namespace [anonymous] +} // namespace namespace cali { -void -aggregate_over_mpi(CaliperMetadataDB& metadb, Aggregator& aggr, MPI_Comm comm) +void aggregate_over_mpi(CaliperMetadataDB& metadb, Aggregator& aggr, MPI_Comm comm) { int commsize; int rank; MPI_Comm_size(comm, &commsize); MPI_Comm_rank(comm, &rank); - + for (int step = 0, steppow2 = 1; steppow2 < commsize; ++step, steppow2 *= 2) { // receive and merge - if (rank % (2*steppow2) == 0 && rank + steppow2 < commsize) + if (rank % (2 * steppow2) == 0 && rank + steppow2 < commsize) ::receive_and_merge(rank + steppow2, metadb, aggr, comm); // send up the tree (happens only in one step for each rank, and never for rank 0) - if (rank % steppow2 == 0 && rank % (2*steppow2) != 0) + if (rank % steppow2 == 0 && rank % (2 * steppow2) != 0) ::pack_and_send(rank - steppow2, metadb, aggr, comm); } } -} +} // namespace cali diff --git a/src/caliper/api.cpp b/src/caliper/api.cpp index e3261db0a..1a1e95eb1 100644 --- a/src/caliper/api.cpp +++ b/src/caliper/api.cpp @@ -19,58 +19,51 @@ cali_id_t cali_loop_attr_id = CALI_INV_ID; cali_id_t cali_region_attr_id = CALI_INV_ID; cali_id_t cali_phase_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_fn_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_label_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_uid_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_addr_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_elem_size_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_num_elems_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_total_size_attr_id = CALI_INV_ID; -cali_id_t cali_alloc_same_size_count_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_fn_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_label_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_uid_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_addr_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_elem_size_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_num_elems_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_total_size_attr_id = CALI_INV_ID; +cali_id_t cali_alloc_same_size_count_attr_id = CALI_INV_ID; namespace cali { - Attribute class_aggregatable_attr; - Attribute class_symboladdress_attr; - Attribute class_memoryaddress_attr; - Attribute class_iteration_attr; - Attribute subscription_event_attr; +Attribute class_aggregatable_attr; +Attribute class_symboladdress_attr; +Attribute class_memoryaddress_attr; +Attribute class_iteration_attr; +Attribute subscription_event_attr; - Attribute region_attr; - Attribute phase_attr; - Attribute loop_attr; - Attribute comm_region_attr; +Attribute region_attr; +Attribute phase_attr; +Attribute loop_attr; +Attribute comm_region_attr; - void init_attribute_classes(Caliper* c) { - class_aggregatable_attr = - c->create_attribute("class.aggregatable", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); - class_symboladdress_attr = - c->create_attribute("class.symboladdress", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); - class_memoryaddress_attr = - c->create_attribute("class.memoryaddress", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); - class_iteration_attr = - c->create_attribute("class.iteration", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); - subscription_event_attr = - c->create_attribute("subscription_event", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); +void init_attribute_classes(Caliper* c) +{ + class_aggregatable_attr = c->create_attribute("class.aggregatable", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); + class_symboladdress_attr = c->create_attribute("class.symboladdress", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); + class_memoryaddress_attr = c->create_attribute("class.memoryaddress", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); + class_iteration_attr = c->create_attribute("class.iteration", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); + subscription_event_attr = c->create_attribute("subscription_event", CALI_TYPE_BOOL, CALI_ATTR_SKIP_EVENTS); - cali_class_aggregatable_attr_id = class_aggregatable_attr.id(); - cali_class_symboladdress_attr_id = class_symboladdress_attr.id(); - cali_class_memoryaddress_attr_id = class_memoryaddress_attr.id(); - cali_class_iteration_attr_id = class_iteration_attr.id(); - } + cali_class_aggregatable_attr_id = class_aggregatable_attr.id(); + cali_class_symboladdress_attr_id = class_symboladdress_attr.id(); + cali_class_memoryaddress_attr_id = class_memoryaddress_attr.id(); + cali_class_iteration_attr_id = class_iteration_attr.id(); +} - void init_api_attributes(Caliper* c) { - loop_attr = - c->create_attribute("loop", CALI_TYPE_STRING, CALI_ATTR_NESTED); - region_attr = - c->create_attribute("region", CALI_TYPE_STRING, CALI_ATTR_NESTED); - phase_attr = - c->create_attribute("phase", CALI_TYPE_STRING, CALI_ATTR_NESTED | CALI_ATTR_LEVEL_4); - comm_region_attr = - c->create_attribute("comm.region", CALI_TYPE_STRING, CALI_ATTR_NESTED | CALI_ATTR_LEVEL_1); +void init_api_attributes(Caliper* c) +{ + loop_attr = c->create_attribute("loop", CALI_TYPE_STRING, CALI_ATTR_NESTED); + region_attr = c->create_attribute("region", CALI_TYPE_STRING, CALI_ATTR_NESTED); + phase_attr = c->create_attribute("phase", CALI_TYPE_STRING, CALI_ATTR_NESTED | CALI_ATTR_LEVEL_4); + comm_region_attr = c->create_attribute("comm.region", CALI_TYPE_STRING, CALI_ATTR_NESTED | CALI_ATTR_LEVEL_1); - cali_region_attr_id = region_attr.id(); - cali_phase_attr_id = phase_attr.id(); - cali_loop_attr_id = loop_attr.id(); - } + cali_region_attr_id = region_attr.id(); + cali_phase_attr_id = phase_attr.id(); + cali_loop_attr_id = loop_attr.id(); } +} // namespace cali diff --git a/src/caliper/builtin_configmanager.cpp b/src/caliper/builtin_configmanager.cpp index 5f4e97d16..a0f03e9e0 100644 --- a/src/caliper/builtin_configmanager.cpp +++ b/src/caliper/builtin_configmanager.cpp @@ -18,20 +18,15 @@ using namespace cali; - namespace { Channel make_flush_trigger_channel(Caliper* c) { - auto services = - services::get_available_services(); - bool have_mpiflush = - std::find(services.begin(), services.end(), "mpiflush") != services.end(); + auto services = services::get_available_services(); + bool have_mpiflush = std::find(services.begin(), services.end(), "mpiflush") != services.end(); - std::map cfgmap = { - { "CALI_CHANNEL_CONFIG_CHECK", "false" } - }; + std::map cfgmap = { { "CALI_CHANNEL_CONFIG_CHECK", "false" } }; if (have_mpiflush) cfgmap["CALI_SERVICES_ENABLE"] = "mpi,mpiflush"; @@ -43,7 +38,7 @@ Channel make_flush_trigger_channel(Caliper* c) return c->create_channel("builtin.configmgr", cfg); } -} +} // namespace namespace cali { @@ -63,8 +58,7 @@ void init_builtin_configmanager(Caliper* c) mgr.add(configstr); if (mgr.error()) { - Log(0).stream() << "CALI_CONFIG: error: " << mgr.error_msg() - << std::endl; + Log(0).stream() << "CALI_CONFIG: error: " << mgr.error_msg() << std::endl; return; } @@ -73,29 +67,28 @@ void init_builtin_configmanager(Caliper* c) // that case, set the configmgr.flushed attribute to skip flushing at the // end of the program. - Attribute flag_attr = - c->create_attribute("cali.configmgr.flushed", CALI_TYPE_BOOL, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_HIDDEN | - CALI_ATTR_ASVALUE); + Attribute flag_attr = c->create_attribute( + "cali.configmgr.flushed", + CALI_TYPE_BOOL, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN | CALI_ATTR_ASVALUE + ); Channel channel = ::make_flush_trigger_channel(c); mgr.start(); - channel.events().write_output_evt.connect( - [mgr,flag_attr](Caliper* c, Channel* channel, SnapshotView) mutable { - if (c->get(channel, flag_attr).value().to_bool() == true) - return; + channel.events().write_output_evt.connect([mgr, flag_attr](Caliper* c, Channel* channel, SnapshotView) mutable { + if (c->get(channel, flag_attr).value().to_bool() == true) + return; - mgr.flush(); + mgr.flush(); - c->set(channel, flag_attr, Variant(true)); - }); + c->set(channel, flag_attr, Variant(true)); + }); Log(1).stream() << "Registered builtin ConfigManager" << std::endl; } -} // namespace cali::internal +} // namespace internal } // namespace cali diff --git a/src/caliper/cali.cpp b/src/caliper/cali.cpp index 62c7fe1b4..b1647b5a1 100644 --- a/src/caliper/cali.cpp +++ b/src/caliper/cali.cpp @@ -34,7 +34,7 @@ extern Attribute region_attr; extern Attribute phase_attr; extern Attribute comm_region_attr; -} +} // namespace cali using namespace cali; @@ -42,8 +42,7 @@ using namespace cali; // --- Miscellaneous // -const char* -cali_caliper_version() +const char* cali_caliper_version() { return CALIPER_VERSION; } @@ -52,24 +51,26 @@ cali_caliper_version() // --- Attribute interface // -cali_id_t -cali_create_attribute(const char* name, cali_attr_type type, int properties) +cali_id_t cali_create_attribute(const char* name, cali_attr_type type, int properties) { Attribute a = Caliper::instance().create_attribute(name, type, properties); return a.id(); } -cali_id_t -cali_create_attribute_with_metadata(const char* name, cali_attr_type type, int properties, - int n, - const cali_id_t meta_attr_list[], - const cali_variant_t meta_val_list[]) +cali_id_t cali_create_attribute_with_metadata( + const char* name, + cali_attr_type type, + int properties, + int n, + const cali_id_t meta_attr_list[], + const cali_variant_t meta_val_list[] +) { if (n < 1) return cali_create_attribute(name, type, properties); - Caliper c; + Caliper c; Attribute* meta_attr = new Attribute[n]; Variant* meta_data = new Variant[n]; @@ -83,8 +84,7 @@ cali_create_attribute_with_metadata(const char* name, cali_attr_type type, int p meta_data[i] = Variant(meta_val_list[i]); } - Attribute attr = - c.create_attribute(name, type, properties, n, meta_attr, meta_data); + Attribute attr = c.create_attribute(name, type, properties, n, meta_attr, meta_data); delete[] meta_data; delete[] meta_attr; @@ -92,30 +92,26 @@ cali_create_attribute_with_metadata(const char* name, cali_attr_type type, int p return attr.id(); } -cali_id_t -cali_find_attribute(const char* name) +cali_id_t cali_find_attribute(const char* name) { Attribute a = Caliper::instance().get_attribute(name); return a.id(); } -cali_attr_type -cali_attribute_type(cali_id_t attr_id) +cali_attr_type cali_attribute_type(cali_id_t attr_id) { Attribute a = Caliper::instance().get_attribute(attr_id); return a.type(); } -int -cali_attribute_properties(cali_id_t attr_id) +int cali_attribute_properties(cali_id_t attr_id) { Attribute a = Caliper::instance().get_attribute(attr_id); return a.properties(); } -const char* -cali_attribute_name(cali_id_t attr_id) +const char* cali_attribute_name(cali_id_t attr_id) { return Caliper::instance().get_attribute(attr_id).name_c_str(); } @@ -124,12 +120,14 @@ cali_attribute_name(cali_id_t attr_id) // --- Context interface // -void -cali_push_snapshot(int /*scope*/, int n, - const cali_id_t trigger_info_attr_list[], - const cali_variant_t trigger_info_val_list[]) +void cali_push_snapshot( + int /*scope*/, + int n, + const cali_id_t trigger_info_attr_list[], + const cali_variant_t trigger_info_val_list[] +) { - Caliper c; + Caliper c; Attribute attr[64]; Variant data[64]; @@ -144,17 +142,20 @@ cali_push_snapshot(int /*scope*/, int n, FixedSizeSnapshotRecord<64> trigger_info; c.make_record(n, attr, data, trigger_info.builder()); - for (auto &channel : c.get_all_channels()) + for (auto& channel : c.get_all_channels()) if (channel.is_active()) c.push_snapshot(&channel, trigger_info.view()); } -void -cali_channel_push_snapshot(cali_id_t chn_id, int /*scope*/, int n, - const cali_id_t trigger_info_attr_list[], - const cali_variant_t trigger_info_val_list[]) +void cali_channel_push_snapshot( + cali_id_t chn_id, + int /*scope*/, + int n, + const cali_id_t trigger_info_attr_list[], + const cali_variant_t trigger_info_val_list[] +) { - Caliper c; + Caliper c; Attribute attr[64]; Variant data[64]; @@ -175,8 +176,7 @@ cali_channel_push_snapshot(cali_id_t chn_id, int /*scope*/, int n, c.push_snapshot(&channel, trigger_info.view()); } -size_t -cali_channel_pull_snapshot(cali_id_t chn_id, int /* scopes */, size_t len, unsigned char* buf) +size_t cali_channel_pull_snapshot(cali_id_t chn_id, int /* scopes */, size_t len, unsigned char* buf) { Caliper c = Caliper::sigsafe_instance(); @@ -184,7 +184,7 @@ cali_channel_pull_snapshot(cali_id_t chn_id, int /* scopes */, size_t len, unsig return 0; FixedSizeSnapshotRecord snapshot; - Channel channel = c.get_channel(chn_id); + Channel channel = c.get_channel(chn_id); if (channel) c.pull_snapshot(&channel, SnapshotView(), snapshot.builder()); @@ -192,7 +192,7 @@ cali_channel_pull_snapshot(cali_id_t chn_id, int /* scopes */, size_t len, unsig Log(0).stream() << "cali_channel_pull_snapshot(): invalid channel id " << chn_id << std::endl; CompressedSnapshotRecord rec(len, buf); - SnapshotView view(snapshot.view()); + SnapshotView view(snapshot.view()); rec.append(view.size(), view.data()); return rec.needed_len(); @@ -204,67 +204,63 @@ cali_channel_pull_snapshot(cali_id_t chn_id, int /* scopes */, size_t len, unsig namespace { - // Helper operator to unpack entries from - // CompressedSnapshotRecordView::unpack() +// Helper operator to unpack entries from +// CompressedSnapshotRecordView::unpack() - class UnpackEntryOp { - void* m_arg; - cali_entry_proc_fn m_fn; +class UnpackEntryOp +{ + void* m_arg; + cali_entry_proc_fn m_fn; - public: +public: - UnpackEntryOp(cali_entry_proc_fn fn, void* user_arg) - : m_arg(user_arg), m_fn(fn) - { } + UnpackEntryOp(cali_entry_proc_fn fn, void* user_arg) : m_arg(user_arg), m_fn(fn) {} - inline bool operator()(const Entry& e) { - if (e.is_immediate()) { - if ((*m_fn)(m_arg, e.attribute(), e.value().c_variant()) == 0) + inline bool operator() (const Entry& e) + { + if (e.is_immediate()) { + if ((*m_fn)(m_arg, e.attribute(), e.value().c_variant()) == 0) + return false; + } else { + for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) + if ((*m_fn)(m_arg, node->attribute(), node->data().c_variant()) == 0) return false; - } else { - for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) - if ((*m_fn)(m_arg, node->attribute(), node->data().c_variant()) == 0) - return false; - } - - return true; } - }; - class UnpackAttributeEntryOp { - void* m_arg; - cali_entry_proc_fn m_fn; - cali_id_t m_id; + return true; + } +}; - public: +class UnpackAttributeEntryOp +{ + void* m_arg; + cali_entry_proc_fn m_fn; + cali_id_t m_id; - UnpackAttributeEntryOp(cali_id_t id, cali_entry_proc_fn fn, void* user_arg) - : m_arg(user_arg), m_fn(fn), m_id(id) - { } +public: - inline bool operator()(const Entry& e) { - if (e.is_immediate() && e.attribute() == m_id) { - if ((*m_fn)(m_arg, e.attribute(), e.value().c_variant()) == 0) - return false; - } else { - for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) - if (node->attribute() == m_id) - if ((*m_fn)(m_arg, node->attribute(), node->data().c_variant()) == 0) - return false; - } + UnpackAttributeEntryOp(cali_id_t id, cali_entry_proc_fn fn, void* user_arg) : m_arg(user_arg), m_fn(fn), m_id(id) {} - return true; + inline bool operator() (const Entry& e) + { + if (e.is_immediate() && e.attribute() == m_id) { + if ((*m_fn)(m_arg, e.attribute(), e.value().c_variant()) == 0) + return false; + } else { + for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) + if (node->attribute() == m_id) + if ((*m_fn)(m_arg, node->attribute(), node->data().c_variant()) == 0) + return false; } - }; -} -void -cali_unpack_snapshot(const unsigned char* buf, - size_t* bytes_read, - cali_entry_proc_fn proc_fn, - void* user_arg) + return true; + } +}; +} // namespace + +void cali_unpack_snapshot(const unsigned char* buf, size_t* bytes_read, cali_entry_proc_fn proc_fn, void* user_arg) { - size_t pos = 0; + size_t pos = 0; ::UnpackEntryOp op(proc_fn, user_arg); // FIXME: Need sigsafe instance? Only does read-only @@ -277,32 +273,29 @@ cali_unpack_snapshot(const unsigned char* buf, *bytes_read += pos; } -cali_variant_t -cali_find_first_in_snapshot(const unsigned char* buf, - cali_id_t attr_id, - size_t* bytes_read) +cali_variant_t cali_find_first_in_snapshot(const unsigned char* buf, cali_id_t attr_id, size_t* bytes_read) { size_t pos = 0; Variant res; Caliper c; - CompressedSnapshotRecordView(buf, &pos).unpack(&c, [&res,attr_id](const Entry& e) { - if (e.is_immediate()) { - if (e.attribute() == attr_id) { - res = e.value(); + CompressedSnapshotRecordView(buf, &pos).unpack(&c, [&res, attr_id](const Entry& e) { + if (e.is_immediate()) { + if (e.attribute() == attr_id) { + res = e.value(); + return false; + } + } else { + for (const Node* node = e.node(); node; node = node->parent()) + if (node->attribute() == attr_id) { + res = node->data(); return false; } - } else { - for (const Node* node = e.node(); node; node = node->parent()) - if (node->attribute() == attr_id) { - res = node->data(); - return false; - } - } + } - return true; - }); + return true; + }); if (bytes_read) *bytes_read += pos; @@ -310,14 +303,15 @@ cali_find_first_in_snapshot(const unsigned char* buf, return res.c_variant(); } -void -cali_find_all_in_snapshot(const unsigned char* buf, - cali_id_t attr_id, - size_t* bytes_read, - cali_entry_proc_fn proc_fn, - void* user_arg) +void cali_find_all_in_snapshot( + const unsigned char* buf, + cali_id_t attr_id, + size_t* bytes_read, + cali_entry_proc_fn proc_fn, + void* user_arg +) { - size_t pos = 0; + size_t pos = 0; ::UnpackAttributeEntryOp op(attr_id, proc_fn, user_arg); // FIXME: Need sigsafe instance? Only does read-only @@ -334,8 +328,7 @@ cali_find_all_in_snapshot(const unsigned char* buf, // --- Blackboard access interface // -cali_variant_t -cali_get(cali_id_t attr_id) +cali_variant_t cali_get(cali_id_t attr_id) { Caliper c = Caliper::sigsafe_instance(); @@ -345,10 +338,9 @@ cali_get(cali_id_t attr_id) return c.get(c.get_attribute(attr_id)).value().c_variant(); } -cali_variant_t -cali_channel_get(cali_id_t chn_id, cali_id_t attr_id) +cali_variant_t cali_channel_get(cali_id_t chn_id, cali_id_t attr_id) { - Caliper c = Caliper::sigsafe_instance(); + Caliper c = Caliper::sigsafe_instance(); Channel channel = c.get_channel(chn_id); if (!c || !channel) @@ -357,8 +349,7 @@ cali_channel_get(cali_id_t chn_id, cali_id_t attr_id) return c.get(&channel, c.get_attribute(attr_id)).value().c_variant(); } -const char* -cali_get_current_region_or(const char* alt) +const char* cali_get_current_region_or(const char* alt) { Caliper c = Caliper::sigsafe_instance(); @@ -377,50 +368,43 @@ cali_get_current_region_or(const char* alt) // --- Annotation interface // -void -cali_begin_region(const char* name) +void cali_begin_region(const char* name) { Caliper c; c.begin(cali::region_attr, Variant(name)); } -void -cali_end_region(const char* name) +void cali_end_region(const char* name) { Caliper c; c.end_with_value_check(cali::region_attr, Variant(name)); } -void -cali_begin_phase(const char* name) +void cali_begin_phase(const char* name) { Caliper c; c.begin(cali::phase_attr, Variant(name)); } -void -cali_end_phase(const char* name) +void cali_end_phase(const char* name) { Caliper c; c.end_with_value_check(cali::phase_attr, Variant(name)); } -void -cali_begin_comm_region(const char* name) +void cali_begin_comm_region(const char* name) { Caliper c; c.begin(cali::comm_region_attr, Variant(name)); } -void -cali_end_comm_region(const char* name) +void cali_end_comm_region(const char* name) { Caliper c; c.end_with_value_check(cali::comm_region_attr, Variant(name)); } -void -cali_begin(cali_id_t attr_id) +void cali_begin(cali_id_t attr_id) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -428,8 +412,7 @@ cali_begin(cali_id_t attr_id) c.begin(attr, Variant(true)); } -void -cali_end(cali_id_t attr_id) +void cali_end(cali_id_t attr_id) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -437,8 +420,7 @@ cali_end(cali_id_t attr_id) c.end(attr); } -void -cali_set(cali_id_t attr_id, const void* value, size_t size) +void cali_set(cali_id_t attr_id, const void* value, size_t size) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -446,8 +428,7 @@ cali_set(cali_id_t attr_id, const void* value, size_t size) c.set(attr, Variant(attr.type(), value, size)); } -void -cali_begin_double(cali_id_t attr_id, double val) +void cali_begin_double(cali_id_t attr_id, double val) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -455,8 +436,7 @@ cali_begin_double(cali_id_t attr_id, double val) c.begin(attr, Variant(val)); } -void -cali_begin_int(cali_id_t attr_id, int val) +void cali_begin_int(cali_id_t attr_id, int val) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -464,8 +444,7 @@ cali_begin_int(cali_id_t attr_id, int val) c.begin(attr, Variant(val)); } -void -cali_begin_string(cali_id_t attr_id, const char* val) +void cali_begin_string(cali_id_t attr_id, const char* val) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -473,8 +452,7 @@ cali_begin_string(cali_id_t attr_id, const char* val) c.begin(attr, Variant(CALI_TYPE_STRING, val, strlen(val))); } -void -cali_set_double(cali_id_t attr_id, double val) +void cali_set_double(cali_id_t attr_id, double val) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -482,8 +460,7 @@ cali_set_double(cali_id_t attr_id, double val) c.set(attr, Variant(val)); } -void -cali_set_int(cali_id_t attr_id, int val) +void cali_set_int(cali_id_t attr_id, int val) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -491,8 +468,7 @@ cali_set_int(cali_id_t attr_id, int val) c.set(attr, Variant(val)); } -void -cali_set_string(cali_id_t attr_id, const char* val) +void cali_set_string(cali_id_t attr_id, const char* val) { Caliper c; Attribute attr = c.get_attribute(attr_id); @@ -504,78 +480,63 @@ cali_set_string(cali_id_t attr_id, const char* val) // --- By-name annotation interface // -void -cali_begin_byname(const char* attr_name) +void cali_begin_byname(const char* attr_name) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_BOOL, CALI_ATTR_DEFAULT); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_BOOL, CALI_ATTR_DEFAULT); c.begin(attr, Variant(true)); } -void -cali_begin_double_byname(const char* attr_name, double val) +void cali_begin_double_byname(const char* attr_name, double val) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_DEFAULT); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_DEFAULT); c.begin(attr, Variant(val)); } -void -cali_begin_int_byname(const char* attr_name, int val) +void cali_begin_int_byname(const char* attr_name, int val) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_INT, CALI_ATTR_DEFAULT); c.begin(attr, Variant(val)); } -void -cali_begin_string_byname(const char* attr_name, const char* val) +void cali_begin_string_byname(const char* attr_name, const char* val) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_STRING, CALI_ATTR_DEFAULT); c.begin(attr, Variant(CALI_TYPE_STRING, val, strlen(val))); } -void -cali_set_double_byname(const char* attr_name, double val) +void cali_set_double_byname(const char* attr_name, double val) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_UNALIGNED); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_UNALIGNED); c.set(attr, Variant(val)); } -void -cali_set_int_byname(const char* attr_name, int val) +void cali_set_int_byname(const char* attr_name, int val) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_INT, CALI_ATTR_UNALIGNED); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_INT, CALI_ATTR_UNALIGNED); c.set(attr, Variant(val)); } -void -cali_set_string_byname(const char* attr_name, const char* val) +void cali_set_string_byname(const char* attr_name, const char* val) { Caliper c; - Attribute attr = - c.create_attribute(attr_name, CALI_TYPE_STRING, CALI_ATTR_UNALIGNED); + Attribute attr = c.create_attribute(attr_name, CALI_TYPE_STRING, CALI_ATTR_UNALIGNED); c.set(attr, Variant(CALI_TYPE_STRING, val, strlen(val))); } -void -cali_end_byname(const char* attr_name) +void cali_end_byname(const char* attr_name) { Caliper c; Attribute attr = c.get_attribute(attr_name); @@ -586,8 +547,7 @@ cali_end_byname(const char* attr_name) // --- Set globals // -void -cali_set_global_double_byname(const char* name, double val) +void cali_set_global_double_byname(const char* name, double val) { Caliper c; Attribute attr = @@ -598,8 +558,7 @@ cali_set_global_double_byname(const char* name, double val) c.set(attr, cali_make_variant_from_double(val)); } -void -cali_set_global_int_byname(const char* name, int val) +void cali_set_global_int_byname(const char* name, int val) { Caliper c; Attribute attr = @@ -610,8 +569,7 @@ cali_set_global_int_byname(const char* name, int val) c.set(attr, cali_make_variant_from_int(val)); } -void -cali_set_global_string_byname(const char* name, const char* val) +void cali_set_global_string_byname(const char* name, const char* val) { Caliper c; Attribute attr = @@ -619,11 +577,10 @@ cali_set_global_string_byname(const char* name, const char* val) // TODO: check for existing incompatible attribute key - c.set(attr, Variant(CALI_TYPE_STRING, val, strlen(val)+1)); + c.set(attr, Variant(CALI_TYPE_STRING, val, strlen(val) + 1)); } -void -cali_set_global_uint_byname(const char* name, uint64_t val) +void cali_set_global_uint_byname(const char* name, uint64_t val) { Caliper c; Attribute attr = @@ -637,30 +594,25 @@ cali_set_global_uint_byname(const char* name, uint64_t val) // --- Config API // -void -cali_config_preset(const char* key, const char* value) +void cali_config_preset(const char* key, const char* value) { if (Caliper::is_initialized()) Log(0).stream() << "Warning: Caliper is already initialized. " - << "cali_config_preset(\"" << key << "\", \"" << value - << "\") has no effect." << std::endl; + << "cali_config_preset(\"" << key << "\", \"" << value << "\") has no effect." << std::endl; RuntimeConfig::get_default_config().preset(key, value); } -void -cali_config_set(const char* key, const char* value) +void cali_config_set(const char* key, const char* value) { if (Caliper::is_initialized()) Log(0).stream() << "Warning: Caliper is already initialized. " - << "cali_config_set(\"" << key << "\", \"" << value - << "\") has no effect." << std::endl; + << "cali_config_set(\"" << key << "\", \"" << value << "\") has no effect." << std::endl; RuntimeConfig::get_default_config().set(key, value); } -void -cali_config_allow_read_env(int allow) +void cali_config_allow_read_env(int allow) { RuntimeConfig::get_default_config().allow_read_env(allow != 0); } @@ -669,34 +621,30 @@ struct _cali_configset_t { std::map cfgset; }; -cali_configset_t -cali_create_configset(const char* keyvallist[][2]) +cali_configset_t cali_create_configset(const char* keyvallist[][2]) { cali_configset_t cfg = new _cali_configset_t; if (!keyvallist) return cfg; - for ( ; (*keyvallist)[0] && (*keyvallist)[1]; ++keyvallist) + for (; (*keyvallist)[0] && (*keyvallist)[1]; ++keyvallist) cfg->cfgset.insert(std::make_pair((*keyvallist)[0], (*keyvallist)[1])); return cfg; } -void -cali_delete_configset(cali_configset_t cfg) +void cali_delete_configset(cali_configset_t cfg) { delete cfg; } -void -cali_configset_set(cali_configset_t cfg, const char* key, const char* value) +void cali_configset_set(cali_configset_t cfg, const char* key, const char* value) { cfg->cfgset[key] = value; } -cali_id_t -cali_create_channel(const char* name, int flags, cali_configset_t cfgset) +cali_id_t cali_create_channel(const char* name, int flags, cali_configset_t cfgset) { RuntimeConfig cfg; @@ -714,8 +662,7 @@ cali_create_channel(const char* name, int flags, cali_configset_t cfgset) return channel.id(); } -void -cali_delete_channel(cali_id_t chn_id) +void cali_delete_channel(cali_id_t chn_id) { Caliper c; Channel channel = c.get_channel(chn_id); @@ -726,8 +673,7 @@ cali_delete_channel(cali_id_t chn_id) Log(0).stream() << "cali_channel_delete(): invalid channel id " << chn_id << std::endl; } -void -cali_activate_channel(cali_id_t chn_id) +void cali_activate_channel(cali_id_t chn_id) { Caliper c; Channel channel = c.get_channel(chn_id); @@ -738,8 +684,7 @@ cali_activate_channel(cali_id_t chn_id) Log(0).stream() << "cali_activate_channel(): invalid channel id " << chn_id << std::endl; } -void -cali_deactivate_channel(cali_id_t chn_id) +void cali_deactivate_channel(cali_id_t chn_id) { Caliper c; Channel channel = c.get_channel(chn_id); @@ -750,8 +695,7 @@ cali_deactivate_channel(cali_id_t chn_id) Log(0).stream() << "cali_deactivate_channel(): invalid channel id " << chn_id << std::endl; } -int -cali_channel_is_active(cali_id_t chn_id) +int cali_channel_is_active(cali_id_t chn_id) { Channel channel = Caliper::instance().get_channel(chn_id); @@ -763,8 +707,7 @@ cali_channel_is_active(cali_id_t chn_id) return (channel.is_active() ? 1 : 0); } -void -cali_flush(int flush_opts) +void cali_flush(int flush_opts) { Caliper c; Channel channel = c.get_channel(0); // channel 0 should be the default channel @@ -777,8 +720,7 @@ cali_flush(int flush_opts) } } -void -cali_channel_flush(cali_id_t chn_id, int flush_opts) +void cali_channel_flush(cali_id_t chn_id, int flush_opts) { Caliper c; Channel channel = c.get_channel(chn_id); @@ -789,14 +731,12 @@ cali_channel_flush(cali_id_t chn_id, int flush_opts) c.clear(&channel); } -void -cali_init() +void cali_init() { Caliper::instance(); } -int -cali_is_initialized() +int cali_is_initialized() { return Caliper::is_initialized() ? 1 : 0; } @@ -812,17 +752,19 @@ extern Attribute class_iteration_attr; } -cali_id_t -cali_make_loop_iteration_attribute(const char* name) +cali_id_t cali_make_loop_iteration_attribute(const char* name) { Variant v_true(true); Caliper c; - Attribute attr = - c.create_attribute(std::string("iteration#").append(name), - CALI_TYPE_INT, - CALI_ATTR_ASVALUE, - 1, &class_iteration_attr, &v_true); + Attribute attr = c.create_attribute( + std::string("iteration#").append(name), + CALI_TYPE_INT, + CALI_ATTR_ASVALUE, + 1, + &class_iteration_attr, + &v_true + ); return attr.id(); } @@ -834,8 +776,7 @@ cali_make_loop_iteration_attribute(const char* name) namespace cali { -cali_id_t -create_channel(const char* name, int flags, const config_map_t& cfgmap) +cali_id_t create_channel(const char* name, int flags, const config_map_t& cfgmap) { RuntimeConfig cfg; @@ -853,15 +794,13 @@ create_channel(const char* name, int flags, const config_map_t& cfgmap) return channel.id(); } -void -write_report_for_query(cali_id_t chn_id, const char* query, int flush_opts, std::ostream& os) +void write_report_for_query(cali_id_t chn_id, const char* query, int flush_opts, std::ostream& os) { Caliper c; Channel channel = c.get_channel(chn_id); if (!channel) { - Log(0).stream() << "write_report_for_query(): invalid channel id " << chn_id - << std::endl; + Log(0).stream() << "write_report_for_query(): invalid channel id " << chn_id << std::endl; return; } @@ -869,9 +808,7 @@ write_report_for_query(cali_id_t chn_id, const char* query, int flush_opts, std: CalQLParser parser(query); if (parser.error()) { - Log(0).stream() << "write_report_for_query(): query parse error: " - << parser.error_msg() - << std::endl; + Log(0).stream() << "write_report_for_query(): query parse error: " << parser.error_msg() << std::endl; return; } @@ -883,11 +820,11 @@ write_report_for_query(cali_id_t chn_id, const char* query, int flush_opts, std: QueryProcessor queryP(spec, stream); - c.flush(&channel, SnapshotView(), [&queryP](CaliperMetadataAccessInterface& db, const std::vector& rec){ - queryP.process_record(db, rec); - }); + c.flush(&channel, SnapshotView(), [&queryP](CaliperMetadataAccessInterface& db, const std::vector& rec) { + queryP.process_record(db, rec); + }); queryP.flush(c); } -} +} // namespace cali diff --git a/src/caliper/cali_datatracker.cpp b/src/caliper/cali_datatracker.cpp index c53c91523..48bafd285 100644 --- a/src/caliper/cali_datatracker.cpp +++ b/src/caliper/cali_datatracker.cpp @@ -11,26 +11,23 @@ using namespace cali; -void -cali_datatracker_track(const void *ptr, - const char *label, - size_t size) +void cali_datatracker_track(const void* ptr, const char* label, size_t size) { Caliper::instance().memory_region_begin(ptr, label, 1, 1, &size); } -void -cali_datatracker_track_dimensional(const void *ptr, - const char *label, - size_t elem_size, - const size_t *dimensions, - size_t ndims) +void cali_datatracker_track_dimensional( + const void* ptr, + const char* label, + size_t elem_size, + const size_t* dimensions, + size_t ndims +) { Caliper::instance().memory_region_begin(ptr, label, elem_size, ndims, dimensions); } -void -cali_datatracker_untrack(const void *ptr) +void cali_datatracker_untrack(const void* ptr) { Caliper::instance().memory_region_end(ptr); } diff --git a/src/caliper/collective_flush.cpp b/src/caliper/collective_flush.cpp index d2b73579b..ba3ba00de 100644 --- a/src/caliper/collective_flush.cpp +++ b/src/caliper/collective_flush.cpp @@ -23,38 +23,55 @@ using namespace cali; namespace cali { -void -collective_flush(OutputStream& stream, Caliper& c, Channel& channel, SnapshotView flush_info, const QuerySpec& local_query, const QuerySpec& cross_query, MPI_Comm comm) +void collective_flush( + OutputStream& stream, + Caliper& c, + Channel& channel, + SnapshotView flush_info, + const QuerySpec& local_query, + const QuerySpec& cross_query, + MPI_Comm comm +) { CaliperMetadataDB db; db.add_attribute_aliases(cross_query.aliases); db.add_attribute_units(cross_query.units); - Aggregator cross_agg(cross_query); - Aggregator local_agg(local_query); + Aggregator cross_agg(cross_query); + Aggregator local_agg(local_query); - Preprocessor cross_pp(cross_query); - Preprocessor local_pp(local_query); + Preprocessor cross_pp(cross_query); + Preprocessor local_pp(local_query); - RecordSelector cross_filter(cross_query); - RecordSelector local_filter(local_query); + RecordSelector cross_filter(cross_query); + RecordSelector local_filter(local_query); // flush this rank's caliper data into local aggregator - c.flush(&channel, flush_info, [&db,&local_agg,&local_pp,&local_filter](CaliperMetadataAccessInterface& in_db, const std::vector& rec){ + c.flush( + &channel, + flush_info, + [&db, + &local_agg, + &local_pp, + &local_filter](CaliperMetadataAccessInterface& in_db, const std::vector& rec) { EntryList mrec = local_pp.process(db, db.merge_snapshot(in_db, rec)); if (local_filter.pass(db, mrec)) local_agg.add(db, mrec); - }); + } + ); // flush local aggregator results into cross-process aggregator - local_agg.flush(db, [&cross_agg,&cross_pp,&cross_filter](CaliperMetadataAccessInterface& db, const std::vector& rec){ + local_agg.flush( + db, + [&cross_agg, &cross_pp, &cross_filter](CaliperMetadataAccessInterface& db, const std::vector& rec) { EntryList mrec = cross_pp.process(db, rec); if (cross_filter.pass(db, mrec)) cross_agg.add(db, mrec); - }); + } + ); int rank = 0; diff --git a/src/caliper/config_sanity_check.cpp b/src/caliper/config_sanity_check.cpp index afe964e00..19674dc91 100644 --- a/src/caliper/config_sanity_check.cpp +++ b/src/caliper/config_sanity_check.cpp @@ -14,16 +14,8 @@ using namespace cali; namespace { -const char* trigger_grp[] = { - "alloc", - "cuptitrace", - "event", - "libpfm", - "loop_monitor", - "region_monitor", - "sampler", - nullptr -}; +const char* trigger_grp[] = { "alloc", "cuptitrace", "event", "libpfm", + "loop_monitor", "region_monitor", "sampler", nullptr }; const char* buffer_grp[] = { "aggregate", "trace", "cuptitrace", nullptr }; const char* process_grp[] = { "aggregate", "trace", "textlog", nullptr }; const char* online_grp[] = { "textlog", nullptr }; @@ -32,13 +24,11 @@ const char* offline_grp[] = { "recorder", "report", "sos", "mpireport", nullptr struct service_group_t { const char* group_name; const char** services; -} service_groups[] = { - { "snapshot trigger", trigger_grp }, - { "snapshot buffer", buffer_grp }, - { "snapshot process", process_grp }, - { "online output", online_grp }, - { "offline output", offline_grp } -}; +} service_groups[] = { { "snapshot trigger", trigger_grp }, + { "snapshot buffer", buffer_grp }, + { "snapshot process", process_grp }, + { "online output", online_grp }, + { "offline output", offline_grp } }; enum ServiceGroupID { SnapshotTrigger = 0, @@ -48,15 +38,14 @@ enum ServiceGroupID { OfflineOutput = 4 }; -std::string -format_group(const char* const* group, const char* sep, const char* last_sep) +std::string format_group(const char* const* group, const char* sep, const char* last_sep) { std::string ret; - int count = 0; + int count = 0; for (const char* const* sptr = group; sptr && *sptr; ++sptr) { if (count++) { - if (*(sptr+1) == nullptr) + if (*(sptr + 1) == nullptr) ret.append(last_sep); else ret.append(sep); @@ -68,14 +57,15 @@ format_group(const char* const* group, const char* sep, const char* last_sep) return ret; } -void -check_service_dependency(const service_group_t dependent, - const service_group_t dependency, - const std::vector& services) +void check_service_dependency( + const service_group_t dependent, + const service_group_t dependency, + const std::vector& services +) { const char* const* dept = dependent.services; - for ( ; dept && *dept; ++dept) + for (; dept && *dept; ++dept) if (std::find(services.begin(), services.end(), *dept) != services.end()) break; @@ -84,16 +74,15 @@ check_service_dependency(const service_group_t dependent, const char* const* depcy = dependency.services; - for ( ; depcy && *depcy; ++depcy) + for (; depcy && *depcy; ++depcy) if (std::find(services.begin(), services.end(), *depcy) != services.end()) break; if (depcy && *depcy) return; - Log(1).stream() << "Config check: Warning: " << dependent.group_name - << " service \"" << *dept << "\" requires " << dependency.group_name - << " services, but none are active.\n Add " + Log(1).stream() << "Config check: Warning: " << dependent.group_name << " service \"" << *dept << "\" requires " + << dependency.group_name << " services, but none are active.\n Add " << format_group(dependency.services, ", ", " or ") << " to CALI_SERVICES_ENABLE to generate Caliper output." << std::endl; } @@ -101,34 +90,29 @@ check_service_dependency(const service_group_t dependent, void check_services(const std::vector& services) { const struct ServiceDependency { - ServiceGroupID dept; ServiceGroupID depcy; - } dependencies[] { - { SnapshotTrigger, SnapshotProcess }, - { SnapshotProcess, SnapshotTrigger }, - { SnapshotBuffer, OfflineOutput }, - { OnlineOutput, SnapshotTrigger }, - { OfflineOutput, SnapshotBuffer } - }; - - for (const ServiceDependency &d : dependencies) - check_service_dependency(service_groups[d.dept], service_groups[d.depcy], - services); + ServiceGroupID dept; + ServiceGroupID depcy; + } dependencies[] { { SnapshotTrigger, SnapshotProcess }, + { SnapshotProcess, SnapshotTrigger }, + { SnapshotBuffer, OfflineOutput }, + { OnlineOutput, SnapshotTrigger }, + { OfflineOutput, SnapshotBuffer } }; + + for (const ServiceDependency& d : dependencies) + check_service_dependency(service_groups[d.dept], service_groups[d.depcy], services); } -} // namespace [anonymous] - +} // namespace namespace cali { void config_sanity_check(const char* channel, RuntimeConfig cfg) { - std::vector services = - cfg.get("services", "enable").to_stringlist(",:"); + std::vector services = cfg.get("services", "enable").to_stringlist(",:"); if (services.empty()) { - Log(1).stream() << channel << ": No services enabled, " - << channel << " channel will not record data." + Log(1).stream() << channel << ": No services enabled, " << channel << " channel will not record data." << std::endl; return; } @@ -142,4 +126,4 @@ void config_sanity_check(const char* channel, RuntimeConfig cfg) ::check_services(services); } -} +} // namespace cali diff --git a/src/caliper/controllers/CudaActivityProfileController.cpp b/src/caliper/controllers/CudaActivityProfileController.cpp index 5cc21d3e5..de7bc8fd7 100644 --- a/src/caliper/controllers/CudaActivityProfileController.cpp +++ b/src/caliper/controllers/CudaActivityProfileController.cpp @@ -24,73 +24,75 @@ class CudaActivityProfileController : public cali::ChannelController { public: - CudaActivityProfileController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts, const std::string& format) + CudaActivityProfileController( + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts, + const std::string& format + ) : ChannelController(name, 0, initial_cfg) - { - std::string output(opts.get("output", "cuda_profile").to_string()); - - if (output != "stdout" && output != "stderr") { - auto pos = output.find_last_of('.'); - std::string ext = (format == "cali" ? ".cali" : ".json"); - - if (pos == std::string::npos || output.substr(pos) != ext) - output.append(ext); - } - - auto avail_services = services::get_available_services(); - bool have_mpi = - std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); - bool have_adiak = - std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); - - bool use_mpi = have_mpi; - - if (opts.is_set("use.mpi")) - use_mpi = have_mpi && opts.is_enabled("use.mpi"); - - if (have_adiak) { - config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); - config()["CALI_ADIAK_IMPORT_CATEGORIES"] = - opts.get("adiak.import_categories", "2,3").to_string(); - } - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_AGGREGATE_KEY" ] = "*,mpi.rank"; - config()["CALI_MPIREPORT_FILENAME"] = output; - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("local", { - { "select", - "*,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec" - " ,scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec" - }, - { "group by", "path,cupti.kernel.name,cupti.activity.kind,mpi.rank" }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = output; - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "select", - "*,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec" - " ,scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec" }, - { "group by", "path,cupti.kernel.name,cupti.activity.kind" }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + std::string output(opts.get("output", "cuda_profile").to_string()); + + if (output != "stdout" && output != "stderr") { + auto pos = output.find_last_of('.'); + std::string ext = (format == "cali" ? ".cali" : ".json"); + + if (pos == std::string::npos || output.substr(pos) != ext) + output.append(ext); + } + + auto avail_services = services::get_available_services(); + bool have_mpi = std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); + bool have_adiak = + std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); + + bool use_mpi = have_mpi; + + if (opts.is_set("use.mpi")) + use_mpi = have_mpi && opts.is_enabled("use.mpi"); + + if (have_adiak) { + config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); + config()["CALI_ADIAK_IMPORT_CATEGORIES"] = opts.get("adiak.import_categories", "2,3").to_string(); } + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_AGGREGATE_KEY"] = "*,mpi.rank"; + config()["CALI_MPIREPORT_FILENAME"] = output; + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "local", + { { "select", + "*,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec" + " ,scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec" }, + { "group by", "path,cupti.kernel.name,cupti.activity.kind,mpi.rank" }, + { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = output; + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "select", + "*,scale(cupti.activity.duration,1e-9) as \"time (gpu)\" unit sec" + " ,scale(sum#cupti.host.duration,1e-9) as \"time\" unit sec" }, + { "group by", "path,cupti.kernel.name,cupti.activity.kind" }, + { "format", format } } + ); + } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; -std::string -check_args(const cali::ConfigManager::Options& opts) { +std::string check_args(const cali::ConfigManager::Options& opts) +{ // Check if output.format is valid - std::string format = opts.get("output.format", "cali").to_string(); + std::string format = opts.get("output.format", "cali").to_string(); std::set allowed_formats = { "cali", "json", "json-split", "hatchet" }; if (allowed_formats.find(format) == allowed_formats.end()) @@ -99,8 +101,11 @@ check_args(const cali::ConfigManager::Options& opts) { return ""; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { std::string format = opts.get("output.format", "cali").to_string(); @@ -109,8 +114,7 @@ make_controller(const char* name, const config_map_t& initial_cfg, const cali::C if (!(format == "json-split" || format == "json" || format == "cali")) { format = "cali"; - Log(0).stream() << "cuda-activity-profile: Unknown output format \"" << format - << "\". Using cali." + Log(0).stream() << "cuda-activity-profile: Unknown output format \"" << format << "\". Using cali." << std::endl; } @@ -145,14 +149,11 @@ const char* controller_spec = R"json( }; )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo cuda_activity_profile_controller_info -{ - ::controller_spec, ::make_controller, ::check_args -}; +ConfigManager::ConfigInfo cuda_activity_profile_controller_info { ::controller_spec, ::make_controller, ::check_args }; } diff --git a/src/caliper/controllers/CudaActivityReportController.cpp b/src/caliper/controllers/CudaActivityReportController.cpp index 257978143..abf6102f7 100644 --- a/src/caliper/controllers/CudaActivityReportController.cpp +++ b/src/caliper/controllers/CudaActivityReportController.cpp @@ -23,80 +23,74 @@ class CudaActivityReportController : public cali::ChannelController { public: - CudaActivityReportController(bool use_mpi, const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) + CudaActivityReportController( + bool use_mpi, + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts + ) : ChannelController(name, 0, initial_cfg) - { - // Config for first aggregation step in MPI mode (process-local aggregation) - std::string local_select = - " inclusive_scale(sum#cupti.host.duration,1e-9)" - ",inclusive_scale(cupti.activity.duration,1e-9)"; - // Config for serial-mode aggregation - std::string serial_select = - " inclusive_scale(sum#cupti.host.duration,1e-9) as \"Host Time\"" - ",inclusive_scale(cupti.activity.duration,1e-9) as \"GPU Time\"" - ",inclusive_ratio(cupti.activity.duration,sum#cupti.host.duration,100.0) as \"GPU %\""; - - // Config for second aggregation step in MPI mode (cross-process aggregation) - std::string cross_select = - " avg(iscale#sum#cupti.host.duration) as \"Avg Host Time\"" - ",max(iscale#sum#cupti.host.duration) as \"Max Host Time\"" - ",avg(iscale#cupti.activity.duration) as \"Avg GPU Time\"" - ",max(iscale#cupti.activity.duration) as \"Max GPU Time\"" - ",ratio(iscale#cupti.activity.duration,iscale#sum#cupti.host.duration,100.0) as \"GPU %\""; - - std::string groupby = "path"; - - if (opts.is_enabled("show_kernels")) { - groupby += ",cupti.kernel.name"; - serial_select - = std::string("cupti.kernel.name as Kernel,") + serial_select; - cross_select - = std::string("cupti.kernel.name as Kernel,") + cross_select; - } - - std::string format = util::build_tree_format_spec(config(), opts); - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); - config()["CALI_MPIREPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_LOCAL_CONFIG"] = - opts.build_query("local", { - { "select", local_select }, - { "group by", groupby } - }); - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("cross", { - { "select", cross_select }, - { "group by", groupby }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string(); - config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "select", serial_select }, - { "group by", groupby }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + // Config for first aggregation step in MPI mode (process-local aggregation) + std::string local_select = + " inclusive_scale(sum#cupti.host.duration,1e-9)" + ",inclusive_scale(cupti.activity.duration,1e-9)"; + // Config for serial-mode aggregation + std::string serial_select = + " inclusive_scale(sum#cupti.host.duration,1e-9) as \"Host Time\"" + ",inclusive_scale(cupti.activity.duration,1e-9) as \"GPU Time\"" + ",inclusive_ratio(cupti.activity.duration,sum#cupti.host.duration,100.0) as \"GPU %\""; + + // Config for second aggregation step in MPI mode (cross-process aggregation) + std::string cross_select = + " avg(iscale#sum#cupti.host.duration) as \"Avg Host Time\"" + ",max(iscale#sum#cupti.host.duration) as \"Max Host Time\"" + ",avg(iscale#cupti.activity.duration) as \"Avg GPU Time\"" + ",max(iscale#cupti.activity.duration) as \"Max GPU Time\"" + ",ratio(iscale#cupti.activity.duration,iscale#sum#cupti.host.duration,100.0) as \"GPU %\""; + + std::string groupby = "path"; + + if (opts.is_enabled("show_kernels")) { + groupby += ",cupti.kernel.name"; + serial_select = std::string("cupti.kernel.name as Kernel,") + serial_select; + cross_select = std::string("cupti.kernel.name as Kernel,") + cross_select; + } + + std::string format = util::build_tree_format_spec(config(), opts); + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_LOCAL_CONFIG"] = + opts.build_query("local", { { "select", local_select }, { "group by", groupby } }); + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "cross", + { { "select", cross_select }, { "group by", groupby }, { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_REPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "select", serial_select }, { "group by", groupby }, { "format", format } } + ); } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; // Parse the "mpi=" argument -bool -use_mpi(const cali::ConfigManager::Options& opts) +bool use_mpi(const cali::ConfigManager::Options& opts) { auto services = services::get_available_services(); - bool have_mpireport = - std::find(services.begin(), services.end(), "mpireport") != services.end(); + bool have_mpireport = std::find(services.begin(), services.end(), "mpireport") != services.end(); bool use_mpi = have_mpireport; @@ -105,15 +99,17 @@ use_mpi(const cali::ConfigManager::Options& opts) if (use_mpi && !have_mpireport) { use_mpi = false; - Log(0).stream() << "cuda-activity: cannot enable mpi support: mpireport service is not available." - << std::endl; + Log(0).stream() << "cuda-activity: cannot enable mpi support: mpireport service is not available." << std::endl; } return use_mpi; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { return new CudaActivityReportController(use_mpi(opts), name, initial_cfg, opts); } @@ -151,14 +147,11 @@ const char* controller_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo cuda_activity_report_controller_info -{ - ::controller_spec, ::make_controller, nullptr -}; +ConfigManager::ConfigInfo cuda_activity_report_controller_info { ::controller_spec, ::make_controller, nullptr }; } diff --git a/src/caliper/controllers/HatchetRegionProfileController.cpp b/src/caliper/controllers/HatchetRegionProfileController.cpp index 94f4a0792..97491e291 100644 --- a/src/caliper/controllers/HatchetRegionProfileController.cpp +++ b/src/caliper/controllers/HatchetRegionProfileController.cpp @@ -24,70 +24,73 @@ class HatchetRegionProfileController : public cali::ChannelController { public: - HatchetRegionProfileController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts, const std::string& format) + HatchetRegionProfileController( + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts, + const std::string& format + ) : ChannelController(name, 0, initial_cfg) - { - std::string output(opts.get("output", "region_profile").to_string()); - - if (output != "stdout" && output != "stderr") { - auto pos = output.find_last_of('.'); - std::string ext = (format == "cali" ? ".cali" : ".json"); - - if (pos == std::string::npos || output.substr(pos) != ext) - output.append(ext); - } - - auto avail_services = services::get_available_services(); - bool have_mpi = - std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); - bool have_adiak = - std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); - - bool use_mpi = have_mpi; - - if (opts.is_set("use.mpi")) - use_mpi = have_mpi && opts.is_enabled("use.mpi"); - - if (have_adiak) { - config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); - config()["CALI_ADIAK_IMPORT_CATEGORIES"] = - opts.get("adiak.import_categories", "2,3").to_string(); - } - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_AGGREGATE_KEY" ] = "*,mpi.rank"; - config()["CALI_MPIREPORT_FILENAME"] = output; - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("local", { - { "let", "sum#time.duration=scale(sum#time.duration.ns,1e-9)" }, - { "select", "*,sum(sum#time.duration) as time unit sec" }, - { "group by", "path,mpi.rank" }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = output; - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "let", "sum#time.duration=scale(sum#time.duration.ns,1e-9)" }, - { "select", "*,sum(sum#time.duration) as time unit sec" }, - { "group by", "path" }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + std::string output(opts.get("output", "region_profile").to_string()); + + if (output != "stdout" && output != "stderr") { + auto pos = output.find_last_of('.'); + std::string ext = (format == "cali" ? ".cali" : ".json"); + + if (pos == std::string::npos || output.substr(pos) != ext) + output.append(ext); + } + + auto avail_services = services::get_available_services(); + bool have_mpi = std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); + bool have_adiak = + std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); + + bool use_mpi = have_mpi; + + if (opts.is_set("use.mpi")) + use_mpi = have_mpi && opts.is_enabled("use.mpi"); + + if (have_adiak) { + config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); + config()["CALI_ADIAK_IMPORT_CATEGORIES"] = opts.get("adiak.import_categories", "2,3").to_string(); + } + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_AGGREGATE_KEY"] = "*,mpi.rank"; + config()["CALI_MPIREPORT_FILENAME"] = output; + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "local", + { { "let", "sum#time.duration=scale(sum#time.duration.ns,1e-9)" }, + { "select", "*,sum(sum#time.duration) as time unit sec" }, + { "group by", "path,mpi.rank" }, + { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = output; + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "let", "sum#time.duration=scale(sum#time.duration.ns,1e-9)" }, + { "select", "*,sum(sum#time.duration) as time unit sec" }, + { "group by", "path" }, + { "format", format } } + ); } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; -std::string -check_args(const cali::ConfigManager::Options& opts) { +std::string check_args(const cali::ConfigManager::Options& opts) +{ // Check if output.format is valid - std::string format = opts.get("output.format", "json-split").to_string(); + std::string format = opts.get("output.format", "json-split").to_string(); std::set allowed_formats = { "cali", "json", "json-split", "hatchet" }; if (allowed_formats.find(format) == allowed_formats.end()) @@ -96,8 +99,11 @@ check_args(const cali::ConfigManager::Options& opts) { return ""; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { std::string format = opts.get("output.format", "cali").to_string(); @@ -106,8 +112,7 @@ make_controller(const char* name, const config_map_t& initial_cfg, const cali::C if (!(format == "json-split" || format == "json" || format == "cali")) { format = "json-split"; - Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format - << "\". Using json-split." + Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format << "\". Using json-split." << std::endl; } @@ -158,14 +163,11 @@ const char* controller_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo hatchet_region_profile_controller_info -{ - ::controller_spec, ::make_controller, ::check_args -}; +ConfigManager::ConfigInfo hatchet_region_profile_controller_info { ::controller_spec, ::make_controller, ::check_args }; } diff --git a/src/caliper/controllers/HatchetSampleProfileController.cpp b/src/caliper/controllers/HatchetSampleProfileController.cpp index 703e969cb..cab79976d 100644 --- a/src/caliper/controllers/HatchetSampleProfileController.cpp +++ b/src/caliper/controllers/HatchetSampleProfileController.cpp @@ -24,82 +24,78 @@ class HatchetSampleProfileController : public cali::ChannelController { public: - HatchetSampleProfileController(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts, const std::string& format) + HatchetSampleProfileController( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts, + const std::string& format + ) : ChannelController(name, 0, initial_cfg) - { - std::string freqstr = opts.get("sample.frequency", "200").to_string(); - - config()["CALI_SAMPLER_FREQUENCY"] = freqstr; - - std::string select = "path,count()"; - double freq = std::stod(freqstr); - - if (freq > 0) { - select.append(",scale_count("); - select.append(std::to_string(1.0/freq)); - select.append(") as time unit sec"); - } - - std::string output(opts.get("output", "sample_profile").to_string()); - - if (output != "stdout" && output != "stderr") { - auto pos = output.find_last_of('.'); - std::string ext = (format == "cali" ? ".cali" : ".json"); - - if (pos == std::string::npos || output.substr(pos) != ext) - output.append(ext); - } - - auto avail_services = services::get_available_services(); - bool have_mpi = - std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); - bool have_adiak = - std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); - bool have_pthread = - std::find(avail_services.begin(), avail_services.end(), "pthread") != avail_services.end(); - - bool use_mpi = have_mpi; - - if (opts.is_set("use.mpi")) - use_mpi = have_mpi && opts.is_enabled("use.mpi"); - - if (have_adiak) { - config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); - config()["CALI_ADIAK_IMPORT_CATEGORIES"] = - opts.get("adiak.import_categories", "2,3").to_string(); - } - - if (have_pthread) - config()["CALI_SERVICES_ENABLE"].append(",pthread"); - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_MPIREPORT_FILENAME"] = output; - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("local", { - { "select", select }, - { "group by", "path,mpi.rank" }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = output; - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "select", select }, - { "group by", "path" }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + std::string freqstr = opts.get("sample.frequency", "200").to_string(); + + config()["CALI_SAMPLER_FREQUENCY"] = freqstr; + + std::string select = "path,count()"; + double freq = std::stod(freqstr); + + if (freq > 0) { + select.append(",scale_count("); + select.append(std::to_string(1.0 / freq)); + select.append(") as time unit sec"); + } + + std::string output(opts.get("output", "sample_profile").to_string()); + + if (output != "stdout" && output != "stderr") { + auto pos = output.find_last_of('.'); + std::string ext = (format == "cali" ? ".cali" : ".json"); + + if (pos == std::string::npos || output.substr(pos) != ext) + output.append(ext); + } + + auto avail_services = services::get_available_services(); + bool have_mpi = std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); + bool have_adiak = + std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); + bool have_pthread = std::find(avail_services.begin(), avail_services.end(), "pthread") != avail_services.end(); + + bool use_mpi = have_mpi; + + if (opts.is_set("use.mpi")) + use_mpi = have_mpi && opts.is_enabled("use.mpi"); + + if (have_adiak) { + config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); + config()["CALI_ADIAK_IMPORT_CATEGORIES"] = opts.get("adiak.import_categories", "2,3").to_string(); + } + + if (have_pthread) + config()["CALI_SERVICES_ENABLE"].append(",pthread"); + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_MPIREPORT_FILENAME"] = output; + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "local", + { { "select", select }, { "group by", "path,mpi.rank" }, { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = output; + config()["CALI_REPORT_CONFIG"] = + opts.build_query("local", { { "select", select }, { "group by", "path" }, { "format", format } }); } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; -std::string -check_args(const cali::ConfigManager::Options& opts) { +std::string check_args(const cali::ConfigManager::Options& opts) +{ services::add_default_service_specs(); auto svcs = services::get_available_services(); @@ -114,7 +110,7 @@ check_args(const cali::ConfigManager::Options& opts) { // Check if output.format is valid // - std::string format = opts.get("output.format", "json-split").to_string(); + std::string format = opts.get("output.format", "json-split").to_string(); std::set allowed_formats = { "cali", "json", "json-split" }; if (allowed_formats.find(format) == allowed_formats.end()) @@ -123,8 +119,11 @@ check_args(const cali::ConfigManager::Options& opts) { return ""; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { std::string format = opts.get("output.format", "cali").to_string(); @@ -133,8 +132,7 @@ make_controller(const char* name, const config_map_t& initial_cfg, const cali::C if (!(format == "json-split" || format == "json" || format == "cali")) { format = "json-split"; - Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format - << "\". Using json-split." + Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format << "\". Using json-split." << std::endl; } @@ -182,14 +180,11 @@ const char* controller_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo hatchet_sample_profile_controller_info -{ - ::controller_spec, ::make_controller, ::check_args -}; +ConfigManager::ConfigInfo hatchet_sample_profile_controller_info { ::controller_spec, ::make_controller, ::check_args }; } diff --git a/src/caliper/controllers/LoopReportController.cpp b/src/caliper/controllers/LoopReportController.cpp index 8ff6b1dc2..fc0dcee3e 100644 --- a/src/caliper/controllers/LoopReportController.cpp +++ b/src/caliper/controllers/LoopReportController.cpp @@ -29,8 +29,8 @@ namespace struct LoopInfo { std::string name; - int iterations; - int count; + int iterations; + int count; }; LoopInfo get_loop_info(CaliperMetadataAccessInterface& db, const EntryList& rec) @@ -42,10 +42,10 @@ LoopInfo get_loop_info(CaliperMetadataAccessInterface& db, const EntryList& rec) Attribute lcnt_a = db.get_attribute("max#count"); for (const Entry& e : rec) { - if (e.attribute() == iter_a.id()) + if (e.attribute() == iter_a.id()) ret.iterations = e.value().to_int(); else if (e.attribute() == lcnt_a.id()) - ret.count = e.value().to_int(); + ret.count = e.value().to_int(); else { Variant v_loop = e.value(loop_a); if (!v_loop.empty()) @@ -56,30 +56,35 @@ LoopInfo get_loop_info(CaliperMetadataAccessInterface& db, const EntryList& rec) return ret; } - using Comm = cali::internal::CustomOutputController::Comm; class LoopReportController : public cali::internal::CustomOutputController { cali::ConfigManager::Options m_opts; - Aggregator local_aggregate(Caliper& c, CaliperMetadataDB& db, const QuerySpec& spec) { + Aggregator local_aggregate(Caliper& c, CaliperMetadataDB& db, const QuerySpec& spec) + { RecordSelector filter(spec); Preprocessor prp(spec); Aggregator agg(spec); - Channel chn(channel()); - c.flush(&chn, SnapshotView(), [&db,&filter,&prp,&agg](CaliperMetadataAccessInterface& in_db, const EntryList& rec){ + Channel chn(channel()); + c.flush( + &chn, + SnapshotView(), + [&db, &filter, &prp, &agg](CaliperMetadataAccessInterface& in_db, const EntryList& rec) { EntryList mrec = prp.process(db, db.merge_snapshot(in_db, rec)); if (filter.pass(db, mrec)) agg.add(db, mrec); - }); + } + ); return agg; } - Aggregator summary_local_aggregation(Caliper& c, CaliperMetadataDB& db) { + Aggregator summary_local_aggregation(Caliper& c, CaliperMetadataDB& db) + { const char* select = " loop" ",count()" @@ -89,17 +94,19 @@ class LoopReportController : public cali::internal::CustomOutputController ",max(iter_per_sec)" ",avg(iter_per_sec)"; - std::string query = m_opts.build_query("local", { - { "let", "iter_per_sec = ratio(loop.iterations,time.duration.ns,1e9)" }, - { "select", select }, - { "group by", "loop" }, - { "where", "loop" } - }); + std::string query = m_opts.build_query( + "local", + { { "let", "iter_per_sec = ratio(loop.iterations,time.duration.ns,1e9)" }, + { "select", select }, + { "group by", "loop" }, + { "where", "loop" } } + ); return local_aggregate(c, db, CalQLParser(query.c_str()).spec()); } - QuerySpec summary_query() { + QuerySpec summary_query() + { const char* select = " loop as Loop" ",max(sum#loop.iterations) as \"Iterations\"" @@ -108,48 +115,57 @@ class LoopReportController : public cali::internal::CustomOutputController ",max(max#iter_per_sec) as \"Iter/s (max)\"" ",ratio(sum#loop.iterations,scale#time.duration.ns) as \"Iter/s (avg)\""; - std::string query = m_opts.build_query("cross", { - { "select", select }, + std::string query = m_opts.build_query( + "cross", + { + { "select", select }, { "aggregate", "max(count)" }, - { "group by", "loop" }, - { "format", "table" }, - }); + { "group by", "loop" }, + { "format", "table" }, + } + ); return CalQLParser(query.c_str()).spec(); } - Aggregator timeseries_local_aggregation(Caliper& c, CaliperMetadataDB& db, const std::string& loopname, int blocksize) { + Aggregator timeseries_local_aggregation( + Caliper& c, + CaliperMetadataDB& db, + const std::string& loopname, + int blocksize + ) + { const char* select = " Block" ",scale(time.duration.ns,1e-9)" ",sum(loop.iterations)" ",ratio(loop.iterations,time.duration.ns,1e9)"; - std::string block = - std::string("Block = truncate(loop.start_iteration,") + std::to_string(blocksize) + ")"; + std::string block = std::string("Block = truncate(loop.start_iteration,") + std::to_string(blocksize) + ")"; - std::string query = m_opts.build_query("local", { - { "let", block }, - { "select", select }, - { "group by", "Block" }, - { "where", std::string("loop=\"")+loopname+"\"" } - }); + std::string query = m_opts.build_query( + "local", + { { "let", block }, + { "select", select }, + { "group by", "Block" }, + { "where", std::string("loop=\"") + loopname + "\"" } } + ); return local_aggregate(c, db, CalQLParser(query.c_str()).spec()); } - QuerySpec timeseries_spec() { + QuerySpec timeseries_spec() + { const char* select = " Block" ",max(sum#loop.iterations) as \"Iterations\"" ",max(scale#time.duration.ns) as \"Time (s)\"" ",avg(ratio#loop.iterations/time.duration.ns) as \"Iter/s\""; - std::string query = m_opts.build_query("cross", { - { "select", select }, - { "group by", "Block" }, - { "format", "table order by Block" } - }); + std::string query = m_opts.build_query( + "cross", + { { "select", select }, { "group by", "Block" }, { "format", "table order by Block" } } + ); CalQLParser parser(query.c_str()); @@ -159,10 +175,11 @@ class LoopReportController : public cali::internal::CustomOutputController return CalQLParser(query.c_str()).spec(); } - void process_timeseries(Caliper& c, CaliperMetadataDB& db, OutputStream& stream, const LoopInfo& info, Comm comm) { - int iterations = comm.bcast_int(info.iterations); - int rec_count = comm.bcast_int(info.count); - std::string name = comm.bcast_str(info.name); + void process_timeseries(Caliper& c, CaliperMetadataDB& db, OutputStream& stream, const LoopInfo& info, Comm comm) + { + int iterations = comm.bcast_int(info.iterations); + int rec_count = comm.bcast_int(info.count); + std::string name = comm.bcast_str(info.name); if (iterations > 0) { int nblocks = 20; @@ -192,18 +209,20 @@ class LoopReportController : public cali::internal::CustomOutputController } } - OutputStream create_stream() { + OutputStream create_stream() + { std::string output = m_opts.get("output", "stdout").to_string(); - Caliper c; + Caliper c; OutputStream stream; stream.set_filename(output.c_str(), c, c.get_globals()); return stream; } - void collective_flush(OutputStream& stream, Comm& comm) override { - Caliper c; + void collective_flush(OutputStream& stream, Comm& comm) override + { + Caliper c; CaliperMetadataDB db; Aggregator summary_local_agg = summary_local_aggregation(c, db); @@ -240,9 +259,9 @@ class LoopReportController : public cali::internal::CustomOutputController if (print_timeseries) { std::vector infovec; - summary_cross_agg.flush(db, [&infovec](CaliperMetadataAccessInterface& db, const EntryList& rec){ - infovec.push_back(get_loop_info(db, rec)); - }); + summary_cross_agg.flush(db, [&infovec](CaliperMetadataAccessInterface& db, const EntryList& rec) { + infovec.push_back(get_loop_info(db, rec)); + }); if (!infovec.empty()) { for (const LoopInfo& loopinfo : infovec) @@ -257,8 +276,7 @@ class LoopReportController : public cali::internal::CustomOutputController public: LoopReportController(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) - : cali::internal::CustomOutputController(name, 0, initial_cfg), - m_opts(opts) + : cali::internal::CustomOutputController(name, 0, initial_cfg), m_opts(opts) { if (opts.is_set("iteration_interval")) config()["CALI_LOOP_MONITOR_ITERATION_INTERVAL"] = opts.get("iteration_interval").to_string(); @@ -268,7 +286,7 @@ class LoopReportController : public cali::internal::CustomOutputController config()["CALI_LOOP_MONITOR_TIME_INTERVAL"] = "0.5"; if (opts.is_set("target_loops")) - config()["CALI_LOOP_MONITOR_TARGET_LOOPS" ] = opts.get("target_loops").to_string(); + config()["CALI_LOOP_MONITOR_TARGET_LOOPS"] = opts.get("target_loops").to_string(); opts.update_channel_config(config()); opts.update_channel_metadata(metadata()); @@ -321,19 +339,22 @@ const char* loop_report_controller_spec = " ]" "}"; -cali::ChannelController* -make_loopreport_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) { +cali::ChannelController* make_loopreport_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) +{ return new LoopReportController(name, initial_cfg, opts); } -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo loop_report_controller_info -{ - ::loop_report_controller_spec, ::make_loopreport_controller, nullptr -}; +ConfigManager::ConfigInfo loop_report_controller_info { ::loop_report_controller_spec, + ::make_loopreport_controller, + nullptr }; } diff --git a/src/caliper/controllers/OpenMPReportController.cpp b/src/caliper/controllers/OpenMPReportController.cpp index 4a5b859b7..bd64ebe47 100644 --- a/src/caliper/controllers/OpenMPReportController.cpp +++ b/src/caliper/controllers/OpenMPReportController.cpp @@ -21,97 +21,91 @@ class OpenMPReportController : public cali::ChannelController { public: - OpenMPReportController(bool use_mpi, const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) + OpenMPReportController( + bool use_mpi, + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts + ) : ChannelController(name, 0, initial_cfg) - { - const char* let = - " sum#time.duration=scale(sum#time.duration.ns,1e-9)" - ",n.threads=first(omp.num.threads)" - ",t.initial=first(sum#time.duration) if omp.thread.type=initial"; - - // Config for first aggregation step in MPI mode (process-local aggregation) - std::string local_select = - " max(n.threads)" - ",inclusive_sum(t.initial)" - ",inclusive_sum(sum#time.duration)"; - // Config for serial-mode aggregation - std::string serial_select = - " max(n.threads) as \"#Threads\"" - ",inclusive_sum(t.initial) as \"Time (thread)\"" - ",inclusive_sum(sum#time.duration) as \"Time (total)\""; - - // Config for second aggregation step in MPI mode (cross-process aggregation) - std::string cross_select = - " max(max#n.threads) as \"#Threads\"" - ",avg(inclusive#t.initial) as \"Time (thread) (avg)\"" - ",sum(inclusive#sum#time.duration) as \"Time (total)\""; - - std::string groupby; - std::string format("table"); - - if (opts.is_enabled("show_threads")) { - groupby.append(groupby.empty() ? "omp.thread.id" : ",omp.thread.id"); - - serial_select.append(",omp.thread.id as Thread"); - cross_select.append(",omp.thread.id as Thread"); - } - - if (opts.is_enabled("show_thread_type")) { - groupby.append(groupby.empty() ? "omp.thread.type" : ",omp.thread.type"); - - serial_select.append(",omp.thread.type as Type"); - cross_select.append(",omp.thread.type as Type"); - } - - if (opts.is_enabled("show_regions")) { - groupby.append(groupby.empty() ? "path" : ",path"); - format = "tree"; - } - - if (groupby.empty()) - groupby = "none"; - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_LOCAL_CONFIG"] = - opts.build_query("local", { - { "let", let }, - { "select", local_select }, - { "group by", groupby } - }); - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("cross", { - { "select", cross_select }, - { "group by", groupby }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string(); - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "let", let }, - { "select", serial_select }, - { "group by", groupby }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + const char* let = + " sum#time.duration=scale(sum#time.duration.ns,1e-9)" + ",n.threads=first(omp.num.threads)" + ",t.initial=first(sum#time.duration) if omp.thread.type=initial"; + + // Config for first aggregation step in MPI mode (process-local aggregation) + std::string local_select = + " max(n.threads)" + ",inclusive_sum(t.initial)" + ",inclusive_sum(sum#time.duration)"; + // Config for serial-mode aggregation + std::string serial_select = + " max(n.threads) as \"#Threads\"" + ",inclusive_sum(t.initial) as \"Time (thread)\"" + ",inclusive_sum(sum#time.duration) as \"Time (total)\""; + + // Config for second aggregation step in MPI mode (cross-process aggregation) + std::string cross_select = + " max(max#n.threads) as \"#Threads\"" + ",avg(inclusive#t.initial) as \"Time (thread) (avg)\"" + ",sum(inclusive#sum#time.duration) as \"Time (total)\""; + + std::string groupby; + std::string format("table"); + + if (opts.is_enabled("show_threads")) { + groupby.append(groupby.empty() ? "omp.thread.id" : ",omp.thread.id"); + + serial_select.append(",omp.thread.id as Thread"); + cross_select.append(",omp.thread.id as Thread"); } + + if (opts.is_enabled("show_thread_type")) { + groupby.append(groupby.empty() ? "omp.thread.type" : ",omp.thread.type"); + + serial_select.append(",omp.thread.type as Type"); + cross_select.append(",omp.thread.type as Type"); + } + + if (opts.is_enabled("show_regions")) { + groupby.append(groupby.empty() ? "path" : ",path"); + format = "tree"; + } + + if (groupby.empty()) + groupby = "none"; + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_LOCAL_CONFIG"] = + opts.build_query("local", { { "let", let }, { "select", local_select }, { "group by", groupby } }); + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "cross", + { { "select", cross_select }, { "group by", groupby }, { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "let", let }, { "select", serial_select }, { "group by", groupby }, { "format", format } } + ); + } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; // Parse the "mpi=" argument -bool -use_mpi(const cali::ConfigManager::Options& opts) +bool use_mpi(const cali::ConfigManager::Options& opts) { auto services = services::get_available_services(); - bool have_mpireport = - std::find(services.begin(), services.end(), "mpireport") != services.end(); + bool have_mpireport = std::find(services.begin(), services.end(), "mpireport") != services.end(); bool use_mpi = have_mpireport; @@ -120,15 +114,17 @@ use_mpi(const cali::ConfigManager::Options& opts) if (use_mpi && !have_mpireport) { use_mpi = false; - Log(0).stream() << "openmp-report: cannot enable mpi support: mpireport service is not available." - << std::endl; + Log(0).stream() << "openmp-report: cannot enable mpi support: mpireport service is not available." << std::endl; } return use_mpi; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { return new OpenMPReportController(use_mpi(opts), name, initial_cfg, opts); } @@ -173,14 +169,11 @@ const char* controller_spec = " ]" "}"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo openmp_report_controller_info -{ - ::controller_spec, ::make_controller, nullptr -}; +ConfigManager::ConfigInfo openmp_report_controller_info { ::controller_spec, ::make_controller, nullptr }; } diff --git a/src/caliper/controllers/ROCmActivityProfileController.cpp b/src/caliper/controllers/ROCmActivityProfileController.cpp index de60acf0a..c0d9e1271 100644 --- a/src/caliper/controllers/ROCmActivityProfileController.cpp +++ b/src/caliper/controllers/ROCmActivityProfileController.cpp @@ -24,73 +24,75 @@ class RocmActivityProfileController : public cali::ChannelController { public: - RocmActivityProfileController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts, const std::string& format) + RocmActivityProfileController( + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts, + const std::string& format + ) : ChannelController(name, 0, initial_cfg) - { - std::string output(opts.get("output", "rocm_profile").to_string()); - - if (output != "stdout" && output != "stderr") { - auto pos = output.find_last_of('.'); - std::string ext = (format == "cali" ? ".cali" : ".json"); - - if (pos == std::string::npos || output.substr(pos) != ext) - output.append(ext); - } - - auto avail_services = services::get_available_services(); - bool have_mpi = - std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); - bool have_adiak = - std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); - - bool use_mpi = have_mpi; - - if (opts.is_set("use.mpi")) - use_mpi = have_mpi && opts.is_enabled("use.mpi"); - - if (have_adiak) { - config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); - config()["CALI_ADIAK_IMPORT_CATEGORIES"] = - opts.get("adiak.import_categories", "2,3").to_string(); - } - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_AGGREGATE_KEY" ] = "*,mpi.rank"; - config()["CALI_MPIREPORT_FILENAME"] = output; - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("local", { - { "select", - "*,scale(sum#rocm.activity.duration,1e-9) as \"time (gpu)\" unit sec" - " ,scale(sum#time.duration.ns,1e-9) as \"time\" unit sec" - }, - { "group by", "path,rocm.kernel.name,rocm.activity.kind,mpi.rank" }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = output; - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "select", - "*,scale(sum#rocm.activity.duration,1e-9) as \"time (gpu)\" unit sec" - " ,scale(sum#time.duration.ns,1e-9) as \"time\" unit sec" }, - { "group by", "path,rocm.kernel.name,rocm.activity.kind" }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + std::string output(opts.get("output", "rocm_profile").to_string()); + + if (output != "stdout" && output != "stderr") { + auto pos = output.find_last_of('.'); + std::string ext = (format == "cali" ? ".cali" : ".json"); + + if (pos == std::string::npos || output.substr(pos) != ext) + output.append(ext); + } + + auto avail_services = services::get_available_services(); + bool have_mpi = std::find(avail_services.begin(), avail_services.end(), "mpireport") != avail_services.end(); + bool have_adiak = + std::find(avail_services.begin(), avail_services.end(), "adiak_import") != avail_services.end(); + + bool use_mpi = have_mpi; + + if (opts.is_set("use.mpi")) + use_mpi = have_mpi && opts.is_enabled("use.mpi"); + + if (have_adiak) { + config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); + config()["CALI_ADIAK_IMPORT_CATEGORIES"] = opts.get("adiak.import_categories", "2,3").to_string(); } + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_AGGREGATE_KEY"] = "*,mpi.rank"; + config()["CALI_MPIREPORT_FILENAME"] = output; + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "local", + { { "select", + "*,scale(sum#rocm.activity.duration,1e-9) as \"time (gpu)\" unit sec" + " ,scale(sum#time.duration.ns,1e-9) as \"time\" unit sec" }, + { "group by", "path,rocm.kernel.name,rocm.activity.kind,mpi.rank" }, + { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = output; + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "select", + "*,scale(sum#rocm.activity.duration,1e-9) as \"time (gpu)\" unit sec" + " ,scale(sum#time.duration.ns,1e-9) as \"time\" unit sec" }, + { "group by", "path,rocm.kernel.name,rocm.activity.kind" }, + { "format", format } } + ); + } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; -std::string -check_args(const cali::ConfigManager::Options& opts) { +std::string check_args(const cali::ConfigManager::Options& opts) +{ // Check if output.format is valid - std::string format = opts.get("output.format", "cali").to_string(); + std::string format = opts.get("output.format", "cali").to_string(); std::set allowed_formats = { "cali", "json", "json-split", "hatchet" }; if (allowed_formats.find(format) == allowed_formats.end()) @@ -99,8 +101,11 @@ check_args(const cali::ConfigManager::Options& opts) { return ""; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { std::string format = opts.get("output.format", "cali").to_string(); @@ -109,8 +114,7 @@ make_controller(const char* name, const config_map_t& initial_cfg, const cali::C if (!(format == "json-split" || format == "json" || format == "cali")) { format = "cali"; - Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format - << "\". Using json-split." + Log(0).stream() << "hatchet-region-profile: Unknown output format \"" << format << "\". Using json-split." << std::endl; } @@ -147,14 +151,11 @@ const char* controller_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo rocm_activity_profile_controller_info -{ - ::controller_spec, ::make_controller, ::check_args -}; +ConfigManager::ConfigInfo rocm_activity_profile_controller_info { ::controller_spec, ::make_controller, ::check_args }; } diff --git a/src/caliper/controllers/ROCmActivityReportController.cpp b/src/caliper/controllers/ROCmActivityReportController.cpp index 10fd63f53..eed301adc 100644 --- a/src/caliper/controllers/ROCmActivityReportController.cpp +++ b/src/caliper/controllers/ROCmActivityReportController.cpp @@ -23,80 +23,74 @@ class RocmActivityReportController : public cali::ChannelController { public: - RocmActivityReportController(bool use_mpi, const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) + RocmActivityReportController( + bool use_mpi, + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts + ) : ChannelController(name, 0, initial_cfg) - { - // Config for first aggregation step in MPI mode (process-local aggregation) - std::string local_select = - " inclusive_scale(sum#time.duration.ns,1e-9)" - ",inclusive_scale(sum#rocm.activity.duration,1e-9)"; - // Config for serial-mode aggregation - std::string serial_select = - " inclusive_scale(sum#time.duration.ns,1e-9) as \"Host Time\"" - ",inclusive_scale(sum#rocm.activity.duration,1e-9) as \"GPU Time\"" - ",inclusive_ratio(sum#rocm.activity.duration,sum#time.duration.ns,100.0) as \"GPU %\""; - - // Config for second aggregation step in MPI mode (cross-process aggregation) - std::string cross_select = - " avg(inclusive#scale#time.duration.ns) as \"Avg Host Time\"" - ",max(inclusive#scale#time.duration.ns) as \"Max Host Time\"" - ",avg(iscale#sum#rocm.activity.duration) as \"Avg GPU Time\"" - ",max(iscale#sum#rocm.activity.duration) as \"Max GPU Time\"" - ",ratio(iscale#sum#rocm.activity.duration,iscale#sum#time.duration.ns,100.0) as \"GPU %\""; - - std::string groupby = "path"; - - if (opts.is_enabled("show_kernels")) { - groupby += ",rocm.kernel.name"; - serial_select - = std::string("rocm.kernel.name as Kernel,") + serial_select; - cross_select - = std::string("rocm.kernel.name as Kernel,") + cross_select; - } - - std::string format = util::build_tree_format_spec(config(), opts); - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); - config()["CALI_MPIREPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_LOCAL_CONFIG"] = - opts.build_query("local", { - { "select", local_select }, - { "group by", groupby } - }); - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("cross", { - { "select", cross_select }, - { "group by", groupby }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string(); - config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "select", serial_select }, - { "group by", groupby }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + // Config for first aggregation step in MPI mode (process-local aggregation) + std::string local_select = + " inclusive_scale(sum#time.duration.ns,1e-9)" + ",inclusive_scale(sum#rocm.activity.duration,1e-9)"; + // Config for serial-mode aggregation + std::string serial_select = + " inclusive_scale(sum#time.duration.ns,1e-9) as \"Host Time\"" + ",inclusive_scale(sum#rocm.activity.duration,1e-9) as \"GPU Time\"" + ",inclusive_ratio(sum#rocm.activity.duration,sum#time.duration.ns,100.0) as \"GPU %\""; + + // Config for second aggregation step in MPI mode (cross-process aggregation) + std::string cross_select = + " avg(inclusive#scale#time.duration.ns) as \"Avg Host Time\"" + ",max(inclusive#scale#time.duration.ns) as \"Max Host Time\"" + ",avg(iscale#sum#rocm.activity.duration) as \"Avg GPU Time\"" + ",max(iscale#sum#rocm.activity.duration) as \"Max GPU Time\"" + ",ratio(iscale#sum#rocm.activity.duration,iscale#sum#time.duration.ns,100.0) as \"GPU %\""; + + std::string groupby = "path"; + + if (opts.is_enabled("show_kernels")) { + groupby += ",rocm.kernel.name"; + serial_select = std::string("rocm.kernel.name as Kernel,") + serial_select; + cross_select = std::string("rocm.kernel.name as Kernel,") + cross_select; + } + + std::string format = util::build_tree_format_spec(config(), opts); + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_LOCAL_CONFIG"] = + opts.build_query("local", { { "select", local_select }, { "group by", groupby } }); + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "cross", + { { "select", cross_select }, { "group by", groupby }, { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_REPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "select", serial_select }, { "group by", groupby }, { "format", format } } + ); } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } }; // Parse the "mpi=" argument -bool -use_mpi(const cali::ConfigManager::Options& opts) +bool use_mpi(const cali::ConfigManager::Options& opts) { auto services = services::get_available_services(); - bool have_mpireport = - std::find(services.begin(), services.end(), "mpireport") != services.end(); + bool have_mpireport = std::find(services.begin(), services.end(), "mpireport") != services.end(); bool use_mpi = have_mpireport; @@ -105,15 +99,17 @@ use_mpi(const cali::ConfigManager::Options& opts) if (use_mpi && !have_mpireport) { use_mpi = false; - Log(0).stream() << "rocm-activity: cannot enable mpi support: mpireport service is not available." - << std::endl; + Log(0).stream() << "rocm-activity: cannot enable mpi support: mpireport service is not available." << std::endl; } return use_mpi; } -cali::ChannelController* -make_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { return new RocmActivityReportController(use_mpi(opts), name, initial_cfg, opts); } @@ -152,14 +148,11 @@ const char* controller_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo rocm_activity_report_controller_info -{ - ::controller_spec, ::make_controller, nullptr -}; +ConfigManager::ConfigInfo rocm_activity_report_controller_info { ::controller_spec, ::make_controller, nullptr }; } diff --git a/src/caliper/controllers/RuntimeReportController.cpp b/src/caliper/controllers/RuntimeReportController.cpp index 39a024f9e..d647a2b9d 100644 --- a/src/caliper/controllers/RuntimeReportController.cpp +++ b/src/caliper/controllers/RuntimeReportController.cpp @@ -23,84 +23,71 @@ class RuntimeReportController : public cali::ChannelController { public: - RuntimeReportController(bool use_mpi, const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) + RuntimeReportController( + bool use_mpi, + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts + ) : ChannelController(name, 0, initial_cfg) - { - std::string local_let = - "sum#time.duration=scale(sum#time.duration.ns,1e-9)"; - - // Config for first aggregation step in MPI mode (process-local aggregation) - std::string local_select = - " sum(sum#time.duration)"; - // Config for serial-mode aggregation - std::string serial_select = - " sum(sum#time.duration) as \"Time (E)\"" - ",inclusive_sum(sum#time.duration) as \"Time (I)\"" - ",percent_total(sum#time.duration) as \"Time % (E)\"" - ",inclusive_percent_total(sum#time.duration) as \"Time % (I)\""; - - std::string tmetric = "sum#sum#time.duration"; - std::string pmetric = "percent_total(sum#sum#time.duration)"; - - if (opts.is_enabled("calc.inclusive")) { - local_select += ",inclusive_sum(sum#time.duration)"; - tmetric = "inclusive#sum#time.duration"; - pmetric = "inclusive_percent_total(sum#sum#time.duration)"; - } - - std::string format = util::build_tree_format_spec(config(), opts); - - // Config for second aggregation step in MPI mode (cross-process aggregation) - std::string cross_select = - std::string(" min(") + tmetric + ") as \"Min time/rank\"" - + std::string(",max(") + tmetric + ") as \"Max time/rank\"" - + std::string(",avg(") + tmetric + ") as \"Avg time/rank\"" - + std::string("," ) + pmetric + " as \"Time %\""; - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); - config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append").to_string(); - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_LOCAL_CONFIG"] = - opts.build_query("local", { - { "let", local_let }, - { "select", local_select }, - { "group by", "path" } - }); - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("cross", { - { "select", cross_select }, - { "group by", "path" }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string(); - config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "let", local_let }, - { "select", serial_select }, - { "group by", "path" }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + std::string local_let = "sum#time.duration=scale(sum#time.duration.ns,1e-9)"; + + // Config for first aggregation step in MPI mode (process-local aggregation) + std::string local_select = " sum(sum#time.duration)"; + // Config for serial-mode aggregation + std::string serial_select = + " sum(sum#time.duration) as \"Time (E)\"" + ",inclusive_sum(sum#time.duration) as \"Time (I)\"" + ",percent_total(sum#time.duration) as \"Time % (E)\"" + ",inclusive_percent_total(sum#time.duration) as \"Time % (I)\""; + + std::string tmetric = "sum#sum#time.duration"; + std::string pmetric = "percent_total(sum#sum#time.duration)"; + + if (opts.is_enabled("calc.inclusive")) { + local_select += ",inclusive_sum(sum#time.duration)"; + tmetric = "inclusive#sum#time.duration"; + pmetric = "inclusive_percent_total(sum#sum#time.duration)"; } -}; + std::string format = util::build_tree_format_spec(config(), opts); + + // Config for second aggregation step in MPI mode (cross-process aggregation) + std::string cross_select = std::string(" min(") + tmetric + ") as \"Min time/rank\"" + std::string(",max(") + + tmetric + ") as \"Max time/rank\"" + std::string(",avg(") + tmetric + + ") as \"Avg time/rank\"" + std::string(",") + pmetric + " as \"Time %\""; + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_LOCAL_CONFIG"] = + opts.build_query("local", { { "let", local_let }, { "select", local_select }, { "group by", "path" } }); + config()["CALI_MPIREPORT_CONFIG"] = + opts.build_query("cross", { { "select", cross_select }, { "group by", "path" }, { "format", format } }); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_REPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "let", local_let }, { "select", serial_select }, { "group by", "path" }, { "format", format } } + ); + } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } +}; // Parse the "mpi=" argument -bool -use_mpi(const cali::ConfigManager::Options& opts) +bool use_mpi(const cali::ConfigManager::Options& opts) { auto services = services::get_available_services(); - bool have_mpireport = - std::find(services.begin(), services.end(), "mpireport") != services.end(); + bool have_mpireport = std::find(services.begin(), services.end(), "mpireport") != services.end(); bool use_mpi = have_mpireport; @@ -116,8 +103,11 @@ use_mpi(const cali::ConfigManager::Options& opts) return use_mpi; } -cali::ChannelController* -make_runtime_report_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_runtime_report_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { return new RuntimeReportController(use_mpi(opts), name, initial_cfg, opts); } @@ -172,14 +162,13 @@ const char* runtime_report_spec = R"json( }; )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo runtime_report_controller_info -{ - ::runtime_report_spec, ::make_runtime_report_controller, nullptr -}; +ConfigManager::ConfigInfo runtime_report_controller_info { ::runtime_report_spec, + ::make_runtime_report_controller, + nullptr }; } diff --git a/src/caliper/controllers/SampleReportController.cpp b/src/caliper/controllers/SampleReportController.cpp index 2baab72b8..1adf8e048 100644 --- a/src/caliper/controllers/SampleReportController.cpp +++ b/src/caliper/controllers/SampleReportController.cpp @@ -23,87 +23,80 @@ class SampleReportController : public cali::ChannelController { public: - SampleReportController(bool use_mpi, const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) + SampleReportController( + bool use_mpi, + const char* name, + const config_map_t& initial_cfg, + const ConfigManager::Options& opts + ) : ChannelController(name, 0, initial_cfg) - { - double freq = std::max(1.0, std::stod(opts.get("sample.frequency", "200").to_string())); - - config()["CALI_SAMPLER_FREQUENCY"] = std::to_string(freq); - - // Config for first aggregation step in MPI mode (process-local aggregation) - std::string local_select = std::string("count() as \"Samples\",scale_count(") - + std::to_string(1.0/freq) - + ") as \"Time (sec)\" unit sec"; - - // Config for second aggregation step in MPI mode (cross-process aggregation) - std::string cross_select = - " min(scount) as \"Min time/rank\" unit sec" - ",max(scount) as \"Max time/rank\" unit sec" - ",avg(scount) as \"Avg time/rank\" unit sec" - ",sum(scount) as \"Total time\" unit sec" - ",percent_total(scount) as \"Time %\""; - - auto avail_services = services::get_available_services(); - bool have_pthread = - std::find(avail_services.begin(), avail_services.end(), "pthread") != avail_services.end(); - - bool use_callpath = opts.is_enabled("callpath"); - - const char* groupby = "path"; - const char* fmt_in = ""; - - if (use_callpath) { - groupby = "source.function#callpath.address"; - fmt_in = "path-attributes=source.function#callpath.address"; - } - - std::string format = util::build_tree_format_spec(config(), opts, fmt_in); - - if (have_pthread) - config()["CALI_SERVICES_ENABLE"].append(",pthread"); - - if (use_mpi) { - config()["CALI_SERVICES_ENABLE" ].append(",mpi,mpireport"); - config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); - config()["CALI_MPIREPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; - config()["CALI_MPIREPORT_LOCAL_CONFIG"] = - opts.build_query("local", { - { "select", local_select }, - { "group by", groupby } - }); - config()["CALI_MPIREPORT_CONFIG" ] = - opts.build_query("cross", { - { "select", cross_select }, - { "group by", groupby }, - { "format", format } - }); - } else { - config()["CALI_SERVICES_ENABLE" ].append(",report"); - config()["CALI_REPORT_FILENAME" ] = opts.get("output", "stderr").to_string(); - config()["CALI_REPORT_APPEND" ] = opts.get("output.append").to_string(); - config()["CALI_REPORT_CONFIG" ] = - opts.build_query("local", { - { "select", local_select }, - { "group by", groupby }, - { "format", format } - }); - } - - opts.update_channel_config(config()); - opts.update_channel_metadata(metadata()); + { + double freq = std::max(1.0, std::stod(opts.get("sample.frequency", "200").to_string())); + + config()["CALI_SAMPLER_FREQUENCY"] = std::to_string(freq); + + // Config for first aggregation step in MPI mode (process-local aggregation) + std::string local_select = std::string("count() as \"Samples\",scale_count(") + std::to_string(1.0 / freq) + + ") as \"Time (sec)\" unit sec"; + + // Config for second aggregation step in MPI mode (cross-process aggregation) + std::string cross_select = + " min(scount) as \"Min time/rank\" unit sec" + ",max(scount) as \"Max time/rank\" unit sec" + ",avg(scount) as \"Avg time/rank\" unit sec" + ",sum(scount) as \"Total time\" unit sec" + ",percent_total(scount) as \"Time %\""; + + auto avail_services = services::get_available_services(); + bool have_pthread = std::find(avail_services.begin(), avail_services.end(), "pthread") != avail_services.end(); + + bool use_callpath = opts.is_enabled("callpath"); + + const char* groupby = "path"; + const char* fmt_in = ""; + + if (use_callpath) { + groupby = "source.function#callpath.address"; + fmt_in = "path-attributes=source.function#callpath.address"; } -}; + std::string format = util::build_tree_format_spec(config(), opts, fmt_in); + + if (have_pthread) + config()["CALI_SERVICES_ENABLE"].append(",pthread"); + + if (use_mpi) { + config()["CALI_SERVICES_ENABLE"].append(",mpi,mpireport"); + config()["CALI_MPIREPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_MPIREPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_MPIREPORT_WRITE_ON_FINALIZE"] = "false"; + config()["CALI_MPIREPORT_LOCAL_CONFIG"] = + opts.build_query("local", { { "select", local_select }, { "group by", groupby } }); + config()["CALI_MPIREPORT_CONFIG"] = opts.build_query( + "cross", + { { "select", cross_select }, { "group by", groupby }, { "format", format } } + ); + } else { + config()["CALI_SERVICES_ENABLE"].append(",report"); + config()["CALI_REPORT_FILENAME"] = opts.get("output", "stderr").to_string(); + config()["CALI_REPORT_APPEND"] = opts.get("output.append").to_string(); + config()["CALI_REPORT_CONFIG"] = opts.build_query( + "local", + { { "select", local_select }, { "group by", groupby }, { "format", format } } + ); + } + + opts.update_channel_config(config()); + opts.update_channel_metadata(metadata()); + } +}; // Parse the "mpi=" argument -bool -use_mpi(const cali::ConfigManager::Options& opts) +bool use_mpi(const cali::ConfigManager::Options& opts) { auto services = services::get_available_services(); - bool have_mpireport = - std::find(services.begin(), services.end(), "mpireport") != services.end(); + bool have_mpireport = std::find(services.begin(), services.end(), "mpireport") != services.end(); bool use_mpi = have_mpireport; @@ -112,15 +105,17 @@ use_mpi(const cali::ConfigManager::Options& opts) if (use_mpi && !have_mpireport) { use_mpi = false; - Log(0).stream() << "sample-report: cannot enable mpi support: mpireport service is not available." - << std::endl; + Log(0).stream() << "sample-report: cannot enable mpi support: mpireport service is not available." << std::endl; } return use_mpi; } -cali::ChannelController* -make_sample_report_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_sample_report_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) { return new SampleReportController(use_mpi(opts), name, initial_cfg, opts); } @@ -160,14 +155,13 @@ const char* sample_report_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo sample_report_controller_info -{ - ::sample_report_spec, ::make_sample_report_controller, nullptr -}; +ConfigManager::ConfigInfo sample_report_controller_info { ::sample_report_spec, + ::make_sample_report_controller, + nullptr }; } diff --git a/src/caliper/controllers/SpotController.cpp b/src/caliper/controllers/SpotController.cpp index 52dab299b..cc3178901 100644 --- a/src/caliper/controllers/SpotController.cpp +++ b/src/caliper/controllers/SpotController.cpp @@ -31,33 +31,35 @@ constexpr int spot_format_version = 2; // Helper functions // -QuerySpec -parse_spec(const char* query) +QuerySpec parse_spec(const char* query) { CalQLParser parser(query); if (parser.error()) - Log(0).stream() << "[spot controller]: Internal query parse error: " << parser.error_msg() - << std::endl; + Log(0).stream() << "[spot controller]: Internal query parse error: " << parser.error_msg() << std::endl; return parser.spec(); } /// \brief Perform process-local aggregation of channel data into \a output_agg -void -local_aggregate(const char* query, Caliper& c, Channel& channel, CaliperMetadataDB& db, Aggregator& output_agg) { - QuerySpec spec(parse_spec(query)); +void local_aggregate(const char* query, Caliper& c, Channel& channel, CaliperMetadataDB& db, Aggregator& output_agg) +{ + QuerySpec spec(parse_spec(query)); RecordSelector filter(spec); Preprocessor prp(spec); Aggregator agg(spec); - c.flush(&channel, SnapshotView(), [&db,&filter,&prp,&agg](CaliperMetadataAccessInterface& in_db, const std::vector& rec){ + c.flush( + &channel, + SnapshotView(), + [&db, &filter, &prp, &agg](CaliperMetadataAccessInterface& in_db, const std::vector& rec) { EntryList mrec = prp.process(db, db.merge_snapshot(in_db, rec)); if (filter.pass(db, mrec)) agg.add(db, mrec); - }); + } + ); // write intermediate results into output aggregator agg.flush(db, output_agg); @@ -67,8 +69,7 @@ local_aggregate(const char* query, Caliper& c, Channel& channel, CaliperMetadata // Timeseries processing // -void -convert_timeseries_option(const ConfigManager::Options& opts, const char* ts_opt_name, std::string& target) +void convert_timeseries_option(const ConfigManager::Options& opts, const char* ts_opt_name, std::string& target) { std::string spot_opt_name = "timeseries."; spot_opt_name.append(ts_opt_name); @@ -85,8 +86,7 @@ convert_timeseries_option(const ConfigManager::Options& opts, const char* ts_opt // Make a config string for the timeseries ConfigManager from the // Spot controller's options -std::string -get_timeseries_config_string(const ConfigManager::Options& opts) +std::string get_timeseries_config_string(const ConfigManager::Options& opts) { std::string ret = "spot.timeseries("; std::string tsopts; @@ -106,8 +106,8 @@ get_timeseries_config_string(const ConfigManager::Options& opts) struct LoopInfo { std::string name; - int iterations; - int count; + int iterations; + int count; }; LoopInfo get_loop_info(CaliperMetadataAccessInterface& db, const EntryList& rec) @@ -119,10 +119,10 @@ LoopInfo get_loop_info(CaliperMetadataAccessInterface& db, const EntryList& rec) Attribute lcnt_a = db.get_attribute("max#count"); for (const Entry& e : rec) { - if (e.attribute() == iter_a.id()) + if (e.attribute() == iter_a.id()) ret.iterations = e.value().to_int(); else if (e.attribute() == lcnt_a.id()) - ret.count = e.value().to_int(); + ret.count = e.value().to_int(); else { Variant v_loop = e.value(loop_a); if (!v_loop.empty()) @@ -133,9 +133,8 @@ LoopInfo get_loop_info(CaliperMetadataAccessInterface& db, const EntryList& rec) return ret; } -template -std::vector -augment_vector(const std::vector& orig, std::initializer_list ilist) +template +std::vector augment_vector(const std::vector& orig, std::initializer_list ilist) { std::vector ret; ret.reserve(orig.size() + ilist.size()); @@ -150,7 +149,14 @@ class SpotTimeseriesController : public cali::ChannelController public: - void timeseries_local_aggregation(Caliper& c, CaliperMetadataDB& db, const std::string& loopname, int blocksize, Aggregator& output_agg) { + void timeseries_local_aggregation( + Caliper& c, + CaliperMetadataDB& db, + const std::string& loopname, + int blocksize, + Aggregator& output_agg + ) + { const char* select = " cali.channel" ",loop" @@ -159,22 +165,23 @@ class SpotTimeseriesController : public cali::ChannelController ",sum(loop.iterations)" ",ratio(loop.iterations,time.duration.ns,1e9)"; - std::string block = - std::string("block = truncate(loop.start_iteration,") + std::to_string(blocksize) + ")"; + std::string block = std::string("block = truncate(loop.start_iteration,") + std::to_string(blocksize) + ")"; - std::string query = m_opts.build_query("local", { - { "let", block }, - { "select", select }, - { "group by", "cali.channel,loop,block" }, - { "where", std::string("loop.start_iteration,loop=\"") + loopname + "\"" } - }); + std::string query = m_opts.build_query( + "local", + { { "let", block }, + { "select", select }, + { "group by", "cali.channel,loop,block" }, + { "where", std::string("loop.start_iteration,loop=\"") + loopname + "\"" } } + ); Channel chn = channel(); if (chn) local_aggregate(query.c_str(), c, chn, db, output_agg); } - QuerySpec timeseries_spec() { + QuerySpec timeseries_spec() + { const char* select = " cali.channel" ",loop" @@ -183,32 +190,33 @@ class SpotTimeseriesController : public cali::ChannelController ",max(scale#time.duration.ns) as \"Time (s)\" unit sec" ",avg(ratio#loop.iterations/time.duration.ns) as \"Iter/s\" unit iter/s"; - std::string query = m_opts.build_query("cross", { - { "select", select }, - { "group by", "cali.channel,loop,block" } - }); + std::string query = + m_opts.build_query("cross", { { "select", select }, { "group by", "cali.channel,loop,block" } }); return parse_spec(query.c_str()); } - void flush() { } + void flush() {} - SpotTimeseriesController(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) - : ChannelController(name, 0, initial_cfg), - m_opts(opts) - { - if (m_opts.is_set("iteration_interval")) - config()["CALI_LOOP_MONITOR_ITERATION_INTERVAL"] = m_opts.get("iteration_interval").to_string(); - else if (m_opts.is_set("time_interval")) - config()["CALI_LOOP_MONITOR_TIME_INTERVAL"] = m_opts.get("time_interval").to_string(); - else - config()["CALI_LOOP_MONITOR_TIME_INTERVAL"] = "0.5"; + SpotTimeseriesController( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts + ) + : ChannelController(name, 0, initial_cfg), m_opts(opts) + { + if (m_opts.is_set("iteration_interval")) + config()["CALI_LOOP_MONITOR_ITERATION_INTERVAL"] = m_opts.get("iteration_interval").to_string(); + else if (m_opts.is_set("time_interval")) + config()["CALI_LOOP_MONITOR_TIME_INTERVAL"] = m_opts.get("time_interval").to_string(); + else + config()["CALI_LOOP_MONITOR_TIME_INTERVAL"] = "0.5"; - if (m_opts.is_set("target_loops")) - config()["CALI_LOOP_MONITOR_TARGET_LOOPS" ] = m_opts.get("target_loops").to_string(); + if (m_opts.is_set("target_loops")) + config()["CALI_LOOP_MONITOR_TARGET_LOOPS"] = m_opts.get("target_loops").to_string(); - m_opts.update_channel_config(config()); - } + m_opts.update_channel_config(config()); + } }; const char* spot_timeseries_spec = @@ -242,8 +250,11 @@ const char* spot_timeseries_spec = " ]" "}"; -cali::ChannelController* -make_timeseries_controller(const char* name, const config_map_t& cfg, const cali::ConfigManager::Options& opts) +cali::ChannelController* make_timeseries_controller( + const char* name, + const config_map_t& cfg, + const cali::ConfigManager::Options& opts +) { return new SpotTimeseriesController(name, cfg, opts); } @@ -260,18 +271,25 @@ class SpotController : public cali::internal::CustomOutputController { ConfigManager::Options m_opts; - std::string m_spot_metrics; - std::string m_spot_timeseries_metrics; + std::string m_spot_metrics; + std::string m_spot_timeseries_metrics; - ConfigManager m_timeseries_mgr; + ConfigManager m_timeseries_mgr; CaliperMetadataDB m_db; Attribute m_channel_attr; - void process_timeseries(SpotTimeseriesController* tsc, Caliper& c, CaliWriter& writer, const LoopInfo& info, Comm& comm) { - int iterations = comm.bcast_int(info.iterations); - int rec_count = comm.bcast_int(info.count); - std::string name = comm.bcast_str(info.name); + void process_timeseries( + SpotTimeseriesController* tsc, + Caliper& c, + CaliWriter& writer, + const LoopInfo& info, + Comm& comm + ) + { + int iterations = comm.bcast_int(info.iterations); + int rec_count = comm.bcast_int(info.count); + std::string name = comm.bcast_str(info.name); if (iterations > 0) { int nblocks = 20; @@ -297,7 +315,7 @@ class SpotController : public cali::internal::CustomOutputController // loop, so just clear them before setting them. m_spot_timeseries_metrics.clear(); - for (const auto &op : spec.aggregate.list) { + for (const auto& op : spec.aggregate.list) { if (!m_spot_timeseries_metrics.empty()) m_spot_timeseries_metrics.append(","); @@ -308,14 +326,18 @@ class SpotController : public cali::internal::CustomOutputController Entry entry(m_db.make_tree_entry(1, &m_channel_attr, &v_data)); // --- Write data - cross_agg.flush(m_db, [&writer,entry](CaliperMetadataAccessInterface& in_db, const std::vector& rec){ - writer.write_snapshot(in_db, augment_vector( rec, { entry } )); - }); + cross_agg.flush( + m_db, + [&writer, entry](CaliperMetadataAccessInterface& in_db, const std::vector& rec) { + writer.write_snapshot(in_db, augment_vector(rec, { entry })); + } + ); } } } - void flush_timeseries(Caliper& c, CaliWriter& writer, Comm& comm) { + void flush_timeseries(Caliper& c, CaliWriter& writer, Comm& comm) + { auto p = m_timeseries_mgr.get_channel("spot.timeseries"); if (!p) { @@ -325,10 +347,8 @@ class SpotController : public cali::internal::CustomOutputController auto tsc = std::dynamic_pointer_cast(p); - const char* summary_local_query = - "aggregate count(),sum(loop.iterations) where loop group by loop"; - const char* summary_cross_query = - "aggregate max(sum#loop.iterations),max(count) group by loop"; + const char* summary_local_query = "aggregate count(),sum(loop.iterations) where loop group by loop"; + const char* summary_cross_query = "aggregate max(sum#loop.iterations),max(count) group by loop"; Aggregator summary_cross_agg(CalQLParser(summary_cross_query).spec()); @@ -338,9 +358,9 @@ class SpotController : public cali::internal::CustomOutputController std::vector infovec; - summary_cross_agg.flush(m_db, [&infovec](CaliperMetadataAccessInterface& in_db, const EntryList& rec){ - infovec.push_back(get_loop_info(in_db, rec)); - }); + summary_cross_agg.flush(m_db, [&infovec](CaliperMetadataAccessInterface& in_db, const EntryList& rec) { + infovec.push_back(get_loop_info(in_db, rec)); + }); if (!infovec.empty()) { for (const LoopInfo& loopinfo : infovec) @@ -351,7 +371,8 @@ class SpotController : public cali::internal::CustomOutputController } } - void flush_regionprofile(Caliper& c, CaliWriter& writer, Comm& comm) { + void flush_regionprofile(Caliper& c, CaliWriter& writer, Comm& comm) + { // --- Setup output reduction aggregator (final cross-process aggregation) const char* cross_select = " *" @@ -360,10 +381,7 @@ class SpotController : public cali::internal::CustomOutputController ",avg(inclusive#sum#time.duration) as \"Avg time/rank\" unit sec" ",sum(inclusive#sum#time.duration) as \"Total time\" unit sec"; - std::string cross_query = m_opts.build_query("cross", { - { "select", cross_select }, - { "group by", "path" } - }); + std::string cross_query = m_opts.build_query("cross", { { "select", cross_select }, { "group by", "path" } }); QuerySpec output_spec(parse_spec(cross_query.c_str())); Aggregator output_agg(output_spec); @@ -374,11 +392,14 @@ class SpotController : public cali::internal::CustomOutputController // --- Flush Caliper buffers into intermediate aggregator to calculate // region profile inclusive times { - std::string query = m_opts.build_query("local", { - { "let", "sum#time.duration=scale(sum#time.duration.ns,1e-9)" }, - { "select", "inclusive_sum(sum#time.duration)" }, + std::string query = m_opts.build_query( + "local", + { + { "let", "sum#time.duration=scale(sum#time.duration.ns,1e-9)" }, + { "select", "inclusive_sum(sum#time.duration)" }, { "group by", "path" }, - }); + } + ); Channel chn = channel(); local_aggregate(query.c_str(), c, chn, m_db, output_agg); @@ -391,7 +412,7 @@ class SpotController : public cali::internal::CustomOutputController // --- Save the spot metrics m_spot_metrics.clear(); - for (const auto &op : output_spec.aggregate.list) { + for (const auto& op : output_spec.aggregate.list) { if (!m_spot_metrics.empty()) m_spot_metrics.append(","); @@ -402,17 +423,21 @@ class SpotController : public cali::internal::CustomOutputController Entry entry(m_db.make_tree_entry(1, &m_channel_attr, &v_data)); // --- Write region profile - output_agg.flush(m_db, [&writer,entry](CaliperMetadataAccessInterface& in_db, const std::vector& rec){ - writer.write_snapshot(in_db, augment_vector( rec, { entry } )); - }); + output_agg.flush( + m_db, + [&writer, entry](CaliperMetadataAccessInterface& in_db, const std::vector& rec) { + writer.write_snapshot(in_db, augment_vector(rec, { entry })); + } + ); } } - void save_spot_metadata() { + void save_spot_metadata() + { std::string spot_channels = "regionprofile"; - std::string spot_opts = ""; + std::string spot_opts = ""; - for (const auto &o : m_opts.enabled_options()) { + for (const auto& o : m_opts.enabled_options()) { if (!spot_opts.empty()) spot_opts.append(","); spot_opts.append(o); @@ -420,16 +445,11 @@ class SpotController : public cali::internal::CustomOutputController spot_channels.append(",timeseries"); } - Attribute mtr_attr = - m_db.create_attribute("spot.metrics", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); - Attribute tsm_attr = - m_db.create_attribute("spot.timeseries.metrics", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); - Attribute fmt_attr = - m_db.create_attribute("spot.format.version", CALI_TYPE_INT, CALI_ATTR_GLOBAL); - Attribute opt_attr = - m_db.create_attribute("spot.options", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); - Attribute chn_attr = - m_db.create_attribute("spot.channels", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute mtr_attr = m_db.create_attribute("spot.metrics", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute tsm_attr = m_db.create_attribute("spot.timeseries.metrics", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute fmt_attr = m_db.create_attribute("spot.format.version", CALI_TYPE_INT, CALI_ATTR_GLOBAL); + Attribute opt_attr = m_db.create_attribute("spot.options", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute chn_attr = m_db.create_attribute("spot.channels", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); m_db.set_global(mtr_attr, Variant(m_spot_metrics.c_str())); m_db.set_global(tsm_attr, Variant(m_spot_timeseries_metrics.c_str())); @@ -438,17 +458,17 @@ class SpotController : public cali::internal::CustomOutputController m_db.set_global(chn_attr, Variant(spot_channels.c_str())); } - void on_create(Caliper*, Channel&) override { + void on_create(Caliper*, Channel&) override + { if (m_timeseries_mgr.error()) - Log(0).stream() << "[spot controller]: Timeseries config error: " - << m_timeseries_mgr.error_msg() + Log(0).stream() << "[spot controller]: Timeseries config error: " << m_timeseries_mgr.error_msg() << std::endl; m_timeseries_mgr.start(); } - OutputStream - create_output_stream() { + OutputStream create_output_stream() + { std::string outdir = m_opts.get("outdir", "").to_string(); std::string output = m_opts.get("output", "").to_string(); @@ -457,7 +477,7 @@ class SpotController : public cali::internal::CustomOutputController if (!outdir.empty() && output != "stderr" && output != "stdout") output = outdir + std::string("/") + output; - Caliper c; + Caliper c; OutputStream stream; stream.set_filename(output.c_str(), c, c.get_globals()); @@ -466,15 +486,14 @@ class SpotController : public cali::internal::CustomOutputController public: - void - collective_flush(OutputStream& stream, Comm& comm) override + void collective_flush(OutputStream& stream, Comm& comm) override { Log(1).stream() << name() << ": Flushing Caliper data" << std::endl; if (stream.type() == OutputStream::None) stream = create_output_stream(); - Caliper c; + Caliper c; CaliWriter writer(stream); flush_regionprofile(c, writer, comm); @@ -487,24 +506,19 @@ class SpotController : public cali::internal::CustomOutputController save_spot_metadata(); writer.write_globals(m_db, m_db.get_globals()); - Log(1).stream() << name() << ": Wrote " - << writer.num_written() << " records." - << std::endl; + Log(1).stream() << name() << ": Wrote " << writer.num_written() << " records." << std::endl; } } SpotController(const char* name, const config_map_t& initial_cfg, const ConfigManager::Options& opts) - : cali::internal::CustomOutputController(name, 0, initial_cfg), - m_opts(opts) + : cali::internal::CustomOutputController(name, 0, initial_cfg), m_opts(opts) { - m_channel_attr = - m_db.create_attribute("spot.channel", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + m_channel_attr = m_db.create_attribute("spot.channel", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - #ifdef CALIPER_HAVE_ADIAK +#ifdef CALIPER_HAVE_ADIAK config()["CALI_SERVICES_ENABLE"].append(",adiak_import"); - config()["CALI_ADIAK_IMPORT_CATEGORIES"] = - opts.get("adiak.import_categories", "2,3").to_string(); - #endif + config()["CALI_ADIAK_IMPORT_CATEGORIES"] = opts.get("adiak.import_categories", "2,3").to_string(); +#endif if (opts.is_enabled("timeseries")) { m_timeseries_mgr.add_config_spec(spot_timeseries_info); @@ -516,8 +530,8 @@ class SpotController : public cali::internal::CustomOutputController } }; -std::string -check_spot_timeseries_args(const cali::ConfigManager::Options& opts) { +std::string check_spot_timeseries_args(const cali::ConfigManager::Options& opts) +{ if (opts.is_enabled("timeseries")) { // Check if the timeseries options are valid @@ -528,13 +542,11 @@ check_spot_timeseries_args(const cali::ConfigManager::Options& opts) { } else { // Warn when a timeseries option is set but timeseries is disabled - const char* tsopts[] = { - "timeseries.maxrows", - "timeseries.iteration_interval", - "timeseries.time_interval", - "timeseries.target_loops", - "timeseries.metrics" - }; + const char* tsopts[] = { "timeseries.maxrows", + "timeseries.iteration_interval", + "timeseries.time_interval", + "timeseries.target_loops", + "timeseries.metrics" }; for (const char* opt : tsopts) if (opts.is_set(opt)) @@ -643,20 +655,22 @@ const char* spot_controller_spec = R"json( } )json"; - -cali::ChannelController* -make_spot_controller(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) { +cali::ChannelController* make_spot_controller( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts +) +{ return new SpotController(name, initial_cfg, opts); } -} // namespace [anonymous] +} // namespace namespace cali { -ConfigManager::ConfigInfo spot_controller_info -{ - ::spot_controller_spec, ::make_spot_controller, ::check_spot_timeseries_args -}; +ConfigManager::ConfigInfo spot_controller_info { ::spot_controller_spec, + ::make_spot_controller, + ::check_spot_timeseries_args }; } diff --git a/src/caliper/controllers/controllers.cpp b/src/caliper/controllers/controllers.cpp index d58796bb7..09fd8aad9 100644 --- a/src/caliper/controllers/controllers.cpp +++ b/src/caliper/controllers/controllers.cpp @@ -181,12 +181,12 @@ const char* mpireport_spec = R"json( )json"; cali::ConfigManager::ConfigInfo event_trace_controller_info { event_trace_spec, nullptr, nullptr }; -cali::ConfigManager::ConfigInfo nvprof_controller_info { nvprof_spec, nullptr, nullptr }; -cali::ConfigManager::ConfigInfo nvtx_controller_info { nvtx_spec, nullptr, nullptr }; -cali::ConfigManager::ConfigInfo roctx_controller_info { roctx_spec, nullptr, nullptr }; -cali::ConfigManager::ConfigInfo mpireport_controller_info { mpireport_spec, nullptr, nullptr }; +cali::ConfigManager::ConfigInfo nvprof_controller_info { nvprof_spec, nullptr, nullptr }; +cali::ConfigManager::ConfigInfo nvtx_controller_info { nvtx_spec, nullptr, nullptr }; +cali::ConfigManager::ConfigInfo roctx_controller_info { roctx_spec, nullptr, nullptr }; +cali::ConfigManager::ConfigInfo mpireport_controller_info { mpireport_spec, nullptr, nullptr }; -} +} // namespace namespace cali { @@ -203,25 +203,23 @@ extern ConfigManager::ConfigInfo runtime_report_controller_info; extern ConfigManager::ConfigInfo sample_report_controller_info; extern ConfigManager::ConfigInfo spot_controller_info; -const ConfigManager::ConfigInfo* builtin_controllers_table[] = { - &cuda_activity_profile_controller_info, - &cuda_activity_report_controller_info, - &::event_trace_controller_info, - &::nvprof_controller_info, - &::nvtx_controller_info, - &::roctx_controller_info, - &::mpireport_controller_info, - &hatchet_region_profile_controller_info, - &hatchet_sample_profile_controller_info, - &loop_report_controller_info, - &openmp_report_controller_info, - &rocm_activity_report_controller_info, - &rocm_activity_profile_controller_info, - &runtime_report_controller_info, - &sample_report_controller_info, - &spot_controller_info, - nullptr -}; +const ConfigManager::ConfigInfo* builtin_controllers_table[] = { &cuda_activity_profile_controller_info, + &cuda_activity_report_controller_info, + &::event_trace_controller_info, + &::nvprof_controller_info, + &::nvtx_controller_info, + &::roctx_controller_info, + &::mpireport_controller_info, + &hatchet_region_profile_controller_info, + &hatchet_sample_profile_controller_info, + &loop_report_controller_info, + &openmp_report_controller_info, + &rocm_activity_report_controller_info, + &rocm_activity_profile_controller_info, + &runtime_report_controller_info, + &sample_report_controller_info, + &spot_controller_info, + nullptr }; const char* builtin_option_specs = R"json( [ @@ -1258,4 +1256,4 @@ const char* builtin_option_specs = R"json( ] )json"; -} +} // namespace cali diff --git a/src/caliper/controllers/util.h b/src/caliper/controllers/util.h index c335ae01e..aaabd8f1a 100644 --- a/src/caliper/controllers/util.h +++ b/src/caliper/controllers/util.h @@ -19,6 +19,6 @@ std::string build_tree_format_spec(config_map_t&, const cali::ConfigManager::Opt } -} +} // namespace cali #endif // CALI_CONTROLLERS_UTIL_H diff --git a/src/caliper/machine.h b/src/caliper/machine.h index 8635a28e9..d4080eca2 100644 --- a/src/caliper/machine.h +++ b/src/caliper/machine.h @@ -16,9 +16,7 @@ namespace machine { /// \brief Describe the hardware level -enum MachineLevel { - None, Process, Socket, Node -}; +enum MachineLevel { None, Process, Socket, Node }; /// \brief Determine rank of the calling process/thread on the given \a level int get_rank_for(MachineLevel level); diff --git a/src/caliper/machine_mpi.cpp b/src/caliper/machine_mpi.cpp index 1a22ab8a1..bf13e53d5 100644 --- a/src/caliper/machine_mpi.cpp +++ b/src/caliper/machine_mpi.cpp @@ -28,8 +28,7 @@ int get_rank_for_hash(uint64_t hash, MPI_Comm comm) std::vector res(ccsize, 0); - int ret = PMPI_Allgather(&hash, 1, MPI_UINT64_T, - res.data(), 1, MPI_UINT64_T, comm); + int ret = PMPI_Allgather(&hash, 1, MPI_UINT64_T, res.data(), 1, MPI_UINT64_T, comm); if (ret != MPI_SUCCESS) return -1; @@ -46,7 +45,7 @@ int get_rank_for_hash(uint64_t hash, MPI_Comm comm) int get_rank_for_node() { constexpr std::size_t max_len = 1024; - char hostname[max_len]; + char hostname[max_len]; if (gethostname(hostname, max_len) < 0) return -1; @@ -55,10 +54,10 @@ int get_rank_for_node() if (!initialized) return 0; - return ::get_rank_for_hash(std::hash{}(std::string(hostname)), MPI_COMM_WORLD); + return ::get_rank_for_hash(std::hash {}(std::string(hostname)), MPI_COMM_WORLD); } -} // namespace [anonymous] +} // namespace namespace cali { @@ -74,9 +73,7 @@ int get_rank_for(MachineLevel level) case MachineLevel::Node: return ::get_rank_for_node(); default: - Log(0).stream() << "machine::get_rank_for(MachineLevel): level " - << level << " is not supported" - << std::endl; + Log(0).stream() << "machine::get_rank_for(MachineLevel): level " << level << " is not supported" << std::endl; } return -1; @@ -84,4 +81,4 @@ int get_rank_for(MachineLevel level) } // namespace machine -} +} // namespace cali diff --git a/src/caliper/machine_serial.cpp b/src/caliper/machine_serial.cpp index a9c016f6f..6c4558d1f 100644 --- a/src/caliper/machine_serial.cpp +++ b/src/caliper/machine_serial.cpp @@ -21,14 +21,12 @@ int get_rank_for(MachineLevel level) case MachineLevel::Node: return 0; default: - Log(0).stream() << "machine::get_rank_for(MachineLevel): level " - << level << " is not supported" - << std::endl; + Log(0).stream() << "machine::get_rank_for(MachineLevel): level " << level << " is not supported" << std::endl; } return -1; } -} +} // namespace machine -} +} // namespace cali diff --git a/src/caliper/mpi_flush.cpp b/src/caliper/mpi_flush.cpp index 6850a4ea2..35de7c418 100644 --- a/src/caliper/mpi_flush.cpp +++ b/src/caliper/mpi_flush.cpp @@ -21,16 +21,14 @@ namespace void mpiflush_init(Caliper* c, Channel* channel) { - mpiwrap_get_events(channel)->mpi_finalize_evt.connect( - [](Caliper* c, Channel* channel){ - c->flush_and_write(channel, SnapshotView()); - }); + mpiwrap_get_events(channel)->mpi_finalize_evt.connect([](Caliper* c, Channel* channel) { + c->flush_and_write(channel, SnapshotView()); + }); Log(1).stream() << channel->name() << ": Registered mpiflush service" << std::endl; } -} - +} // namespace namespace cali { diff --git a/src/caliper/setup_mpi.cpp b/src/caliper/setup_mpi.cpp index d92875dcc..17ffb628e 100644 --- a/src/caliper/setup_mpi.cpp +++ b/src/caliper/setup_mpi.cpp @@ -24,8 +24,7 @@ using COC = cali::internal::CustomOutputController; namespace { -void -setup_log_prefix() +void setup_log_prefix() { static bool done = false; @@ -54,8 +53,7 @@ setup_log_prefix() } // Implement flush over MPI for CustomOutputController objects -void -custom_output_controller_flush_mpi(COC* controller) +void custom_output_controller_flush_mpi(COC* controller) { Log(2).stream() << controller->name() << ": CustomOutputController::flush(): using MPI" << std::endl; @@ -65,7 +63,7 @@ custom_output_controller_flush_mpi(COC* controller) controller->collective_flush(stream, comm); } -} // namespace [anonymous] +} // namespace namespace cali { @@ -74,9 +72,7 @@ extern CaliperService mpiflush_service; void add_submodule_controllers_and_services() { - static const CaliperService mpi_services[] = { - mpiflush_service, { nullptr, nullptr } - }; + static const CaliperService mpi_services[] = { mpiflush_service, { nullptr, nullptr } }; services::add_service_specs(mpi_services); services::add_default_service_specs(); @@ -95,10 +91,8 @@ void init_submodules() extern "C" { -void -cali_mpi_init() +void cali_mpi_init() { ::setup_log_prefix(); } - } diff --git a/src/caliper/test/MockupMetadataDB.h b/src/caliper/test/MockupMetadataDB.h index 677c63732..2ebac0862 100644 --- a/src/caliper/test/MockupMetadataDB.h +++ b/src/caliper/test/MockupMetadataDB.h @@ -1,4 +1,4 @@ -// Copyright (c) 2016, Lawrence Livermore National Security, LLC. +// Copyright (c) 2016, Lawrence Livermore National Security, LLC. // Produced at the Lawrence Livermore National Laboratory. // // This file is part of Caliper. @@ -30,7 +30,7 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -/// \file MockupMetadataDB.h +/// \file MockupMetadataDB.h /// Mockup Caliper metadata access interface for testing #pragma once @@ -48,60 +48,53 @@ namespace cali /// \brief A simple mockup implementation for CaliperMetadataAccessInterface class MockupMetadataDB : public CaliperMetadataAccessInterface { - std::map m_node_map; - std::map m_attr_map; + std::map m_node_map; + std::map m_attr_map; std::map m_attr_names; // gtest FAIL() can only be called in void functions - void fail(const char* str) { - FAIL() << str; - } - + void fail(const char* str) { FAIL() << str; } + public: - MockupMetadataDB() - { } + MockupMetadataDB() {} - ~MockupMetadataDB() - { } + ~MockupMetadataDB() {} - inline void - add_node(Node* node) { - m_node_map.insert(std::make_pair(node->id(), node)); - } + inline void add_node(Node* node) { m_node_map.insert(std::make_pair(node->id(), node)); } - inline void - add_nodes(size_t n, Node* nodes[]) { + inline void add_nodes(size_t n, Node* nodes[]) + { for (size_t i = 0; i < n; ++i) add_node(nodes[i]); } - - inline void - add_attribute(const Attribute& attr) { + + inline void add_attribute(const Attribute& attr) + { m_attr_map.insert(std::make_pair(attr.id(), attr)); m_attr_names.insert(std::make_pair(attr.name(), attr)); } - - inline Node* - node(cali_id_t id) const { + + inline Node* node(cali_id_t id) const + { auto it = m_node_map.find(id); return it == m_node_map.end() ? nullptr : it->second; } - inline Attribute - get_attribute(cali_id_t id) const { + inline Attribute get_attribute(cali_id_t id) const + { auto it = m_attr_map.find(id); return it == m_attr_map.end() ? Attribute() : it->second; } - inline Attribute - get_attribute(const std::string& name) const { + inline Attribute get_attribute(const std::string& name) const + { auto it = m_attr_names.find(name); return it == m_attr_names.end() ? Attribute() : it->second; } - inline std::vector - get_all_attributes() const { + inline std::vector get_all_attributes() const + { std::vector vec; for (auto p : m_attr_map) @@ -110,23 +103,24 @@ class MockupMetadataDB : public CaliperMetadataAccessInterface return vec; } - Attribute - create_attribute(const std::string& name, - cali_attr_type type, - int prop = CALI_ATTR_DEFAULT, - int meta = 0, - const Attribute* meta_attr = nullptr, - const Variant* meta_data = nullptr) { + Attribute create_attribute( + const std::string& name, + cali_attr_type type, + int prop = CALI_ATTR_DEFAULT, + int meta = 0, + const Attribute* meta_attr = nullptr, + const Variant* meta_data = nullptr + ) + { fail("create_attribute() is not implemented in MockupMetadataDB!"); return Attribute(); } - Node* - make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent) { + Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent) + { fail("make_tree_entry() is not implemented in MockupMetadataDB!"); return nullptr; } - }; -} // namespace +} // namespace cali diff --git a/src/caliper/test/test_attribute.cpp b/src/caliper/test/test_attribute.cpp index 62cee71c4..49d539a78 100644 --- a/src/caliper/test/test_attribute.cpp +++ b/src/caliper/test/test_attribute.cpp @@ -16,11 +16,11 @@ using namespace cali; -TEST(AttributeAPITest, ValidAttribute) { +TEST(AttributeAPITest, ValidAttribute) +{ Caliper c; - Attribute meta_attr = - c.create_attribute("test.attribute.api.meta", CALI_TYPE_INT, CALI_ATTR_HIDDEN); + Attribute meta_attr = c.create_attribute("test.attribute.api.meta", CALI_TYPE_INT, CALI_ATTR_HIDDEN); ASSERT_TRUE(meta_attr); @@ -32,10 +32,14 @@ TEST(AttributeAPITest, ValidAttribute) { cali_id_t meta_id = meta_attr.id(); cali_variant_t meta_val = cali_make_variant_from_int(42); - cali_id_t attr_id = - cali_create_attribute_with_metadata("test.attribute.api", CALI_TYPE_STRING, - CALI_ATTR_NESTED | CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_NOMERGE, - 1, &meta_id, &meta_val); + cali_id_t attr_id = cali_create_attribute_with_metadata( + "test.attribute.api", + CALI_TYPE_STRING, + CALI_ATTR_NESTED | CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_NOMERGE, + 1, + &meta_id, + &meta_val + ); ASSERT_NE(attr_id, CALI_INV_ID); @@ -55,7 +59,7 @@ TEST(AttributeAPITest, ValidAttribute) { ASSERT_GE(cali_prop2string(cali_attribute_properties(attr_id), buf, 120), 1); std::vector props; - util::split(std::string(buf), ':', std::back_inserter(props)); + util::split(std::string(buf), ':', std::back_inserter(props)); std::vector props_exp { "nested", "process_scope", "nomerge", "default", "level_0" }; @@ -68,19 +72,19 @@ TEST(AttributeAPITest, ValidAttribute) { EXPECT_TRUE(props_exp.empty()); } -TEST(AttributeAPITest, InvalidAttribute) { +TEST(AttributeAPITest, InvalidAttribute) +{ EXPECT_EQ(cali_attribute_type(CALI_INV_ID), CALI_TYPE_INV); EXPECT_EQ(cali_attribute_name(CALI_INV_ID), nullptr); EXPECT_EQ(cali_find_attribute("test.attribute.api.nope"), CALI_INV_ID); EXPECT_FALSE(Caliper::instance().attribute_exists("test.attribute.api.nope")); - } -TEST(AttributeAPITest, GlobalAttributes) { +TEST(AttributeAPITest, GlobalAttributes) +{ Caliper c; - Attribute global_attr = - c.create_attribute("test.attribute.global", CALI_TYPE_INT, CALI_ATTR_GLOBAL); + Attribute global_attr = c.create_attribute("test.attribute.global", CALI_TYPE_INT, CALI_ATTR_GLOBAL); ASSERT_TRUE(global_attr); @@ -94,23 +98,21 @@ TEST(AttributeAPITest, GlobalAttributes) { std::vector globals = c.get_globals(); - auto it = std::find_if(globals.begin(), globals.end(), - [global_attr](const Entry& e) { - return e.count(global_attr) > 0; - } ); + auto it = std::find_if(globals.begin(), globals.end(), [global_attr](const Entry& e) { + return e.count(global_attr) > 0; + }); ASSERT_NE(it, globals.end()); EXPECT_EQ(it->value(global_attr).to_int(), 42); } -TEST(AttributeAPITest, NestedAttribute) { +TEST(AttributeAPITest, NestedAttribute) +{ Caliper c; - Attribute nested_a = - c.create_attribute("test.attr.nested.a", CALI_TYPE_INT, CALI_ATTR_NESTED); - Attribute nested_b = - c.create_attribute("test.attr.nested.b", CALI_TYPE_INT, CALI_ATTR_NESTED); + Attribute nested_a = c.create_attribute("test.attr.nested.a", CALI_TYPE_INT, CALI_ATTR_NESTED); + Attribute nested_b = c.create_attribute("test.attr.nested.b", CALI_TYPE_INT, CALI_ATTR_NESTED); EXPECT_TRUE(nested_a.is_nested()); EXPECT_TRUE(nested_a.is_autocombineable()); diff --git a/src/caliper/test/test_blackboard.cpp b/src/caliper/test/test_blackboard.cpp index 3eab77fea..69c396b5c 100644 --- a/src/caliper/test/test_blackboard.cpp +++ b/src/caliper/test/test_blackboard.cpp @@ -7,17 +7,14 @@ using namespace cali; -TEST(BlackboardTest, BasicFunctionality) { +TEST(BlackboardTest, BasicFunctionality) +{ Caliper c; - Attribute attr_ref = - c.create_attribute("bb.gs.ref", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute attr_imm = - c.create_attribute("bb.gs.imm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute attr_uns = - c.create_attribute("bb.gs.uns", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute attr_hidden = - c.create_attribute("bb.gs.h", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN); + Attribute attr_ref = c.create_attribute("bb.gs.ref", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute attr_imm = c.create_attribute("bb.gs.imm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute attr_uns = c.create_attribute("bb.gs.uns", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute attr_hidden = c.create_attribute("bb.gs.h", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN); Node* node_p = c.make_tree_entry(attr_ref, Variant(42)); Node* node_c = c.make_tree_entry(attr_ref, Variant(24), node_p); @@ -36,7 +33,7 @@ TEST(BlackboardTest, BasicFunctionality) { bb.set(attr_ref.id(), Entry(node_p), true); EXPECT_EQ(bb.get(attr_ref.id()).node()->id(), node_p->id()); - bb.set(attr_imm.id(), Entry(attr_imm, Variant(1122)), true) ; + bb.set(attr_imm.id(), Entry(attr_imm, Variant(1122)), true); EXPECT_EQ(bb.get(attr_imm.id()).value().to_int(), 1122); bb.set(attr_hidden.id(), Entry(attr_hidden, Variant(2211)), false); EXPECT_EQ(bb.get(attr_hidden.id()).value().to_int(), 2211); @@ -80,11 +77,11 @@ TEST(BlackboardTest, BasicFunctionality) { bb.print_statistics(std::cout) << std::endl; } -TEST(BlackboardTest, Exchange) { +TEST(BlackboardTest, Exchange) +{ Caliper c; - Attribute attr_imm = - c.create_attribute("bb.ex.imm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute attr_imm = c.create_attribute("bb.ex.imm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); Blackboard bb; @@ -95,13 +92,14 @@ TEST(BlackboardTest, Exchange) { EXPECT_EQ(bb.num_skipped_entries(), 0); } -TEST(BlackboardTest, Overflow) { +TEST(BlackboardTest, Overflow) +{ Caliper c; Blackboard bb; for (int i = 0; i < 1100; ++i) { Attribute attr = - c.create_attribute(std::string("bb.ov.")+std::to_string(i), CALI_TYPE_INT, CALI_ATTR_ASVALUE); + c.create_attribute(std::string("bb.ov.") + std::to_string(i), CALI_TYPE_INT, CALI_ATTR_ASVALUE); bb.set(attr.id(), Entry(attr, Variant(i)), true); } @@ -109,15 +107,13 @@ TEST(BlackboardTest, Overflow) { EXPECT_GT(bb.num_skipped_entries(), 0); for (int i = 0; i < 1100; ++i) { - Attribute attr = - c.get_attribute(std::string("bb.ov.")+std::to_string(i)); + Attribute attr = c.get_attribute(std::string("bb.ov.") + std::to_string(i)); bb.del(attr.id()); } { - Attribute attr = - c.get_attribute("bb.ov.42"); + Attribute attr = c.get_attribute("bb.ov.42"); bb.set(attr.id(), Entry(attr, Variant(1142)), true); EXPECT_EQ(bb.get(attr.id()).value().to_int(), 1142); @@ -126,15 +122,13 @@ TEST(BlackboardTest, Overflow) { bb.print_statistics(std::cout) << std::endl; } -TEST(BlackboardTest, Snapshot) { +TEST(BlackboardTest, Snapshot) +{ Caliper c; - Attribute attr_ref = - c.create_attribute("bb.sn.ref", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute attr_imm = - c.create_attribute("bb.sn.imm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute attr_hidden = - c.create_attribute("bb.sn.h", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN); + Attribute attr_ref = c.create_attribute("bb.sn.ref", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute attr_imm = c.create_attribute("bb.sn.imm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute attr_hidden = c.create_attribute("bb.sn.h", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN); Node* node_p = c.make_tree_entry(attr_ref, Variant(42)); Node* node_c = c.make_tree_entry(attr_ref, Variant(24), node_p); @@ -153,14 +147,14 @@ TEST(BlackboardTest, Snapshot) { bb.set(attr_imm.id(), Entry(attr_imm, Variant(1122)), true); EXPECT_EQ(bb.get(attr_imm.id()).value().to_int(), 1122); - + bb.set(attr_hidden.id(), Entry(attr_hidden, Variant(2211)), false); EXPECT_EQ(bb.get(attr_hidden.id()).value().to_int(), 2211); // // --- snapshot // - + FixedSizeSnapshotRecord<8> rec; bb.snapshot(rec.builder()); diff --git a/src/caliper/test/test_c_api.cpp b/src/caliper/test/test_c_api.cpp index 4a6880451..33a993190 100644 --- a/src/caliper/test/test_c_api.cpp +++ b/src/caliper/test/test_c_api.cpp @@ -4,6 +4,7 @@ #include -TEST(C_API_Test, CaliperVersion) { +TEST(C_API_Test, CaliperVersion) +{ EXPECT_STREQ(cali_caliper_version(), CALIPER_VERSION); } diff --git a/src/caliper/test/test_c_snapshot.cpp b/src/caliper/test/test_c_snapshot.cpp index 3d44fcf4a..3c546249a 100644 --- a/src/caliper/test/test_c_snapshot.cpp +++ b/src/caliper/test/test_c_snapshot.cpp @@ -18,43 +18,42 @@ using namespace cali; namespace { - struct entry_data_t { - cali_id_t attr_id; - Variant val; - }; +struct entry_data_t { + cali_id_t attr_id; + Variant val; +}; - struct UnpackSnapshotTestData { - int max_visit_count; - int visit_count; - std::vector entries; +struct UnpackSnapshotTestData { + int max_visit_count; + int visit_count; + std::vector entries; - UnpackSnapshotTestData() - : max_visit_count(-1), - visit_count(0) - { } - }; + UnpackSnapshotTestData() : max_visit_count(-1), visit_count(0) {} +}; - int test_entry_proc_op(void* user_arg, cali_id_t attr_id, cali_variant_t val) { - UnpackSnapshotTestData* arg = static_cast(user_arg); +int test_entry_proc_op(void* user_arg, cali_id_t attr_id, cali_variant_t val) +{ + UnpackSnapshotTestData* arg = static_cast(user_arg); - if (arg->max_visit_count >= 0 && arg->visit_count >= arg->max_visit_count) - return 0; // quit + if (arg->max_visit_count >= 0 && arg->visit_count >= arg->max_visit_count) + return 0; // quit - arg->entries.push_back(entry_data_t({ attr_id, val })); - ++arg->visit_count; + arg->entries.push_back(entry_data_t({ attr_id, val })); + ++arg->visit_count; - return 1; - } + return 1; } +} // namespace -TEST(C_Snapshot_Test, UnpackEmpty) { +TEST(C_Snapshot_Test, UnpackEmpty) +{ CompressedSnapshotRecord rec; EXPECT_EQ(rec.num_nodes(), 0); EXPECT_EQ(rec.num_immediates(), 0); UnpackSnapshotTestData t1; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_unpack_snapshot(rec.data(), &bytes_read, ::test_entry_proc_op, &t1); @@ -62,17 +61,15 @@ TEST(C_Snapshot_Test, UnpackEmpty) { EXPECT_EQ(t1.entries.size(), 0); } -TEST(C_Snapshot_Test, Unpack) { +TEST(C_Snapshot_Test, Unpack) +{ // Mixed node/immediate record unpack test. Modifies a Caliper instance. Caliper c; - Attribute node_str_attr = - c.create_attribute("unpack.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute node_int_attr = - c.create_attribute("unpack.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_int_attr = - c.create_attribute("unpac.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute node_str_attr = c.create_attribute("unpack.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute node_int_attr = c.create_attribute("unpack.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_int_attr = c.create_attribute("unpac.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); Variant node_str_1(CALI_TYPE_STRING, "My wonderful unpack test string", 32); Variant node_str_2(CALI_TYPE_STRING, "My other unpack test string", 27); @@ -85,22 +82,8 @@ TEST(C_Snapshot_Test, Unpack) { FixedSizeSnapshotRecord<20> snapshot; - Attribute attr_in[] = { - node_str_attr, - node_int_attr, - val_int_attr, - node_str_attr, - val_int_attr, - node_int_attr - }; - Variant data_in[] = { - node_str_1, - node_int_1, - val_int_1, - node_str_2, - val_int_2, - node_int_2 - }; + Attribute attr_in[] = { node_str_attr, node_int_attr, val_int_attr, node_str_attr, val_int_attr, node_int_attr }; + Variant data_in[] = { node_str_1, node_int_1, val_int_1, node_str_2, val_int_2, node_int_2 }; c.make_record(6, attr_in, data_in, snapshot.builder()); @@ -114,7 +97,7 @@ TEST(C_Snapshot_Test, Unpack) { // do a full unpack UnpackSnapshotTestData t1; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_unpack_snapshot(rec.data(), &bytes_read, ::test_entry_proc_op, &t1); @@ -124,14 +107,12 @@ TEST(C_Snapshot_Test, Unpack) { EXPECT_EQ(t1.entries.size(), 6); for (int i = 0; i < 6; ++i) { - auto it = std::find_if(t1.entries.begin(), t1.entries.end(), - [i,attr_in,data_in](const entry_data_t& e) { - return (attr_in[i].id() == e.attr_id && data_in[i] == e.val); - }); - - EXPECT_NE(it, t1.entries.end()) << " entry (" - << attr_in[i] << "," << data_in[i] - << ") not found!" << std::endl; + auto it = std::find_if(t1.entries.begin(), t1.entries.end(), [i, attr_in, data_in](const entry_data_t& e) { + return (attr_in[i].id() == e.attr_id && data_in[i] == e.val); + }); + + EXPECT_NE(it, t1.entries.end()) + << " entry (" << attr_in[i] << "," << data_in[i] << ") not found!" << std::endl; } } @@ -139,7 +120,7 @@ TEST(C_Snapshot_Test, Unpack) { // do a partial unpack (quit after 2 entries) UnpackSnapshotTestData t2; - size_t bytes_read = 0; + size_t bytes_read = 0; t2.max_visit_count = 2; @@ -154,7 +135,7 @@ TEST(C_Snapshot_Test, Unpack) { // just check values int p = 0; - for ( ; p < 6 && data_in[p] != e.val; ++p) + for (; p < 6 && data_in[p] != e.val; ++p) ; EXPECT_LT(p, 6) << " entry (" << e.attr_id << "," << e.val << ") not found!"; @@ -162,19 +143,16 @@ TEST(C_Snapshot_Test, Unpack) { } } -TEST(C_Snapshot_Test, PullSnapshot) { +TEST(C_Snapshot_Test, PullSnapshot) +{ // Pull a snapshot with the C interface. Modifies the Caliper instance. Caliper c; - Attribute node_str_attr = - c.create_attribute("pull.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute node_int_attr = - c.create_attribute("pull.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_int_attr = - c.create_attribute("pull.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute val_dbl_attr = - c.create_attribute("pull.val.dbl", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); + Attribute node_str_attr = c.create_attribute("pull.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute node_int_attr = c.create_attribute("pull.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_int_attr = c.create_attribute("pull.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute val_dbl_attr = c.create_attribute("pull.val.dbl", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); Variant node_str_1(CALI_TYPE_STRING, "My wonderful pull test string", 30); Variant node_str_2(CALI_TYPE_STRING, "My other pull test string", 25); @@ -185,29 +163,13 @@ TEST(C_Snapshot_Test, PullSnapshot) { Variant val_int_1(2020); Variant val_dbl_2(0.25); - Attribute attr_in[] = { - node_str_attr, - node_int_attr, - val_int_attr, - node_str_attr, - val_dbl_attr, - node_int_attr - }; - Variant data_in[] = { - node_str_1, - node_int_1, - val_int_1, - node_str_2, - val_dbl_2, - node_int_2 - }; - - const char* cfg[][2] = { - { NULL, NULL } - }; - - cali_configset_t cfgset = cali_create_configset(cfg); - cali_id_t test_channel = cali_create_channel("test.push_snapshot", 0, cfgset); + Attribute attr_in[] = { node_str_attr, node_int_attr, val_int_attr, node_str_attr, val_dbl_attr, node_int_attr }; + Variant data_in[] = { node_str_1, node_int_1, val_int_1, node_str_2, val_dbl_2, node_int_2 }; + + const char* cfg[][2] = { { NULL, NULL } }; + + cali_configset_t cfgset = cali_create_configset(cfg); + cali_id_t test_channel = cali_create_channel("test.push_snapshot", 0, cfgset); cali_delete_configset(cfgset); const int count = 6; @@ -218,17 +180,16 @@ TEST(C_Snapshot_Test, PullSnapshot) { { // full snapshot - const size_t bufsize = 512; + const size_t bufsize = 512; unsigned char buf[512]; - size_t ret = - cali_channel_pull_snapshot(test_channel, CALI_SCOPE_THREAD, bufsize, buf); + size_t ret = cali_channel_pull_snapshot(test_channel, CALI_SCOPE_THREAD, bufsize, buf); ASSERT_NE(ret, 0); ASSERT_LE(ret, bufsize); UnpackSnapshotTestData t1; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_unpack_snapshot(buf, &bytes_read, ::test_entry_proc_op, &t1); @@ -238,31 +199,28 @@ TEST(C_Snapshot_Test, PullSnapshot) { EXPECT_GE(t1.entries.size(), count); for (int i = 0; i < count; ++i) { - auto it = std::find_if(t1.entries.begin(), t1.entries.end(), - [i,attr_in,data_in](const entry_data_t& e) { - return (attr_in[i].id() == e.attr_id && data_in[i] == e.val); - }); - - EXPECT_NE(it, t1.entries.end()) << " entry (" - << attr_in[i] << "," << data_in[i] - << ") not found!" << std::endl; + auto it = std::find_if(t1.entries.begin(), t1.entries.end(), [i, attr_in, data_in](const entry_data_t& e) { + return (attr_in[i].id() == e.attr_id && data_in[i] == e.val); + }); + + EXPECT_NE(it, t1.entries.end()) + << " entry (" << attr_in[i] << "," << data_in[i] << ") not found!" << std::endl; } } { // case with too small buffer - const size_t bufsize = 4; + const size_t bufsize = 4; unsigned char buf[512]; - size_t ret = - cali_channel_pull_snapshot(test_channel, CALI_SCOPE_THREAD, bufsize, buf); + size_t ret = cali_channel_pull_snapshot(test_channel, CALI_SCOPE_THREAD, bufsize, buf); ASSERT_NE(ret, 0); EXPECT_GT(ret, bufsize); UnpackSnapshotTestData t2; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_unpack_snapshot(buf, &bytes_read, ::test_entry_proc_op, &t2); @@ -286,23 +244,21 @@ TEST(C_Snapshot_Test, PullSnapshot) { EXPECT_GE(t3.entries.size(), count); } - for (int p = count-1; p >= 0; --p) + for (int p = count - 1; p >= 0; --p) c.end(attr_in[p]); cali_delete_channel(test_channel); } -TEST(C_Snapshot_Test, FindFirstInSnapshot) { - // Mixed node/immediate record unpack test. Modifies a Caliper instance. +TEST(C_Snapshot_Test, FindFirstInSnapshot) +{ + // Mixed node/immediate record unpack test. Modifies a Caliper instance. Caliper c; - Attribute node_str_attr = - c.create_attribute("findfirst.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute node_int_attr = - c.create_attribute("findfirst.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_int_attr = - c.create_attribute("findfirst.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute node_str_attr = c.create_attribute("findfirst.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute node_int_attr = c.create_attribute("findfirst.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_int_attr = c.create_attribute("findfirst.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); Variant node_str_1(CALI_TYPE_STRING, "My wonderful unpack test string", 32); Variant node_str_2(CALI_TYPE_STRING, "My other unpack test string", 27); @@ -315,22 +271,8 @@ TEST(C_Snapshot_Test, FindFirstInSnapshot) { FixedSizeSnapshotRecord<20> snapshot; - Attribute attr_in[] = { - node_str_attr, - node_int_attr, - val_int_attr, - node_str_attr, - val_int_attr, - node_int_attr - }; - Variant data_in[] = { - node_str_1, - node_int_1, - val_int_1, - node_str_2, - val_int_2, - node_int_2 - }; + Attribute attr_in[] = { node_str_attr, node_int_attr, val_int_attr, node_str_attr, val_int_attr, node_int_attr }; + Variant data_in[] = { node_str_1, node_int_1, val_int_1, node_str_2, val_int_2, node_int_2 }; c.make_record(6, attr_in, data_in, snapshot.builder()); @@ -341,7 +283,7 @@ TEST(C_Snapshot_Test, FindFirstInSnapshot) { ASSERT_EQ(rec.append(snapshot.view().size(), snapshot.view().data()), 0); UnpackSnapshotTestData t1; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_variant_t val = cali_find_first_in_snapshot(rec.data(), node_str_attr.id(), &bytes_read); @@ -349,13 +291,13 @@ TEST(C_Snapshot_Test, FindFirstInSnapshot) { EXPECT_EQ(Variant(val), node_str_2); bytes_read = 0; - val = cali_find_first_in_snapshot(rec.data(), node_int_attr.id(), &bytes_read); + val = cali_find_first_in_snapshot(rec.data(), node_int_attr.id(), &bytes_read); EXPECT_EQ(bytes_read, rec.size()); EXPECT_EQ(Variant(val), node_int_2); bytes_read = 0; - val = cali_find_first_in_snapshot(rec.data(), val_int_attr.id(), &bytes_read); + val = cali_find_first_in_snapshot(rec.data(), val_int_attr.id(), &bytes_read); EXPECT_EQ(bytes_read, rec.size()); EXPECT_EQ(Variant(val), val_int_1); @@ -363,23 +305,21 @@ TEST(C_Snapshot_Test, FindFirstInSnapshot) { // a non-existing attribute bytes_read = 0; - val = cali_find_first_in_snapshot(rec.data(), CALI_INV_ID, &bytes_read); + val = cali_find_first_in_snapshot(rec.data(), CALI_INV_ID, &bytes_read); EXPECT_EQ(bytes_read, rec.size()); EXPECT_TRUE(Variant(val).empty()); } -TEST(C_Snapshot_Test, FindAllInSnapshot) { +TEST(C_Snapshot_Test, FindAllInSnapshot) +{ // Mixed node/immediate record unpack test. Modifies a Caliper instance. Caliper c; - Attribute node_str_attr = - c.create_attribute("findall.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute node_int_attr = - c.create_attribute("findall.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_int_attr = - c.create_attribute("findall.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute node_str_attr = c.create_attribute("findall.node.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute node_int_attr = c.create_attribute("findall.node.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_int_attr = c.create_attribute("findall.val.int", CALI_TYPE_INT, CALI_ATTR_ASVALUE); Variant node_str_1(CALI_TYPE_STRING, "My wonderful findall test string", 33); Variant node_str_2(CALI_TYPE_STRING, "My other findall test string", 28); @@ -392,22 +332,8 @@ TEST(C_Snapshot_Test, FindAllInSnapshot) { FixedSizeSnapshotRecord<20> snapshot; - Attribute attr_in[] = { - node_str_attr, - node_int_attr, - val_int_attr, - node_str_attr, - val_int_attr, - node_int_attr - }; - Variant data_in[] = { - node_str_1, - node_int_1, - val_int_1, - node_str_2, - val_int_2, - node_int_2 - }; + Attribute attr_in[] = { node_str_attr, node_int_attr, val_int_attr, node_str_attr, val_int_attr, node_int_attr }; + Variant data_in[] = { node_str_1, node_int_1, val_int_1, node_str_2, val_int_2, node_int_2 }; c.make_record(6, attr_in, data_in, snapshot.builder()); auto snapshot_view = snapshot.view(); @@ -420,7 +346,7 @@ TEST(C_Snapshot_Test, FindAllInSnapshot) { { UnpackSnapshotTestData t1; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_find_all_in_snapshot(rec.data(), node_str_attr.id(), &bytes_read, ::test_entry_proc_op, &t1); @@ -435,7 +361,7 @@ TEST(C_Snapshot_Test, FindAllInSnapshot) { { UnpackSnapshotTestData t2; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_find_all_in_snapshot(rec.data(), node_int_attr.id(), &bytes_read, ::test_entry_proc_op, &t2); @@ -450,7 +376,7 @@ TEST(C_Snapshot_Test, FindAllInSnapshot) { { UnpackSnapshotTestData t3; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_find_all_in_snapshot(rec.data(), val_int_attr.id(), &bytes_read, ::test_entry_proc_op, &t3); @@ -467,7 +393,7 @@ TEST(C_Snapshot_Test, FindAllInSnapshot) { // empty UnpackSnapshotTestData t4; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_find_all_in_snapshot(rec.data(), CALI_INV_ID, &bytes_read, ::test_entry_proc_op, &t4); @@ -477,13 +403,12 @@ TEST(C_Snapshot_Test, FindAllInSnapshot) { EXPECT_EQ(t4.entries.size(), 0); } - { // quit after 1st UnpackSnapshotTestData t5; t5.max_visit_count = 1; - size_t bytes_read = 0; + size_t bytes_read = 0; cali_find_all_in_snapshot(rec.data(), node_int_attr.id(), &bytes_read, ::test_entry_proc_op, &t5); diff --git a/src/caliper/test/test_c_wrapper.cpp b/src/caliper/test/test_c_wrapper.cpp index 6d687a272..d1d3b4c94 100644 --- a/src/caliper/test/test_c_wrapper.cpp +++ b/src/caliper/test/test_c_wrapper.cpp @@ -8,7 +8,8 @@ #include -TEST(C_Wrapper, BufferedRegionProfile) { +TEST(C_Wrapper, BufferedRegionProfile) +{ cali_BufferedRegionProfile rp; cali_BufferedRegionProfile_new(&rp); diff --git a/src/caliper/test/test_channel_api.cpp b/src/caliper/test/test_channel_api.cpp index fa269c8fa..3e7eb078f 100644 --- a/src/caliper/test/test_channel_api.cpp +++ b/src/caliper/test/test_channel_api.cpp @@ -7,21 +7,18 @@ using namespace cali; -TEST(ChannelAPITest, MultiChannel) { - Caliper c; +TEST(ChannelAPITest, MultiChannel) +{ + Caliper c; - cali_id_t chn_a_id = - create_channel("chn.m.a", 0, { { "CALI_CHANNEL_CONFIG_CHECK", "false" } }); - cali_id_t chn_b_id = - create_channel("chn.m.b", 0, { { "CALI_CHANNEL_CONFIG_CHECK", "false" } }); + cali_id_t chn_a_id = create_channel("chn.m.a", 0, { { "CALI_CHANNEL_CONFIG_CHECK", "false" } }); + cali_id_t chn_b_id = create_channel("chn.m.b", 0, { { "CALI_CHANNEL_CONFIG_CHECK", "false" } }); Channel chn_a = c.get_channel(chn_a_id); Channel chn_b = c.get_channel(chn_b_id); - Attribute attr_global = - c.create_attribute("multichn.global", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute attr_local = - c.create_attribute("multichn.local", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute attr_global = c.create_attribute("multichn.global", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute attr_local = c.create_attribute("multichn.local", CALI_TYPE_INT, CALI_ATTR_DEFAULT); c.begin(attr_global, Variant(42)); @@ -57,21 +54,15 @@ TEST(ChannelAPITest, MultiChannel) { c.delete_channel(chn_b); } -TEST(ChannelAPITest, C_API) { - const char* cfg[][2] = { - { "CALI_CHANNEL_CONFIG_CHECK", "false" }, - { nullptr, nullptr } - }; +TEST(ChannelAPITest, C_API) +{ + const char* cfg[][2] = { { "CALI_CHANNEL_CONFIG_CHECK", "false" }, { nullptr, nullptr } }; - cali_configset_t cfgset = - cali_create_configset(cfg); + cali_configset_t cfgset = cali_create_configset(cfg); - cali_id_t chn_a_id = - cali_create_channel("chn.c_api.a", 0, cfgset); - cali_id_t chn_b_id = - cali_create_channel("chn.c_api.b", 0, cfgset); - cali_id_t chn_c_id = - cali_create_channel("chn.c_api.c", CALI_CHANNEL_LEAVE_INACTIVE, cfgset); + cali_id_t chn_a_id = cali_create_channel("chn.c_api.a", 0, cfgset); + cali_id_t chn_b_id = cali_create_channel("chn.c_api.b", 0, cfgset); + cali_id_t chn_c_id = cali_create_channel("chn.c_api.c", CALI_CHANNEL_LEAVE_INACTIVE, cfgset); cali_delete_configset(cfgset); @@ -97,14 +88,12 @@ TEST(ChannelAPITest, C_API) { EXPECT_EQ(cali_variant_to_int(cali_get(attr_a), NULL), 7744); unsigned char rec[60]; - size_t len = - cali_channel_pull_snapshot(channel_id, CALI_SCOPE_THREAD, 60, rec); + size_t len = cali_channel_pull_snapshot(channel_id, CALI_SCOPE_THREAD, 60, rec); ASSERT_NE(len, 0); ASSERT_LT(len, 60); - cali_variant_t val_a = - cali_find_first_in_snapshot(rec, attr_a, nullptr); + cali_variant_t val_a = cali_find_first_in_snapshot(rec, attr_a, nullptr); EXPECT_EQ(cali_variant_to_int(val_a, nullptr), 7744); } @@ -119,14 +108,12 @@ TEST(ChannelAPITest, C_API) { { unsigned char rec[60]; - size_t len = - cali_channel_pull_snapshot(chn_b_id, CALI_SCOPE_THREAD, 60, rec); + size_t len = cali_channel_pull_snapshot(chn_b_id, CALI_SCOPE_THREAD, 60, rec); ASSERT_NE(len, 0); ASSERT_LT(len, 60); - cali_variant_t val_a = - cali_find_first_in_snapshot(rec, attr_a, nullptr); + cali_variant_t val_a = cali_find_first_in_snapshot(rec, attr_a, nullptr); EXPECT_TRUE(cali_variant_is_empty(val_a)); } @@ -142,21 +129,21 @@ TEST(ChannelAPITest, C_API) { EXPECT_FALSE(c.get_channel(chn_c_id)); } -TEST(ChannelAPITest, WriteReport) { +TEST(ChannelAPITest, WriteReport) +{ Caliper c; - cali_id_t chn_id = - create_channel("chn.report", 0, { - { "CALI_SERVICES_ENABLE", "event,trace" }, - { "CALI_CHANNEL_CONFIG_CHECK", "false" } - }); + cali_id_t chn_id = create_channel( + "chn.report", + 0, + { { "CALI_SERVICES_ENABLE", "event,trace" }, { "CALI_CHANNEL_CONFIG_CHECK", "false" } } + ); cali_begin_int_byname("chn.report.int", 42); cali_end_byname("chn.report.int"); std::ostringstream oss; - const char* query = - "SELECT chn.report.int WHERE chn.report.int FORMAT expand"; + const char* query = "SELECT chn.report.int WHERE chn.report.int FORMAT expand"; write_report_for_query(chn_id, query, 0, oss); diff --git a/src/caliper/test/test_channel_controller.cpp b/src/caliper/test/test_channel_controller.cpp index 5e8c262fa..223485a42 100644 --- a/src/caliper/test/test_channel_controller.cpp +++ b/src/caliper/test/test_channel_controller.cpp @@ -13,17 +13,19 @@ TEST(ChannelControllerTest, ChannelController) bool saw_create_callback = false; Channel the_channel; - void on_create(Caliper*, Channel& chn) { + void on_create(Caliper*, Channel& chn) + { saw_create_callback = true; - the_channel = chn; + the_channel = chn; } TestCC() - : ChannelController("testCC", 0, { - { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, - { "CALI_CHANNEL_CONFIG_CHECK", "false" } - }) - { } + : ChannelController( + "testCC", + 0, + { { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, { "CALI_CHANNEL_CONFIG_CHECK", "false" } } + ) + {} }; TestCC testCC; @@ -49,23 +51,23 @@ TEST(ChannelControllerTest, ChannelController) TEST(ChannelControllerTest, DestroyChannel) { struct DestroyTestCC : public ChannelController { - void destruct() { + void destruct() + { if (is_active()) { Channel chn = channel(); Caliper::instance().delete_channel(chn); } } - bool channel_is_null() { - return !channel(); - } + bool channel_is_null() { return !channel(); } DestroyTestCC() - : ChannelController("DestroyTestCC", 0, { - { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, - { "CALI_CHANNEL_CONFIG_CHECK", "false" } - }) - { } + : ChannelController( + "DestroyTestCC", + 0, + { { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, { "CALI_CHANNEL_CONFIG_CHECK", "false" } } + ) + {} }; DestroyTestCC testCC; diff --git a/src/caliper/test/test_configmanager.cpp b/src/caliper/test/test_configmanager.cpp index 977eee8ac..3ad0701b2 100644 --- a/src/caliper/test/test_configmanager.cpp +++ b/src/caliper/test/test_configmanager.cpp @@ -8,7 +8,8 @@ using namespace cali; -TEST(ConfigManagerTest, ParseErrors) { +TEST(ConfigManagerTest, ParseErrors) +{ { cali::ConfigManager mgr; @@ -42,13 +43,12 @@ TEST(ConfigManagerTest, ParseErrors) { EXPECT_STREQ(mgr.error_msg().c_str(), "Expected ')'"); } - EXPECT_STREQ(cali::ConfigManager::check_config_string("foo").c_str(), - "Unknown config or parameter: foo"); - EXPECT_STREQ(cali::ConfigManager::check_config_string("event-trace,").c_str(), - "Unknown config or parameter: "); + EXPECT_STREQ(cali::ConfigManager::check_config_string("foo").c_str(), "Unknown config or parameter: foo"); + EXPECT_STREQ(cali::ConfigManager::check_config_string("event-trace,").c_str(), "Unknown config or parameter: "); } -TEST(ConfigManagerTest, ParseConfig) { +TEST(ConfigManagerTest, ParseConfig) +{ { cali::ConfigManager mgr; @@ -62,10 +62,12 @@ TEST(ConfigManagerTest, ParseConfig) { } { - cali::ConfigManager mgr; + cali::ConfigManager mgr; cali::ConfigManager::argmap_t extra_kv_pairs; - EXPECT_TRUE(mgr.add(" event-trace, runtime-report, aggregate_across_ranks=false, foo=bar , blagarbl ", extra_kv_pairs)); + EXPECT_TRUE( + mgr.add(" event-trace, runtime-report, aggregate_across_ranks=false, foo=bar , blagarbl ", extra_kv_pairs) + ); EXPECT_FALSE(mgr.error()); auto list = mgr.get_all_channels(); @@ -83,7 +85,9 @@ TEST(ConfigManagerTest, ParseConfig) { { cali::ConfigManager mgr; - EXPECT_TRUE(mgr.add(" event-trace ( output = test.cali ), runtime-report(output=stdout,aggregate_across_ranks=false ) ")); + EXPECT_TRUE(mgr.add( + " event-trace ( output = test.cali ), runtime-report(output=stdout,aggregate_across_ranks=false ) " + )); EXPECT_FALSE(mgr.error()); auto list = mgr.get_all_channels(); @@ -98,7 +102,8 @@ TEST(ConfigManagerTest, ParseConfig) { EXPECT_TRUE(cali::ConfigManager::check_config_string("runtime-report,event-trace,foo=bar", true).empty()); } -TEST(ConfigManagerTest, ParseEmptyConfig) { +TEST(ConfigManagerTest, ParseEmptyConfig) +{ { cali::ConfigManager mgr; mgr.add(""); @@ -114,7 +119,6 @@ TEST(ConfigManagerTest, ParseEmptyConfig) { } } - namespace { @@ -123,26 +127,19 @@ class TestController : public cali::ChannelController cali::ConfigManager::Options opts; TestController(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& o) - : ChannelController(name, 0, initial_cfg), - opts(o) - { - } + : ChannelController(name, 0, initial_cfg), opts(o) + {} public: - std::string get_opt(const char* name) { - return opts.get(name).to_string(); - } + std::string get_opt(const char* name) { return opts.get(name).to_string(); } - bool is_set(const char* name) { - return opts.is_set(name); - } + bool is_set(const char* name) { return opts.is_set(name); } - bool is_enabled(const char* name) { - return opts.is_enabled(name); - } + bool is_enabled(const char* name) { return opts.is_enabled(name); } - bool enabled_opts_list_matches(const std::vector list) { + bool enabled_opts_list_matches(const std::vector list) + { std::vector a(list); std::vector b(opts.enabled_options()); std::sort(a.begin(), a.end()); @@ -150,22 +147,28 @@ class TestController : public cali::ChannelController return a == b; } - std::string get_query(const char* level, const std::map& in) const { + std::string get_query(const char* level, const std::map& in) const + { return opts.build_query(level, in); } - std::string get_config(const std::string& key) { + std::string get_config(const std::string& key) + { config_map_t cfg; opts.update_channel_config(cfg); return cfg[key]; } - static cali::ChannelController* create(const char* name, const config_map_t& initial_cfg, const cali::ConfigManager::Options& opts) { + static cali::ChannelController* create( + const char* name, + const config_map_t& initial_cfg, + const cali::ConfigManager::Options& opts + ) + { return new TestController(name, initial_cfg, opts); } - ~TestController() - { } + ~TestController() {} }; const char* testcontroller_spec = R"json( @@ -241,7 +244,7 @@ const char* test_option_spec = R"json( ]; )json"; -} // namespace [anonymous] +} // namespace TEST(ConfigManagerTest, Options) { @@ -259,8 +262,9 @@ TEST(ConfigManagerTest, Options) std::string expected_configs[] = { "event-trace", "runtime-report", "testcontroller" }; - EXPECT_TRUE(std::includes(configs.begin(), configs.end(), - std::begin(expected_configs), std::end(expected_configs))); + EXPECT_TRUE( + std::includes(configs.begin(), configs.end(), std::begin(expected_configs), std::end(expected_configs)) + ); } { @@ -318,7 +322,7 @@ TEST(ConfigManagerTest, Options) EXPECT_TRUE(tP->is_enabled("boolopt")); EXPECT_FALSE(tP->is_enabled("defaultopt")); - EXPECT_EQ(tP->get_opt("intopt"), std::string("42")); + EXPECT_EQ(tP->get_opt("intopt"), std::string("42")); EXPECT_EQ(tP->get_opt("stringopt"), std::string("set_default_parameter")); } } @@ -360,13 +364,14 @@ TEST(ConfigManagerTest, BuildQuery) EXPECT_TRUE(tP->is_enabled("boolopt")); - std::string q1 = tP->get_query("local", { - { "select", "me" }, - { "format", "expand" }, - { "let", "a=first(b,c)" }, - { "where", "xyz=42" }, - { "group by", "z"} - }); + std::string q1 = tP->get_query( + "local", + { { "select", "me" }, + { "format", "expand" }, + { "let", "a=first(b,c)" }, + { "where", "xyz=42" }, + { "group by", "z" } } + ); const char* expect = " let a=first(b,c),x=scale(y,2)" " select me,sum(x) as \"X\" unit \"Foos\"" @@ -378,10 +383,13 @@ TEST(ConfigManagerTest, BuildQuery) EXPECT_STREQ(q1.c_str(), expect); - std::string q2 = tP->get_query("local", { + std::string q2 = tP->get_query( + "local", + { { "select", "me" }, { "format", "expand" }, - }); + } + ); expect = " let x=scale(y,2)" " select me,sum(x) as \"X\" unit \"Foos\"" @@ -393,7 +401,6 @@ TEST(ConfigManagerTest, BuildQuery) EXPECT_STREQ(q2.c_str(), expect); } - { ConfigManager mgr; mgr.add_config_spec(testcontroller_info); @@ -406,13 +413,14 @@ TEST(ConfigManagerTest, BuildQuery) EXPECT_FALSE(tP->is_enabled("boolopt")); - std::string q3 = tP->get_query("local", { - { "select", "me" }, - { "format", "expand" }, - { "let", "a=first(b,c)" }, - { "where", "xyz=42" }, - { "group by", "z"} - }); + std::string q3 = tP->get_query( + "local", + { { "select", "me" }, + { "format", "expand" }, + { "let", "a=first(b,c)" }, + { "where", "xyz=42" }, + { "group by", "z" } } + ); const char* expect = " let a=first(b,c)" " select me" @@ -422,10 +430,13 @@ TEST(ConfigManagerTest, BuildQuery) EXPECT_STREQ(q3.c_str(), expect); - std::string q4 = tP->get_query("local", { + std::string q4 = tP->get_query( + "local", + { { "select", "me" }, { "format", "expand" }, - }); + } + ); expect = " select me" " format expand"; @@ -434,7 +445,8 @@ TEST(ConfigManagerTest, BuildQuery) } } -TEST(ConfigManagerTest, LoadCmd_SingleConfig) { +TEST(ConfigManagerTest, LoadCmd_SingleConfig) +{ // test parsing of a single config spec with the load() command inside add() ConfigManager mgr; @@ -445,7 +457,8 @@ TEST(ConfigManagerTest, LoadCmd_SingleConfig) { EXPECT_TRUE(cP); } -TEST(ConfigManagerTest, LoadCmd_ConfigList) { +TEST(ConfigManagerTest, LoadCmd_ConfigList) +{ // test parsing of a config spec list with the load() command inside add() ConfigManager mgr; @@ -455,7 +468,8 @@ TEST(ConfigManagerTest, LoadCmd_ConfigList) { EXPECT_EQ(mgr.get_documentation_for_spec("testcontroller_b"), std::string("testcontroller_b\n Test controller B")); } -TEST(ConfigManagerTest, LoadCmd_ConfigAndOptions) { +TEST(ConfigManagerTest, LoadCmd_ConfigAndOptions) +{ // test parsing of a config and options list with load() ConfigManager mgr; diff --git a/src/caliper/test/test_metadatatree.cpp b/src/caliper/test/test_metadatatree.cpp index 331ae6fd0..dbd5426f5 100644 --- a/src/caliper/test/test_metadatatree.cpp +++ b/src/caliper/test/test_metadatatree.cpp @@ -12,25 +12,24 @@ using namespace cali; using namespace cali::internal; -TEST(MetadataTreeTest, BigTree) { +TEST(MetadataTreeTest, BigTree) +{ // just create a lot of nodes Caliper c; - Attribute str_attr = - c.create_attribute("test.metatree.replaceall.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute int_attr = - c.create_attribute("test.metatree.replaceall.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute str_attr = c.create_attribute("test.metatree.replaceall.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute int_attr = c.create_attribute("test.metatree.replaceall.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); const std::string chars { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }; MetadataTree tree; - Node* node = tree.root(); + Node* node = tree.root(); for (int i = 0; i < 400000; ++i) { - std::string sval = chars.substr(i % (chars.length()/2)); - + std::string sval = chars.substr(i % (chars.length() / 2)); + node = tree.get_child(str_attr, Variant(CALI_TYPE_STRING, sval.data(), sval.size()), node); - node = tree.get_child(int_attr, Variant(2*i+1), node); + node = tree.get_child(int_attr, Variant(2 * i + 1), node); ASSERT_NE(node, nullptr); } @@ -38,7 +37,7 @@ TEST(MetadataTreeTest, BigTree) { int str_count = 0; int int_count = 0; - for ( ; node; node = node->parent()) { + for (; node; node = node->parent()) { if (node->attribute() == int_attr.id()) ++int_count; if (node->attribute() == str_attr.id()) @@ -46,18 +45,17 @@ TEST(MetadataTreeTest, BigTree) { } EXPECT_EQ(str_count, 400000); - EXPECT_EQ(int_count, 400000); - + EXPECT_EQ(int_count, 400000); + tree.print_statistics(std::cout) << std::endl; } -TEST(MetadataTreeTest, ReplaceAll) { +TEST(MetadataTreeTest, ReplaceAll) +{ Caliper c; - Attribute str_attr = - c.create_attribute("test.metatree.replaceall.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute int_attr = - c.create_attribute("test.metatree.replaceall.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute str_attr = c.create_attribute("test.metatree.replaceall.str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute int_attr = c.create_attribute("test.metatree.replaceall.int", CALI_TYPE_INT, CALI_ATTR_DEFAULT); MetadataTree tree; @@ -68,10 +66,10 @@ TEST(MetadataTreeTest, ReplaceAll) { Node* node = tree.root(); for (int i = 0; i < 20000; ++i) { - std::string sval = chars.substr(i % (chars.length()/2)); - + std::string sval = chars.substr(i % (chars.length() / 2)); + node = tree.get_child(str_attr, Variant(CALI_TYPE_STRING, sval.data(), sval.size()), node); - node = tree.get_child(int_attr, Variant(2*i+1), node); + node = tree.get_child(int_attr, Variant(2 * i + 1), node); ASSERT_NE(node, nullptr); } @@ -97,7 +95,7 @@ TEST(MetadataTreeTest, ReplaceAll) { int str_count = 0; int int_count = 0; - for ( ; node; node = node->parent()) { + for (; node; node = node->parent()) { if (node->attribute() == int_attr.id()) ++int_count; if (node->attribute() == str_attr.id()) diff --git a/src/caliper/test/test_postprocess_snapshot.cpp b/src/caliper/test/test_postprocess_snapshot.cpp index 3ac8a682b..8a5077510 100644 --- a/src/caliper/test/test_postprocess_snapshot.cpp +++ b/src/caliper/test/test_postprocess_snapshot.cpp @@ -13,8 +13,7 @@ namespace void flush_cb(Caliper* c, Channel*, SnapshotView, SnapshotFlushFn flush_fn) { - Attribute snapshot_attr = - c->create_attribute("tps.snapshot.val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute snapshot_attr = c->create_attribute("tps.snapshot.val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); std::vector rec; rec.push_back(Entry(snapshot_attr, Variant(49))); @@ -24,16 +23,14 @@ void flush_cb(Caliper* c, Channel*, SnapshotView, SnapshotFlushFn flush_fn) void postprocess_snapshot_cb(Caliper* c, Channel*, std::vector& rec) { - Attribute val_attr = - c->create_attribute("tps.postprocess.val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute node_attr = - c->create_attribute("tps.postprocess.node", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = c->create_attribute("tps.postprocess.val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute node_attr = c->create_attribute("tps.postprocess.node", CALI_TYPE_INT, CALI_ATTR_DEFAULT); rec.push_back(Entry(val_attr, Variant(42))); rec.push_back(Entry(c->make_tree_entry(node_attr, Variant(36)))); } -} +} // namespace TEST(PostprocessSnapshotTest, PostprocessSnapshot) { @@ -46,15 +43,15 @@ TEST(PostprocessSnapshotTest, PostprocessSnapshot) channel.events().flush_evt.connect(::flush_cb); channel.events().postprocess_snapshot.connect(::postprocess_snapshot_cb); - std::vector< std::vector > output; + std::vector> output; - c.flush(&channel, SnapshotView(), [&output](CaliperMetadataAccessInterface& db, const std::vector& rec){ - output.push_back(rec); - }); + c.flush(&channel, SnapshotView(), [&output](CaliperMetadataAccessInterface& db, const std::vector& rec) { + output.push_back(rec); + }); Attribute snapshot_val_attr = c.get_attribute("tps.snapshot.val"); - Attribute post_val_attr = c.get_attribute("tps.postprocess.val"); - Attribute post_node_attr = c.get_attribute("tps.postprocess.node"); + Attribute post_val_attr = c.get_attribute("tps.postprocess.val"); + Attribute post_node_attr = c.get_attribute("tps.postprocess.node"); ASSERT_EQ(output.size(), 1); ASSERT_EQ(output.front().size(), 3); diff --git a/src/caliper/test/test_regionfilter.cpp b/src/caliper/test/test_regionfilter.cpp index 283d0a1b8..f1a1514a6 100644 --- a/src/caliper/test/test_regionfilter.cpp +++ b/src/caliper/test/test_regionfilter.cpp @@ -1,79 +1,78 @@ #include "../RegionFilter.h" -#include "caliper/common/Variant.h" +#include "caliper/common/Variant.h" #include using namespace cali; -TEST(RegionFilterTest, IncludeExclude) { +TEST(RegionFilterTest, IncludeExclude) +{ auto p = RegionFilter::from_config( - " \" exact match\", match(\"matchme\"), startswith(start, mpi_))", - "\"start exclude\" ,startswith(mpi_exclude)" - ); + " \" exact match\", match(\"matchme\"), startswith(start, mpi_))", + "\"start exclude\" ,startswith(mpi_exclude)" + ); ASSERT_TRUE(p.second.empty()); RegionFilter f(p.first); - EXPECT_TRUE (f.pass( Variant(" exact match" ))); - EXPECT_FALSE (f.pass( Variant("some random string" ))); - EXPECT_TRUE (f.pass( Variant("matchme" ))); - EXPECT_TRUE (f.pass( Variant("starts with the magic word" ))); - EXPECT_FALSE (f.pass( Variant("start exclude" ))); - EXPECT_TRUE (f.pass( Variant("mpi_include_me" ))); - EXPECT_FALSE (f.pass( Variant("mpi_exclude_me" ))); - EXPECT_FALSE (f.pass( Variant("sta" ))); + EXPECT_TRUE(f.pass(Variant(" exact match"))); + EXPECT_FALSE(f.pass(Variant("some random string"))); + EXPECT_TRUE(f.pass(Variant("matchme"))); + EXPECT_TRUE(f.pass(Variant("starts with the magic word"))); + EXPECT_FALSE(f.pass(Variant("start exclude"))); + EXPECT_TRUE(f.pass(Variant("mpi_include_me"))); + EXPECT_FALSE(f.pass(Variant("mpi_exclude_me"))); + EXPECT_FALSE(f.pass(Variant("sta"))); } -TEST(RegionFilterTest, IncludeOnly) { - auto p = RegionFilter::from_config( - " \" exact match\", match(\"matchme\"), startswith(start, mpi_))", - " " - ); +TEST(RegionFilterTest, IncludeOnly) +{ + auto p = RegionFilter::from_config(" \" exact match\", match(\"matchme\"), startswith(start, mpi_))", " "); ASSERT_TRUE(p.second.empty()); RegionFilter f(p.first); - EXPECT_TRUE (f.pass( Variant(" exact match" ))); - EXPECT_FALSE (f.pass( Variant("some random string" ))); - EXPECT_TRUE (f.pass( Variant("matchme" ))); - EXPECT_TRUE (f.pass( Variant("starts with the magic word" ))); - EXPECT_TRUE (f.pass( Variant("mpi_include_me" ))); - EXPECT_FALSE (f.pass( Variant("sta" ))); + EXPECT_TRUE(f.pass(Variant(" exact match"))); + EXPECT_FALSE(f.pass(Variant("some random string"))); + EXPECT_TRUE(f.pass(Variant("matchme"))); + EXPECT_TRUE(f.pass(Variant("starts with the magic word"))); + EXPECT_TRUE(f.pass(Variant("mpi_include_me"))); + EXPECT_FALSE(f.pass(Variant("sta"))); } -TEST(RegionFilterTest, ExcludeOnly) { - auto p = RegionFilter::from_config( - " ", - "\" exclude\" ,startswith(mpi_exclude)" - ); +TEST(RegionFilterTest, ExcludeOnly) +{ + auto p = RegionFilter::from_config(" ", "\" exclude\" ,startswith(mpi_exclude)"); ASSERT_TRUE(p.second.empty()); RegionFilter f(p.first); - EXPECT_TRUE (f.pass( Variant("some random string" ))); - EXPECT_FALSE (f.pass( Variant(" exclude" ))); - EXPECT_TRUE (f.pass( Variant("mpi_include_me" ))); - EXPECT_FALSE (f.pass( Variant("mpi_exclude_me" ))); - EXPECT_TRUE (f.pass( Variant("mpi" ))); + EXPECT_TRUE(f.pass(Variant("some random string"))); + EXPECT_FALSE(f.pass(Variant(" exclude"))); + EXPECT_TRUE(f.pass(Variant("mpi_include_me"))); + EXPECT_FALSE(f.pass(Variant("mpi_exclude_me"))); + EXPECT_TRUE(f.pass(Variant("mpi"))); } -TEST(RegionFilterTest, IncludeRegex) { +TEST(RegionFilterTest, IncludeRegex) +{ auto p = RegionFilter::from_config(" regex(\".*match\") ", ""); ASSERT_TRUE(p.second.empty()); RegionFilter f(p.first); - EXPECT_TRUE (f.pass( Variant("i should match" ))); - EXPECT_FALSE (f.pass( Variant("i should match not" ))); - EXPECT_FALSE (f.pass( Variant("me neither" ))); + EXPECT_TRUE(f.pass(Variant("i should match"))); + EXPECT_FALSE(f.pass(Variant("i should match not"))); + EXPECT_FALSE(f.pass(Variant("me neither"))); } -TEST(RegionFilterTest, ParseError) { +TEST(RegionFilterTest, ParseError) +{ auto p = RegionFilter::from_config("match(bar, foo, startswith(fox)", ""); ASSERT_FALSE(p.second.empty()); diff --git a/src/common/Attribute.cpp b/src/common/Attribute.cpp index ae56a59ca..938512f3b 100644 --- a/src/common/Attribute.cpp +++ b/src/common/Attribute.cpp @@ -10,14 +10,12 @@ using namespace cali; using namespace std; -Attribute -Attribute::make_attribute(Node* node) +Attribute Attribute::make_attribute(Node* node) { return node && node->attribute() == NAME_ATTR_ID ? Attribute(node) : Attribute(); } -std::string -Attribute::name() const +std::string Attribute::name() const { for (const Node* node = m_node; node; node = node->parent()) if (node->attribute() == NAME_ATTR_ID) @@ -26,8 +24,7 @@ Attribute::name() const return std::string(); } -const char* -Attribute::name_c_str() const +const char* Attribute::name_c_str() const { for (const Node* node = m_node; node; node = node->parent()) if (node->attribute() == NAME_ATTR_ID) @@ -36,8 +33,7 @@ Attribute::name_c_str() const return nullptr; } -cali_attr_type -Attribute::type() const +cali_attr_type Attribute::type() const { for (const Node* node = m_node; node; node = node->parent()) if (node->attribute() == TYPE_ATTR_ID) @@ -46,8 +42,7 @@ Attribute::type() const return CALI_TYPE_INV; } -int -Attribute::properties() const +int Attribute::properties() const { for (const Node* node = m_node; node; node = node->parent()) if (node->attribute() == PROP_ATTR_ID) @@ -56,8 +51,7 @@ Attribute::properties() const return CALI_ATTR_DEFAULT; } -Variant -Attribute::get(const Attribute& attr) const +Variant Attribute::get(const Attribute& attr) const { for (const Node* node = m_node; node; node = node->parent()) if (node->attribute() == attr.id()) @@ -66,15 +60,13 @@ Attribute::get(const Attribute& attr) const return Variant(); } -std::ostream& -cali::operator << (std::ostream& os, const Attribute& a) +std::ostream& cali::operator<< (std::ostream& os, const Attribute& a) { char buf[256]; cali_prop2string(a.properties(), buf, 256); - return os << "{ \"id\" : " << a.id() - << ", \"name\" : \"" << a.name() << "\"" + return os << "{ \"id\" : " << a.id() << ", \"name\" : \"" << a.name() << "\"" << ", \"type\" : \"" << cali_type2string(a.type()) << "\"" << ", \"properties\" : \"" << buf << "\" }"; } diff --git a/src/common/CaliperMetadataAccessInterface.cpp b/src/common/CaliperMetadataAccessInterface.cpp index 978afd664..5760d245f 100644 --- a/src/common/CaliperMetadataAccessInterface.cpp +++ b/src/common/CaliperMetadataAccessInterface.cpp @@ -8,8 +8,7 @@ using namespace cali; -std::vector -CaliperMetadataAccessInterface::find_attributes_with(const Attribute& meta) const +std::vector CaliperMetadataAccessInterface::find_attributes_with(const Attribute& meta) const { std::vector vec = get_all_attributes(); std::vector ret; @@ -21,8 +20,7 @@ CaliperMetadataAccessInterface::find_attributes_with(const Attribute& meta) cons return ret; } -std::vector -CaliperMetadataAccessInterface::find_attributes_with_prop(int prop) const +std::vector CaliperMetadataAccessInterface::find_attributes_with_prop(int prop) const { std::vector vec = get_all_attributes(); std::vector ret; @@ -37,8 +35,7 @@ CaliperMetadataAccessInterface::find_attributes_with_prop(int prop) const namespace cali { -Entry -get_path_entry(const CaliperMetadataAccessInterface& db, const Entry& e) +Entry get_path_entry(const CaliperMetadataAccessInterface& db, const Entry& e) { Entry ret; @@ -53,5 +50,4 @@ get_path_entry(const CaliperMetadataAccessInterface& db, const Entry& e) return ret; } -} - +} // namespace cali diff --git a/src/common/CompressedSnapshotRecord.cpp b/src/common/CompressedSnapshotRecord.cpp index 1b4e73009..0a91bbb0d 100644 --- a/src/common/CompressedSnapshotRecord.cpp +++ b/src/common/CompressedSnapshotRecord.cpp @@ -17,65 +17,62 @@ using namespace cali; - namespace { - /// \brief Async-signal safe memory move. - /// Destination and source may overlap. - inline void* save_memmove(void* dst, void* src, size_t len) - { - if (src == dst) - return dst; - - while (len > 0) { - unsigned char tmp[64]; - size_t blk = std::min(len, 64); - len -= blk; +/// \brief Async-signal safe memory move. +/// Destination and source may overlap. +inline void* save_memmove(void* dst, void* src, size_t len) +{ + if (src == dst) + return dst; - memcpy(tmp, static_cast(src)+len, blk); - memcpy(static_cast(dst)+len, tmp, blk); - } + while (len > 0) { + unsigned char tmp[64]; + size_t blk = std::min(len, 64); + len -= blk; - return dst; + memcpy(tmp, static_cast(src) + len, blk); + memcpy(static_cast(dst) + len, tmp, blk); } + + return dst; } +} // namespace // // --- CompressedSnapshotRecordView // -CompressedSnapshotRecordView::CompressedSnapshotRecordView(const unsigned char* buf, size_t* inc) - : m_buffer(buf) +CompressedSnapshotRecordView::CompressedSnapshotRecordView(const unsigned char* buf, size_t* inc) : m_buffer(buf) { size_t pos = 0; - + m_num_nodes = buf[pos++]; - + for (size_t i = 0; i < m_num_nodes; ++i) - vldec_u64(buf+pos, &pos); + vldec_u64(buf + pos, &pos); m_imm_len = 0; m_imm_pos = pos; m_num_imm = buf[pos++]; for (size_t i = 0; i < m_num_imm; ++i) { - vldec_u64(buf+pos, &pos); - Variant::unpack(buf+pos, &pos, nullptr); + vldec_u64(buf + pos, &pos); + Variant::unpack(buf + pos, &pos, nullptr); } m_imm_len = pos - m_imm_pos; *inc += pos; } -Entry -CompressedSnapshotRecordView::unpack_next_entry(const CaliperMetadataAccessInterface* c, size_t& n, size_t& pos) +Entry CompressedSnapshotRecordView::unpack_next_entry(const CaliperMetadataAccessInterface* c, size_t& n, size_t& pos) { if (n == 0) pos = 1; - + if (n < m_num_nodes) { ++n; - return Entry(c->node(vldec_u64(m_buffer+pos, &pos))); + return Entry(c->node(vldec_u64(m_buffer + pos, &pos))); } if (n == m_num_nodes) @@ -84,8 +81,8 @@ CompressedSnapshotRecordView::unpack_next_entry(const CaliperMetadataAccessInter if (n < m_num_nodes + m_num_imm) { ++n; - cali_id_t id = vldec_u64(m_buffer+pos, &pos); - Variant data = Variant::unpack(m_buffer+pos, &pos, nullptr); + cali_id_t id = vldec_u64(m_buffer + pos, &pos); + Variant data = Variant::unpack(m_buffer + pos, &pos, nullptr); return Entry(c->get_attribute(id), data); } @@ -94,31 +91,28 @@ CompressedSnapshotRecordView::unpack_next_entry(const CaliperMetadataAccessInter } /// \brief Unpack node entries -void -CompressedSnapshotRecordView::unpack_nodes(size_t n, cali_id_t node_vec[]) const +void CompressedSnapshotRecordView::unpack_nodes(size_t n, cali_id_t node_vec[]) const { size_t max = std::min(n, m_num_nodes); size_t pos = 1; for (size_t i = 0; i < max; ++i) - node_vec[i] = vldec_u64(m_buffer+pos, &pos); + node_vec[i] = vldec_u64(m_buffer + pos, &pos); } /// \brief Unpack immediate entries -void -CompressedSnapshotRecordView::unpack_immediate(size_t n, cali_id_t attr_vec[], Variant data_vec[]) const +void CompressedSnapshotRecordView::unpack_immediate(size_t n, cali_id_t attr_vec[], Variant data_vec[]) const { size_t max = std::min(n, m_num_imm); size_t pos = m_imm_pos + 1; for (size_t i = 0; i < max; ++i) { - attr_vec[i] = vldec_u64(m_buffer+pos, &pos); - data_vec[i] = Variant::unpack(m_buffer+pos, &pos, nullptr); + attr_vec[i] = vldec_u64(m_buffer + pos, &pos); + data_vec[i] = Variant::unpack(m_buffer + pos, &pos, nullptr); } } -std::vector -CompressedSnapshotRecordView::to_entrylist(const CaliperMetadataAccessInterface* c) const +std::vector CompressedSnapshotRecordView::to_entrylist(const CaliperMetadataAccessInterface* c) const { std::vector list; @@ -128,15 +122,15 @@ CompressedSnapshotRecordView::to_entrylist(const CaliperMetadataAccessInterface* size_t pos = 1; for (size_t i = 0; i < m_num_nodes; ++i) - list.push_back(Entry(c->node(vldec_u64(m_buffer+pos, &pos)))); + list.push_back(Entry(c->node(vldec_u64(m_buffer + pos, &pos)))); } { size_t pos = m_imm_pos + 1; for (size_t i = 0; i < m_num_imm; ++i) { - cali_id_t id = vldec_u64(m_buffer+pos, &pos); - Variant data = Variant::unpack(m_buffer+pos, &pos, nullptr); + cali_id_t id = vldec_u64(m_buffer + pos, &pos); + Variant data = Variant::unpack(m_buffer + pos, &pos, nullptr); list.push_back(Entry(c->get_attribute(id), data)); } @@ -145,7 +139,6 @@ CompressedSnapshotRecordView::to_entrylist(const CaliperMetadataAccessInterface* return list; } - // // --- CompressedSnapshotRecord // @@ -165,50 +158,48 @@ CompressedSnapshotRecord::CompressedSnapshotRecord(size_t len, unsigned char* bu CompressedSnapshotRecord::CompressedSnapshotRecord() : CompressedSnapshotRecord(m_internal_buffer_size, m_internal_buffer) -{ } +{} -CompressedSnapshotRecord::CompressedSnapshotRecord(size_t n, const Entry entrylist[]) - : CompressedSnapshotRecord() +CompressedSnapshotRecord::CompressedSnapshotRecord(size_t n, const Entry entrylist[]) : CompressedSnapshotRecord() { append(n, entrylist); } CompressedSnapshotRecord::~CompressedSnapshotRecord() -{ } +{} /// \brief Append node entries -size_t -CompressedSnapshotRecord::append(size_t n, const Node* const node_vec[]) +size_t CompressedSnapshotRecord::append(size_t n, const Node* const node_vec[]) { size_t skipped = 0; // blockwise encode, size check, and copy while (n > 0) { - unsigned char tmp[m_blocksize*10]; - size_t blk = std::min(n, m_blocksize); - size_t len = 0; + unsigned char tmp[m_blocksize * 10]; + size_t blk = std::min(n, m_blocksize); + size_t len = 0; // encode to temp buffer for (size_t i = 0; i < blk; ++i) - len += vlenc_u64(node_vec[i]->id(), tmp+len); + len += vlenc_u64(node_vec[i]->id(), tmp + len); // size check, copy to actual buffer - if (m_num_nodes+blk < 128 && m_imm_pos+m_imm_len+len <= m_buffer_len) { - ::save_memmove(m_buffer+m_imm_pos+len, m_buffer+m_imm_pos, m_imm_len); - memcpy(m_buffer+1, tmp, len); + if (m_num_nodes + blk < 128 && m_imm_pos + m_imm_len + len <= m_buffer_len) { + ::save_memmove(m_buffer + m_imm_pos + len, m_buffer + m_imm_pos, m_imm_len); + memcpy(m_buffer + 1, tmp, len); - m_imm_pos += len; + m_imm_pos += len; m_num_nodes += blk; m_buffer[0] += blk; } else { - skipped += blk; + skipped += blk; } m_needed_len += len; // advance - node_vec += blk; - n -= blk; + node_vec += blk; + n -= blk; } m_skipped += skipped; @@ -217,32 +208,31 @@ CompressedSnapshotRecord::append(size_t n, const Node* const node_vec[]) } /// \brief Append immediate entries -size_t -CompressedSnapshotRecord::append(size_t n, const cali_id_t attr_vec[], const Variant data_vec[]) +size_t CompressedSnapshotRecord::append(size_t n, const cali_id_t attr_vec[], const Variant data_vec[]) { size_t skipped = 0; // blockwise encode, size check, and copy while (n > 0) { - unsigned char tmp[m_blocksize*30]; // holds immediate entries (10 bytes per id + 20 bytes per variant) - size_t blk = std::min(n, m_blocksize); - size_t len = 0; + unsigned char tmp[m_blocksize * 30]; // holds immediate entries (10 bytes per id + 20 bytes per variant) + size_t blk = std::min(n, m_blocksize); + size_t len = 0; // encode to tmp buffer for (size_t i = 0; i < blk; ++i) { - len += vlenc_u64(attr_vec[i], tmp+len); - len += data_vec[i].pack(tmp+len); + len += vlenc_u64(attr_vec[i], tmp + len); + len += data_vec[i].pack(tmp + len); } // size check, copy to actual buffer - if (m_num_imm+blk < 128 && m_imm_pos+m_imm_len+len <= m_buffer_len) { - memcpy(m_buffer+m_imm_pos+m_imm_len, tmp, len); + if (m_num_imm + blk < 128 && m_imm_pos + m_imm_len + len <= m_buffer_len) { + memcpy(m_buffer + m_imm_pos + m_imm_len, tmp, len); m_imm_len += len; m_num_imm += blk; m_buffer[m_imm_pos] += blk; } else { - skipped += blk; + skipped += blk; } m_needed_len += len; @@ -250,7 +240,7 @@ CompressedSnapshotRecord::append(size_t n, const cali_id_t attr_vec[], const Var // advance attr_vec += blk; data_vec += blk; - n -= blk; + n -= blk; } m_skipped += skipped; @@ -259,14 +249,13 @@ CompressedSnapshotRecord::append(size_t n, const cali_id_t attr_vec[], const Var } /// \brief Append entry list -size_t -CompressedSnapshotRecord::append(size_t n, const Entry entrylist[]) +size_t CompressedSnapshotRecord::append(size_t n, const Entry entrylist[]) { size_t skipped = 0; const Node* nodes[m_blocksize]; - cali_id_t attr[m_blocksize]; - Variant data[m_blocksize]; + cali_id_t attr[m_blocksize]; + Variant data[m_blocksize]; size_t nn = 0; size_t ni = 0; diff --git a/src/common/CompressedSnapshotRecord.h b/src/common/CompressedSnapshotRecord.h index 55301db5b..4d448daa2 100644 --- a/src/common/CompressedSnapshotRecord.h +++ b/src/common/CompressedSnapshotRecord.h @@ -22,67 +22,56 @@ class CompressedSnapshotRecord; // // --- CompressedSnapshotRecordView // - + /// \brief A read-only decoder of a compressed snapshot record representation /// at a given memory location class CompressedSnapshotRecordView { const unsigned char* m_buffer; - size_t m_num_nodes; ///< Number of node entries - size_t m_num_imm; ///< Number of immediate entries + size_t m_num_nodes; ///< Number of node entries + size_t m_num_imm; ///< Number of immediate entries + + size_t m_imm_pos; ///< Start position of immediate entries + size_t m_imm_len; ///< Length of immediate entries - size_t m_imm_pos; ///< Start position of immediate entries - size_t m_imm_len; ///< Length of immediate entries + Entry unpack_next_entry(const CaliperMetadataAccessInterface* c, size_t& n, size_t& pos); - Entry unpack_next_entry(const CaliperMetadataAccessInterface* c, size_t& n, size_t& pos); - // CompressedSnapshotRecord can use this constructor to build an in-place view // without parsing the record representation first - CompressedSnapshotRecordView(const unsigned char* buffer, - size_t num_nodes, - size_t num_imm, - size_t imm_pos, - size_t imm_len) - : m_buffer(buffer), - m_num_nodes(num_nodes), - m_num_imm(num_imm), - m_imm_pos(imm_pos), - m_imm_len(imm_len) - { } - + CompressedSnapshotRecordView( + const unsigned char* buffer, + size_t num_nodes, + size_t num_imm, + size_t imm_pos, + size_t imm_len + ) + : m_buffer(buffer), m_num_nodes(num_nodes), m_num_imm(num_imm), m_imm_pos(imm_pos), m_imm_len(imm_len) + {} + public: - constexpr CompressedSnapshotRecordView() - : m_buffer(0), - m_num_nodes(0), - m_num_imm(0), - m_imm_pos(0), - m_imm_len(0) - { } - + constexpr CompressedSnapshotRecordView() : m_buffer(0), m_num_nodes(0), m_num_imm(0), m_imm_pos(0), m_imm_len(0) {} + CompressedSnapshotRecordView(const unsigned char* m_buffer, size_t* inc); - ~CompressedSnapshotRecordView() - { } - - size_t num_nodes() const { return m_num_nodes; } - size_t num_immediates() const { return m_num_imm; } + ~CompressedSnapshotRecordView() {} + + size_t num_nodes() const { return m_num_nodes; } + + size_t num_immediates() const { return m_num_imm; } /// \brief Unpack node entries - void - unpack_nodes(size_t n, cali_id_t node_vec[]) const; - + void unpack_nodes(size_t n, cali_id_t node_vec[]) const; + /// \brief Unpack immediate entries - void - unpack_immediate(size_t n, cali_id_t attr_vec[], Variant data_vec[]) const; - - std::vector - to_entrylist(const CaliperMetadataAccessInterface* c) const; - - template - void - unpack(const CaliperMetadataAccessInterface* c, EntryProcFn fn) { + void unpack_immediate(size_t n, cali_id_t attr_vec[], Variant data_vec[]) const; + + std::vector to_entrylist(const CaliperMetadataAccessInterface* c) const; + + template + void unpack(const CaliperMetadataAccessInterface* c, EntryProcFn fn) + { size_t n = 0; size_t pos = 1; @@ -94,7 +83,6 @@ class CompressedSnapshotRecordView friend class CompressedSnapshotRecord; }; - // // --- CompressedSnapshotRecord // @@ -103,18 +91,18 @@ class CompressedSnapshotRecordView class CompressedSnapshotRecord { static constexpr size_t m_internal_buffer_size = 512; - static constexpr size_t m_blocksize = 4; + static constexpr size_t m_blocksize = 4; unsigned char m_internal_buffer[m_internal_buffer_size]; unsigned char* m_buffer; size_t m_buffer_len; - size_t m_num_nodes; ///< Number of node entries - size_t m_num_imm; ///< Number of immediate entries + size_t m_num_nodes; ///< Number of node entries + size_t m_num_imm; ///< Number of immediate entries - size_t m_imm_pos; ///< Start position of immediate entries - size_t m_imm_len; ///< Length of immediate entries + size_t m_imm_pos; ///< Start position of immediate entries + size_t m_imm_len; ///< Length of immediate entries size_t m_needed_len; ///< Actually needed buffer space (may exceed given buffer len) size_t m_skipped; ///< Number of entries skipped @@ -123,42 +111,35 @@ class CompressedSnapshotRecord CompressedSnapshotRecord(size_t len, unsigned char* buf); CompressedSnapshotRecord(size_t n, const Entry entrylist[]); - + CompressedSnapshotRecord(); - + ~CompressedSnapshotRecord(); - size_t num_nodes() const { return m_num_nodes; } - size_t num_immediates() const { return m_num_imm; } + size_t num_nodes() const { return m_num_nodes; } + + size_t num_immediates() const { return m_num_imm; } const unsigned char* data() const { return m_buffer; } - size_t size() const { - return std::min(m_imm_pos + m_imm_len, m_buffer_len); - } + size_t size() const { return std::min(m_imm_pos + m_imm_len, m_buffer_len); } size_t needed_len() const { return m_needed_len; } + size_t num_skipped() const { return m_skipped; } /// \brief Append node entries - size_t - append(size_t n, const Node* const node_vec[]); - + size_t append(size_t n, const Node* const node_vec[]); + /// \brief Append immediate entries - size_t - append(size_t n, const cali_id_t attr_vec[], const Variant data_vec[]); - + size_t append(size_t n, const cali_id_t attr_vec[], const Variant data_vec[]); + /// \brief Append entry list - size_t - append(size_t n, const Entry entrylist[]); - - CompressedSnapshotRecordView - view() const { - return CompressedSnapshotRecordView(m_buffer, - m_num_nodes, - m_num_imm, - m_imm_pos, - m_imm_len); + size_t append(size_t n, const Entry entrylist[]); + + CompressedSnapshotRecordView view() const + { + return CompressedSnapshotRecordView(m_buffer, m_num_nodes, m_num_imm, m_imm_pos, m_imm_len); } }; diff --git a/src/common/Entry.cpp b/src/common/Entry.cpp index 5857d3782..e733b0091 100644 --- a/src/common/Entry.cpp +++ b/src/common/Entry.cpp @@ -12,8 +12,7 @@ using namespace cali; -int -Entry::count(cali_id_t attr_id) const +int Entry::count(cali_id_t attr_id) const { int res = 0; @@ -28,21 +27,19 @@ Entry::count(cali_id_t attr_id) const return res; } -Variant -Entry::value(cali_id_t attr_id) const +Variant Entry::value(cali_id_t attr_id) const { if (m_node && m_node->id() == attr_id) return m_value; for (const Node* node = m_node; node; node = node->parent()) if (node->attribute() == attr_id) - return node->data(); + return node->data(); return Variant(); } -Entry -Entry::get(const Attribute& attr) const +Entry Entry::get(const Attribute& attr) const { if (empty()) return Entry(); @@ -59,24 +56,22 @@ Entry::get(const Attribute& attr) const return Entry(); } -size_t -Entry::pack(unsigned char* buffer) const +size_t Entry::pack(unsigned char* buffer) const { size_t pos = 0; pos += vlenc_u64(m_node->id(), buffer); if (m_node->attribute() == Attribute::NAME_ATTR_ID) - pos += m_value.pack(buffer+pos); + pos += m_value.pack(buffer + pos); return pos; } -Entry -Entry::unpack(const CaliperMetadataAccessInterface& db, const unsigned char* buffer, size_t* inc) +Entry Entry::unpack(const CaliperMetadataAccessInterface& db, const unsigned char* buffer, size_t* inc) { size_t p = 0; - Entry ret { db.node(vldec_u64(buffer, &p)) }; + Entry ret { db.node(vldec_u64(buffer, &p)) }; if (ret.m_node->attribute() == Attribute::NAME_ATTR_ID) - ret.m_value = Variant::unpack(buffer+p, &p); + ret.m_value = Variant::unpack(buffer + p, &p); if (inc) *inc += p; diff --git a/src/common/Log.cpp b/src/common/Log.cpp index a5b4bed62..feaa6dfc9 100644 --- a/src/common/Log.cpp +++ b/src/common/Log.cpp @@ -14,33 +14,31 @@ using namespace cali; using namespace std; - -struct LogImpl -{ +struct LogImpl { // --- data static const char* s_prefix; static const ConfigSet::Entry s_configdata[]; - static LogImpl* s_instance; + static LogImpl* s_instance; enum class Stream { StdOut, StdErr, None, File }; - ConfigSet m_config; + ConfigSet m_config; Stream m_stream; std::ofstream m_ofstream; int m_verbosity; - std::string m_prefix; + std::string m_prefix; // --- helpers - void init_stream(const std::string& name) { - const map strmap { - { "none", Stream::None }, - { "stdout", Stream::StdOut }, - { "stderr", Stream::StdErr } }; + void init_stream(const std::string& name) + { + const map strmap { { "none", Stream::None }, + { "stdout", Stream::StdOut }, + { "stderr", Stream::StdErr } }; auto it = strmap.find(name); @@ -57,17 +55,16 @@ struct LogImpl // --- interface - LogImpl() - : m_prefix { s_prefix } + LogImpl() : m_prefix { s_prefix } { - ConfigSet config = - RuntimeConfig::get_default_config().init("log", s_configdata); + ConfigSet config = RuntimeConfig::get_default_config().init("log", s_configdata); m_verbosity = config.get("verbosity").to_int(); init_stream(config.get("logfile").to_string()); } - ostream& get_stream() { + ostream& get_stream() + { switch (m_stream) { case Stream::StdOut: return std::cout; @@ -79,42 +76,41 @@ struct LogImpl } }; -const char* LogImpl::s_prefix = "== CALIPER: "; +const char* LogImpl::s_prefix = "== CALIPER: "; const ConfigSet::Entry LogImpl::s_configdata[] = { // key, type, value, short description, long description - { "verbosity", CALI_TYPE_UINT, "0", + { "verbosity", + CALI_TYPE_UINT, + "0", "Verbosity level", "Verbosity level.\n" " 0: no output\n" " 1: basic informational runtime output\n" - " 2: debug output" - }, - { "logfile", CALI_TYPE_STRING, "stderr", + " 2: debug output" }, + { "logfile", + CALI_TYPE_STRING, + "stderr", "Log file name", "Log file name or output stream. Either one of\n" " stdout: Standard output stream,\n" " stderr: Standard error stream,\n" " none: No output,\n" - " or a log file name." - }, + " or a log file name." }, ConfigSet::Terminator }; LogImpl* LogImpl::s_instance = nullptr; - // // --- Log public interface // -ostream& -Log::get_stream() +ostream& Log::get_stream() { return LogImpl::s_instance->get_stream() << LogImpl::s_instance->m_prefix; } -ostream& -Log::perror(int errnum, const char* msg) +ostream& Log::perror(int errnum, const char* msg) { if (verbosity() < m_level) return m_nullstream; @@ -127,8 +123,7 @@ Log::perror(int errnum, const char* msg) #endif } -int -Log::verbosity() +int Log::verbosity() { if (LogImpl::s_instance == nullptr) return -1; @@ -136,33 +131,28 @@ Log::verbosity() return LogImpl::s_instance->m_verbosity; } -void -Log::set_verbosity(int v) +void Log::set_verbosity(int v) { LogImpl::s_instance->m_verbosity = v; } -void -Log::add_prefix(const std::string& prefix) +void Log::add_prefix(const std::string& prefix) { LogImpl::s_instance->m_prefix += prefix; } -void -Log::init() +void Log::init() { LogImpl::s_instance = new LogImpl; } -void -Log::fini() +void Log::fini() { delete LogImpl::s_instance; LogImpl::s_instance = nullptr; } -bool -Log::is_initialized() +bool Log::is_initialized() { return LogImpl::s_instance != nullptr; } diff --git a/src/common/NodeBuffer.cpp b/src/common/NodeBuffer.cpp index 88dfc9604..7cb2952dc 100644 --- a/src/common/NodeBuffer.cpp +++ b/src/common/NodeBuffer.cpp @@ -16,74 +16,73 @@ using namespace cali; namespace { - inline NodeBuffer::NodeInfo make_nodeinfo(const Node* node) { - cali_id_t parent_id = node->parent() ? node->parent()->id() : CALI_INV_ID; - - NodeBuffer::NodeInfo info = { - node->id(), - node->attribute(), - parent_id, - node->data() - }; - - return info; - } - - inline size_t max_packed_size(const NodeBuffer::NodeInfo& info) { - return 50 + info.value.size(); - } - - size_t pack_node(unsigned char* buf, const NodeBuffer::NodeInfo& info) { - bool have_parent = (info.parent_id != CALI_INV_ID); - size_t pos = 0; - - // store id & flag in first uint: flag indicates whether parent ID is needed - uint64_t u = 2 * info.node_id + (have_parent ? 1 : 0); - - pos += vlenc_u64(u, buf+pos); - pos += vlenc_u64(info.attr_id, buf+pos); - - if (have_parent) - pos += vlenc_u64(info.parent_id, buf+pos); - - // encode type and size and copy data directly for now - pos += vlenc_u64(static_cast(info.value.type()), buf+pos); - pos += vlenc_u64(info.value.size(), buf+pos); - memcpy(buf+pos, info.value.data(), info.value.size()); - - pos += info.value.size(); - - return pos; - } - - NodeBuffer::NodeInfo unpack_node(const unsigned char* buf, size_t* inc) { - NodeBuffer::NodeInfo ret = { CALI_INV_ID, CALI_INV_ID, CALI_INV_ID, Variant() }; - size_t pos = 0; - - uint64_t u = vldec_u64(buf+pos, &pos); - bool have_parent = ((u % 2) == 1); - - ret.node_id = u / 2; - ret.attr_id = vldec_u64(buf+pos, &pos); - - if (have_parent) - ret.parent_id = vldec_u64(buf+pos, &pos); - - u = vldec_u64(buf+pos, &pos); - size_t size = vldec_u64(buf+pos, &pos); - cali_attr_type type = (u <= CALI_MAXTYPE ? static_cast(u) : CALI_TYPE_INV); - - ret.value = Variant(type, buf+pos, size); - - pos += size; - *inc += pos; - - return ret; - } +inline NodeBuffer::NodeInfo make_nodeinfo(const Node* node) +{ + cali_id_t parent_id = node->parent() ? node->parent()->id() : CALI_INV_ID; + + NodeBuffer::NodeInfo info = { node->id(), node->attribute(), parent_id, node->data() }; + + return info; +} + +inline size_t max_packed_size(const NodeBuffer::NodeInfo& info) +{ + return 50 + info.value.size(); +} + +size_t pack_node(unsigned char* buf, const NodeBuffer::NodeInfo& info) +{ + bool have_parent = (info.parent_id != CALI_INV_ID); + size_t pos = 0; + + // store id & flag in first uint: flag indicates whether parent ID is needed + uint64_t u = 2 * info.node_id + (have_parent ? 1 : 0); + + pos += vlenc_u64(u, buf + pos); + pos += vlenc_u64(info.attr_id, buf + pos); + + if (have_parent) + pos += vlenc_u64(info.parent_id, buf + pos); + + // encode type and size and copy data directly for now + pos += vlenc_u64(static_cast(info.value.type()), buf + pos); + pos += vlenc_u64(info.value.size(), buf + pos); + memcpy(buf + pos, info.value.data(), info.value.size()); + + pos += info.value.size(); + + return pos; +} + +NodeBuffer::NodeInfo unpack_node(const unsigned char* buf, size_t* inc) +{ + NodeBuffer::NodeInfo ret = { CALI_INV_ID, CALI_INV_ID, CALI_INV_ID, Variant() }; + size_t pos = 0; + + uint64_t u = vldec_u64(buf + pos, &pos); + bool have_parent = ((u % 2) == 1); + + ret.node_id = u / 2; + ret.attr_id = vldec_u64(buf + pos, &pos); + + if (have_parent) + ret.parent_id = vldec_u64(buf + pos, &pos); + + u = vldec_u64(buf + pos, &pos); + size_t size = vldec_u64(buf + pos, &pos); + cali_attr_type type = (u <= CALI_MAXTYPE ? static_cast(u) : CALI_TYPE_INV); + + ret.value = Variant(type, buf + pos, size); + + pos += size; + *inc += pos; + + return ret; } +} // namespace -unsigned char* -NodeBuffer::reserve(size_t min) { +unsigned char* NodeBuffer::reserve(size_t min) +{ if (min <= m_reserved_len) return m_buffer; @@ -97,58 +96,46 @@ NodeBuffer::reserve(size_t min) { return m_buffer; } -NodeBuffer::NodeBuffer() - : m_count(0), - m_pos(0), - m_reserved_len(0), - m_buffer(0) -{ } - -NodeBuffer::NodeBuffer(size_t size) - : m_count(0), - m_pos(0), - m_reserved_len(size), - m_buffer(new unsigned char[size]) -{ } - -NodeBuffer::~NodeBuffer() { +NodeBuffer::NodeBuffer() : m_count(0), m_pos(0), m_reserved_len(0), m_buffer(0) +{} + +NodeBuffer::NodeBuffer(size_t size) : m_count(0), m_pos(0), m_reserved_len(size), m_buffer(new unsigned char[size]) +{} + +NodeBuffer::~NodeBuffer() +{ delete[] m_buffer; } -void -NodeBuffer::append(const NodeInfo& info) +void NodeBuffer::append(const NodeInfo& info) { reserve(m_pos + max_packed_size(info)); m_pos += ::pack_node(m_buffer + m_pos, info); - ++m_count; + ++m_count; } -void -NodeBuffer::append(const Node* node) +void NodeBuffer::append(const Node* node) { append(::make_nodeinfo(node)); } - /// \brief Expose buffer for read from external source (e.g., MPI), /// and set count and size. -unsigned char* -NodeBuffer::import(size_t size, size_t count) +unsigned char* NodeBuffer::import(size_t size, size_t count) { reserve(size); - + m_pos = size; m_count = count; - + return m_buffer; } -void -NodeBuffer::for_each(const std::function fn) const +void NodeBuffer::for_each(const std::function fn) const { size_t pos = 0; - + for (size_t i = 0; i < m_count && pos < m_pos; ++i) fn(::unpack_node(m_buffer + pos, &pos)); } diff --git a/src/common/NodeBuffer.h b/src/common/NodeBuffer.h index eef6198f2..4085f9fca 100644 --- a/src/common/NodeBuffer.h +++ b/src/common/NodeBuffer.h @@ -2,7 +2,7 @@ // See top-level LICENSE file for details. /// \file NodeBuffer.h -/// \brief NodeBuffer class +/// \brief NodeBuffer class #pragma once @@ -14,19 +14,19 @@ namespace cali { class Node; - + /// \brief Serialize/deserialize a set of nodes class NodeBuffer { - size_t m_count; - - size_t m_pos; - size_t m_reserved_len; - + size_t m_count; + + size_t m_pos; + size_t m_reserved_len; + unsigned char* m_buffer; unsigned char* reserve(size_t min); - + public: struct NodeInfo { @@ -34,29 +34,30 @@ class NodeBuffer cali_id_t attr_id; cali_id_t parent_id; Variant value; - }; + }; NodeBuffer(); NodeBuffer(size_t size); - + ~NodeBuffer(); - - NodeBuffer(const NodeBuffer&) = delete; - NodeBuffer& operator = (const NodeBuffer&) = delete; + + NodeBuffer(const NodeBuffer&) = delete; + NodeBuffer& operator= (const NodeBuffer&) = delete; void append(const NodeInfo& info); void append(const Node* node); size_t count() const { return m_count; } - size_t size() const { return m_pos; } - + + size_t size() const { return m_pos; } + const unsigned char* data() const { return m_buffer; } /// \brief Expose buffer for read from external source (e.g., MPI), /// and set count and size. unsigned char* import(size_t size, size_t count); - /// \brief Run function \a fn on each element + /// \brief Run function \a fn on each element void for_each(std::function fn) const; }; diff --git a/src/common/OutputStream.cpp b/src/common/OutputStream.cpp index c2252ceda..332d8c44c 100644 --- a/src/common/OutputStream.cpp +++ b/src/common/OutputStream.cpp @@ -81,31 +81,30 @@ bool check_and_create_directory(const std::string& filepath) } #endif -} +} // namespace -struct OutputStream::OutputStreamImpl -{ - StreamType type; - Mode mode; +struct OutputStream::OutputStreamImpl { + StreamType type; + Mode mode; - bool is_initialized; - std::mutex init_mutex; + bool is_initialized; + std::mutex init_mutex; #if defined(_WIN32) || (__cplusplus >= 201703L) - std::filesystem::path filename; + std::filesystem::path filename; #else - std::string filename; + std::string filename; #endif std::ofstream fs; std::ostream* user_os; - void init() { + void init() + { if (is_initialized) return; - std::lock_guard - g(init_mutex); + std::lock_guard g(init_mutex); is_initialized = true; @@ -121,7 +120,8 @@ struct OutputStream::OutputStreamImpl } } - std::ostream* stream() { + std::ostream* stream() + { init(); switch (type) { @@ -140,64 +140,57 @@ struct OutputStream::OutputStreamImpl return &fs; } - void reset() { + void reset() + { fs.close(); filename.clear(); - user_os = nullptr; - type = StreamType::None; + user_os = nullptr; + type = StreamType::None; is_initialized = false; } - OutputStreamImpl() - : type(StreamType::None), mode(Truncate), is_initialized(false), user_os(nullptr) - { } + OutputStreamImpl() : type(StreamType::None), mode(Truncate), is_initialized(false), user_os(nullptr) {} OutputStreamImpl(const char* name) : type(StreamType::None), mode(Truncate), is_initialized(false), filename(name), user_os(nullptr) - { } + {} }; -OutputStream::OutputStream() - : mP(new OutputStreamImpl) -{ } +OutputStream::OutputStream() : mP(new OutputStreamImpl) +{} OutputStream::~OutputStream() { mP.reset(); } -OutputStream::operator bool() const +OutputStream::operator bool () const { return mP->is_initialized; } -OutputStream::StreamType -OutputStream::type() const +OutputStream::StreamType OutputStream::type() const { return mP->type; } -std::ostream* -OutputStream::stream() +std::ostream* OutputStream::stream() { return mP->stream(); } -void -OutputStream::set_mode(OutputStream::Mode mode) +void OutputStream::set_mode(OutputStream::Mode mode) { mP->mode = mode; } -void -OutputStream::set_stream(StreamType type) +void OutputStream::set_stream(StreamType type) { mP->reset(); mP->type = type; } -void -OutputStream::set_stream(std::ostream* os) +void OutputStream::set_stream(std::ostream* os) { mP->reset(); @@ -205,8 +198,7 @@ OutputStream::set_stream(std::ostream* os) mP->user_os = os; } -void -OutputStream::set_filename(const char* filename) +void OutputStream::set_filename(const char* filename) { mP->reset(); @@ -214,12 +206,15 @@ OutputStream::set_filename(const char* filename) mP->type = StreamType::File; } -void -OutputStream::set_filename(const char* formatstr, const CaliperMetadataAccessInterface& db, const std::vector& rec) +void OutputStream::set_filename( + const char* formatstr, + const CaliperMetadataAccessInterface& db, + const std::vector& rec +) { mP->reset(); - if (strcmp(formatstr, "stdout") == 0) + if (strcmp(formatstr, "stdout") == 0) mP->type = StreamType::StdOut; else if (strcmp(formatstr, "stderr") == 0) mP->type = StreamType::StdErr; diff --git a/src/common/RuntimeConfig.cpp b/src/common/RuntimeConfig.cpp index 4a8516c83..45ad267e7 100644 --- a/src/common/RuntimeConfig.cpp +++ b/src/common/RuntimeConfig.cpp @@ -23,53 +23,58 @@ using namespace std; namespace { - const char* builtin_profiles = - "# [serial-trace]\n" - "CALI_SERVICES_ENABLE=event,recorder,timestamp,trace\n" - "CALI_TIMER_INCLUSIVE_DURATION=true\n" - "# [event-trace]\n" - "CALI_SERVICES_ENABLE=event,recorder,timestamp,trace\n" - "# [flat-function-profile]\n" - "CALI_SERVICES_ENABLE=aggregate,event,report,timestamp\n" - "CALI_TIMER_INCLUSIVE_DURATION=true\n" - "CALI_AGGREGATE_KEY=event.end#function\n" - "CALI_REPORT_CONFIG=\"select event.end#function,sum#time.inclusive.duration where event.end#function format table order by time.inclusive.duration desc\"\n" - "# [runtime-report]\n" - "CALI_SERVICES_ENABLE=aggregate,event,report,timestamp\n" - "CALI_EVENT_ENABLE_SNAPSHOT_INFO=false\n" - "CALI_TIMER_SNAPSHOT_DURATION=true\n" - "CALI_TIMER_INCLUSIVE_DURATION=false\n" - "CALI_TIMER_UNIT=sec\n" - "CALI_REPORT_CONFIG=\"select inclusive_sum(sum#time.duration) as \\\"Inclusive time\\\",sum(sum#time.duration) as \\\"Exclusive time\\\",percent_total(sum#time.duration) as \\\"Time %\\\" group by prop:nested format tree\"\n" - "CALI_REPORT_FILENAME=stderr\n" - "# [mpi-runtime-report]\n" - "CALI_SERVICES_ENABLE=aggregate,event,mpi,mpireport,timestamp\n" - "CALI_MPI_BLACKLIST=MPI_Comm_rank,MPI_Comm_size,MPI_Wtick,MPI_Wtime\n" - "CALI_EVENT_ENABLE_SNAPSHOT_INFO=false\n" - "CALI_TIMER_UNIT=sec\n" - "CALI_MPIREPORT_CONFIG=\"select min(sum#time.duration) as \\\"Min time/rank\\\",max(sum#time.duration) as \\\"Max time/rank\\\", avg(sum#time.duration) as \\\"Avg time/rank\\\", percent_total(sum#time.duration) as \\\"Time % (total)\\\" group by prop:nested format tree\"\n" - "CALI_MPIREPORT_FILENAME=stderr\n" - "# [thread-trace]\n" - "CALI_SERVICES_ENABLE=event:pthread:recorder:timestamp:trace\n" - "# [mpi-msg-trace]\n" - "CALI_SERVICES_ENABLE=event,mpi,recorder,timestamp,trace\n" - "CALI_MPI_BLACKLIST=MPI_Comm_rank,MPI_Comm_size,MPI_Wtick,MPI_Wtime\n" - "CALI_MPI_MSG_TRACING=true\n" - "CALI_RECORDER_FILENAME=%mpi.rank%.cali\n"; - - string config_var_name(const string& name, const string& key) { - // make uppercase PREFIX_NAMESPACE_KEY string - - string str = string("CALI_") + name + string("_") + key; - - transform(str.begin(), str.end(), str.begin(), ::toupper); - - return str; - } +const char* builtin_profiles = + "# [serial-trace]\n" + "CALI_SERVICES_ENABLE=event,recorder,timestamp,trace\n" + "CALI_TIMER_INCLUSIVE_DURATION=true\n" + "# [event-trace]\n" + "CALI_SERVICES_ENABLE=event,recorder,timestamp,trace\n" + "# [flat-function-profile]\n" + "CALI_SERVICES_ENABLE=aggregate,event,report,timestamp\n" + "CALI_TIMER_INCLUSIVE_DURATION=true\n" + "CALI_AGGREGATE_KEY=event.end#function\n" + "CALI_REPORT_CONFIG=\"select event.end#function,sum#time.inclusive.duration where event.end#function format table " + "order by time.inclusive.duration desc\"\n" + "# [runtime-report]\n" + "CALI_SERVICES_ENABLE=aggregate,event,report,timestamp\n" + "CALI_EVENT_ENABLE_SNAPSHOT_INFO=false\n" + "CALI_TIMER_SNAPSHOT_DURATION=true\n" + "CALI_TIMER_INCLUSIVE_DURATION=false\n" + "CALI_TIMER_UNIT=sec\n" + "CALI_REPORT_CONFIG=\"select inclusive_sum(sum#time.duration) as \\\"Inclusive time\\\",sum(sum#time.duration) as " + "\\\"Exclusive time\\\",percent_total(sum#time.duration) as \\\"Time %\\\" group by prop:nested format tree\"\n" + "CALI_REPORT_FILENAME=stderr\n" + "# [mpi-runtime-report]\n" + "CALI_SERVICES_ENABLE=aggregate,event,mpi,mpireport,timestamp\n" + "CALI_MPI_BLACKLIST=MPI_Comm_rank,MPI_Comm_size,MPI_Wtick,MPI_Wtime\n" + "CALI_EVENT_ENABLE_SNAPSHOT_INFO=false\n" + "CALI_TIMER_UNIT=sec\n" + "CALI_MPIREPORT_CONFIG=\"select min(sum#time.duration) as \\\"Min time/rank\\\",max(sum#time.duration) as \\\"Max " + "time/rank\\\", avg(sum#time.duration) as \\\"Avg time/rank\\\", percent_total(sum#time.duration) as \\\"Time % " + "(total)\\\" group by prop:nested format tree\"\n" + "CALI_MPIREPORT_FILENAME=stderr\n" + "# [thread-trace]\n" + "CALI_SERVICES_ENABLE=event:pthread:recorder:timestamp:trace\n" + "# [mpi-msg-trace]\n" + "CALI_SERVICES_ENABLE=event,mpi,recorder,timestamp,trace\n" + "CALI_MPI_BLACKLIST=MPI_Comm_rank,MPI_Comm_size,MPI_Wtick,MPI_Wtime\n" + "CALI_MPI_MSG_TRACING=true\n" + "CALI_RECORDER_FILENAME=%mpi.rank%.cali\n"; + +string config_var_name(const string& name, const string& key) +{ + // make uppercase PREFIX_NAMESPACE_KEY string + + string str = string("CALI_") + name + string("_") + key; - typedef map< string, string > config_profile_t; + transform(str.begin(), str.end(), str.begin(), ::toupper); + + return str; } +typedef map config_profile_t; +} // namespace + namespace cali { @@ -77,24 +82,30 @@ namespace cali // --- ConfigSet implementation // -struct ConfigSetImpl -{ +struct ConfigSetImpl { // --- data std::unordered_map m_dict; // --- interface - StringConverter get(const char* key) const { + StringConverter get(const char* key) const + { auto it = m_dict.find(key); return (it == m_dict.end() ? StringConverter() : it->second); } - void init(const char* name, const RuntimeConfig::config_entry_list_t& list, bool read_env, const ::config_profile_t& profile, const ::config_profile_t& top_profile) + void init( + const char* name, + const RuntimeConfig::config_entry_list_t& list, + bool read_env, + const ::config_profile_t& profile, + const ::config_profile_t& top_profile + ) { - for (const auto &e : list) { + for (const auto& e : list) { std::string varname { ::config_var_name(name, e.first) }; - std::string value { e.second }; + std::string value { e.second }; // See if there is an entry in the top config profile auto topit = top_profile.find(varname); @@ -120,34 +131,33 @@ struct ConfigSetImpl } }; - // // --- RuntimeConfig implementation // -struct RuntimeConfig::RuntimeConfigImpl -{ +struct RuntimeConfig::RuntimeConfigImpl { // --- data - bool m_allow_read_env = true; + bool m_allow_read_env = true; // combined profile: initially receives settings made through "add" API, // then merges all selected profiles in here - ::config_profile_t m_combined_profile; + ::config_profile_t m_combined_profile; // top-priority profile: receives all settings made through "set" API // that overwrite other settings - ::config_profile_t m_top_profile; + ::config_profile_t m_top_profile; // the DB of initialized config sets - map< string, shared_ptr > m_database; + map> m_database; // the config profile DB - map< string, ::config_profile_t> m_config_profiles; + map m_config_profiles; // --- helpers - void read_config_profiles(istream& in) { + void read_config_profiles(istream& in) + { // // Parse config file line-by-line // * '#' as the first character is a comment, or start of a new @@ -158,7 +168,7 @@ struct RuntimeConfig::RuntimeConfigImpl ::config_profile_t current_profile; string current_profile_name { "default" }; - for (string line; std::getline(in, line); ) { + for (string line; std::getline(in, line);) { if (line.length() < 1) continue; @@ -167,12 +177,12 @@ struct RuntimeConfig::RuntimeConfigImpl string::size_type b = line.find_first_of('['); string::size_type e = line.find_first_of(']'); - if (b != string::npos && e != string::npos && b+1 < e) { + if (b != string::npos && e != string::npos && b + 1 < e) { if (current_profile.size() > 0) m_config_profiles[current_profile_name].insert(current_profile.begin(), current_profile.end()); current_profile.clear(); - current_profile_name = line.substr(b+1, e-b-1); + current_profile_name = line.substr(b + 1, e - b - 1); } else { continue; } @@ -181,7 +191,7 @@ struct RuntimeConfig::RuntimeConfigImpl string::size_type s = line.find_first_of('='); if (s > 0 && s < line.size()) { - std::istringstream is(line.substr(s+1)); + std::istringstream is(line.substr(s + 1)); current_profile[line.substr(0, s)] = util::read_word(is, ""); } } @@ -190,14 +200,14 @@ struct RuntimeConfig::RuntimeConfigImpl m_config_profiles[current_profile_name] = current_profile; } - void read_config_files(const std::vector& filenames) { + void read_config_files(const std::vector& filenames) + { // read builtin profiles istringstream is(::builtin_profiles); read_config_profiles(is); - - for (const auto &s : filenames) { + for (const auto& s : filenames) { ifstream fs(s.c_str()); if (fs) @@ -205,11 +215,9 @@ struct RuntimeConfig::RuntimeConfigImpl } } - void init_config_database() { - const config_entry_list_t configdata { - { "profile", "default" }, - { "file", "caliper.config" } - }; + void init_config_database() + { + const config_entry_list_t configdata { { "profile", "default" }, { "file", "caliper.config" } }; // read pre-init config set to get config file name from env var ConfigSetImpl init_config_cfg; @@ -220,7 +228,7 @@ struct RuntimeConfig::RuntimeConfigImpl // merge "default" profile into combined profile { - for ( auto &p : m_config_profiles["default"] ) + for (auto& p : m_config_profiles["default"]) m_combined_profile[p.first] = p.second; } @@ -231,8 +239,7 @@ struct RuntimeConfig::RuntimeConfigImpl m_database.insert(make_pair("config", config_cfg)); // get the selected config profile names - vector profile_names = - config_cfg->get("profile").to_stringlist(); + vector profile_names = config_cfg->get("profile").to_stringlist(); // merge all selected profiles for (const std::string& profile_name : profile_names) { @@ -243,14 +250,15 @@ struct RuntimeConfig::RuntimeConfigImpl continue; } - for (auto &p : it->second) + for (auto& p : it->second) m_combined_profile[p.first] = p.second; } } // --- interface - StringConverter get(const char* set, const char* key) { + StringConverter get(const char* set, const char* key) + { if (m_database.empty()) init_config_database(); @@ -258,20 +266,18 @@ struct RuntimeConfig::RuntimeConfigImpl return (it == m_database.end() ? StringConverter() : StringConverter(it->second->get(key))); } - void preset(const char* key, const std::string& value) { - m_combined_profile[key] = value; - } + void preset(const char* key, const std::string& value) { m_combined_profile[key] = value; } - void set(const char* key, const std::string& value) { - m_top_profile[key] = value; - } + void set(const char* key, const std::string& value) { m_top_profile[key] = value; } - void import(const std::map& values) { + void import(const std::map& values) + { for (auto& p : values) m_top_profile[p.first] = p.second; } - shared_ptr init_configset(const char* name, const config_entry_list_t& list) { + shared_ptr init_configset(const char* name, const config_entry_list_t& list) + { if (m_database.empty()) init_config_database(); @@ -288,27 +294,24 @@ struct RuntimeConfig::RuntimeConfigImpl return ret; } - void print(std::ostream& os) const { - for ( auto set : m_database ) - for ( auto entry : set.second->m_dict ) - os << ::config_var_name(set.first, entry.first) - << '=' << entry.second.to_string() << std::endl; + void print(std::ostream& os) const + { + for (auto set : m_database) + for (auto entry : set.second->m_dict) + os << ::config_var_name(set.first, entry.first) << '=' << entry.second.to_string() << std::endl; } }; } // namespace cali - // // --- ConfigSet public interface // -ConfigSet::ConfigSet(const shared_ptr& p) - : mP { p } -{ } +ConfigSet::ConfigSet(const shared_ptr& p) : mP { p } +{} -StringConverter -ConfigSet::get(const char* key) const +StringConverter ConfigSet::get(const char* key) const { if (!mP) return StringConverter(); @@ -316,70 +319,59 @@ ConfigSet::get(const char* key) const return mP->get(key); } - // // --- RuntimeConfig public interface // -RuntimeConfig::RuntimeConfig() - : mP(new RuntimeConfigImpl) -{ } +RuntimeConfig::RuntimeConfig() : mP(new RuntimeConfigImpl) +{} -StringConverter -RuntimeConfig::get(const char* set, const char* key) +StringConverter RuntimeConfig::get(const char* set, const char* key) { return mP->get(set, key); } -ConfigSet -RuntimeConfig::init(const char* name, const ConfigSet::Entry* list) +ConfigSet RuntimeConfig::init(const char* name, const ConfigSet::Entry* list) { config_entry_list_t converted_list; for (const ConfigSet::Entry* e = list; e && e->key; ++e) - converted_list.emplace_back( std::make_pair(e->key, e->value) ); + converted_list.emplace_back(std::make_pair(e->key, e->value)); return ConfigSet(mP->init_configset(name, converted_list)); } -ConfigSet -RuntimeConfig::init(const char* name, const config_entry_list_t& list) +ConfigSet RuntimeConfig::init(const char* name, const config_entry_list_t& list) { return ConfigSet(mP->init_configset(name, list)); } -void -RuntimeConfig::preset(const char* key, const std::string& value) +void RuntimeConfig::preset(const char* key, const std::string& value) { mP->preset(key, value); } -void -RuntimeConfig::set(const char* key, const std::string& value) +void RuntimeConfig::set(const char* key, const std::string& value) { mP->set(key, value); } -void -RuntimeConfig::import(const std::map& values) +void RuntimeConfig::import(const std::map& values) { mP->import(values); } -void -RuntimeConfig::print(ostream& os) +void RuntimeConfig::print(ostream& os) { mP->print(os); } -bool -RuntimeConfig::allow_read_env() +bool RuntimeConfig::allow_read_env() { return mP->m_allow_read_env; } -bool -RuntimeConfig::allow_read_env(bool allow) +bool RuntimeConfig::allow_read_env(bool allow) { mP->m_allow_read_env = allow; return mP->m_allow_read_env; @@ -389,8 +381,7 @@ RuntimeConfig::allow_read_env(bool allow) // static interface // -RuntimeConfig -RuntimeConfig::get_default_config() +RuntimeConfig RuntimeConfig::get_default_config() { static RuntimeConfig s_default_config; diff --git a/src/common/SnapshotBuffer.cpp b/src/common/SnapshotBuffer.cpp index 37afe2af4..feda7545a 100644 --- a/src/common/SnapshotBuffer.cpp +++ b/src/common/SnapshotBuffer.cpp @@ -12,8 +12,8 @@ using namespace cali; -unsigned char* -SnapshotBuffer::reserve(size_t min) { +unsigned char* SnapshotBuffer::reserve(size_t min) +{ if (min <= m_reserved_len) return m_buffer; @@ -27,52 +27,42 @@ SnapshotBuffer::reserve(size_t min) { return m_buffer; } -SnapshotBuffer::SnapshotBuffer() - : m_count(0), - m_pos(0), - m_reserved_len(0), - m_buffer(0) -{ } +SnapshotBuffer::SnapshotBuffer() : m_count(0), m_pos(0), m_reserved_len(0), m_buffer(0) +{} SnapshotBuffer::SnapshotBuffer(size_t size) - : m_count(0), - m_pos(0), - m_reserved_len(size), - m_buffer(new unsigned char[size]) -{ } + : m_count(0), m_pos(0), m_reserved_len(size), m_buffer(new unsigned char[size]) +{} -SnapshotBuffer::~SnapshotBuffer() { +SnapshotBuffer::~SnapshotBuffer() +{ delete[] m_buffer; } -void -SnapshotBuffer::append(const CompressedSnapshotRecord& rec) +void SnapshotBuffer::append(const CompressedSnapshotRecord& rec) { memcpy(reserve(m_pos + rec.size()) + m_pos, rec.data(), rec.size()); - + m_pos += rec.size(); - ++m_count; + ++m_count; } - /// \brief Expose buffer for read from external source (e.g., MPI), /// and set count and size. -unsigned char* -SnapshotBuffer::import(size_t size, size_t count) +unsigned char* SnapshotBuffer::import(size_t size, size_t count) { reserve(size); - + m_pos = size; m_count = count; - + return m_buffer; } -void -SnapshotBuffer::for_each(const std::function fn) const +void SnapshotBuffer::for_each(const std::function fn) const { size_t pos = 0; - + for (size_t i = 0; i < m_count && pos < m_pos; ++i) fn(CompressedSnapshotRecordView(m_buffer + pos, &pos)); } diff --git a/src/common/SnapshotBuffer.h b/src/common/SnapshotBuffer.h index eab555b59..9bf0464aa 100644 --- a/src/common/SnapshotBuffer.h +++ b/src/common/SnapshotBuffer.h @@ -2,7 +2,7 @@ // See top-level LICENSE file for details. /// \file SnapshotBuffer.h -/// \brief SnapshotBuffer class +/// \brief SnapshotBuffer class #pragma once @@ -13,41 +13,42 @@ namespace cali class CompressedSnapshotRecord; class CompressedSnapshotRecordView; - + /// \brief Serialize/deserialize a set of nodes class SnapshotBuffer { - std::size_t m_count; - - std::size_t m_pos; - std::size_t m_reserved_len; - + std::size_t m_count; + + std::size_t m_pos; + std::size_t m_reserved_len; + unsigned char* m_buffer; unsigned char* reserve(std::size_t min); - + public: SnapshotBuffer(); SnapshotBuffer(std::size_t size); - + ~SnapshotBuffer(); - - SnapshotBuffer(const SnapshotBuffer&) = delete; - SnapshotBuffer& operator = (const SnapshotBuffer&) = delete; + + SnapshotBuffer(const SnapshotBuffer&) = delete; + SnapshotBuffer& operator= (const SnapshotBuffer&) = delete; void append(const CompressedSnapshotRecord& rec); std::size_t count() const { return m_count; } - std::size_t size() const { return m_pos; } - + + std::size_t size() const { return m_pos; } + const unsigned char* data() const { return m_buffer; } /// \brief Expose buffer for read from external source (e.g., MPI), /// and set count and size. unsigned char* import(std::size_t size, std::size_t count); - /// \brief Run function \a fn on each snapshot + /// \brief Run function \a fn on each snapshot void for_each(std::function fn) const; }; diff --git a/src/common/SnapshotTextFormatter.cpp b/src/common/SnapshotTextFormatter.cpp index a4a439567..2c9669912 100644 --- a/src/common/SnapshotTextFormatter.cpp +++ b/src/common/SnapshotTextFormatter.cpp @@ -18,24 +18,22 @@ using namespace cali; - -struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl -{ +struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl { struct Field { std::string prefix; std::string attr_name; Attribute attr; - int width; - char align; // 'l', 'r', 'c' + int width; + char align; // 'l', 'r', 'c' }; - std::vector m_fields; + std::vector m_fields; std::mutex m_field_mutex; - void - parse(const std::string& formatstring) { + void parse(const std::string& formatstring) + { m_fields.clear(); // parse format: "(prefix string) %[]attr_name% ... " @@ -56,29 +54,29 @@ struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl std::vector field_strings; util::tokenize(split_string.front(), "[]", std::back_inserter(field_strings)); - + // look for width/alignment specification (in [] brackets) int wfbegin = -1; int wfend = -1; int apos = -1; - int nfields = field_strings.size(); + int nfields = field_strings.size(); for (int i = 0; i < nfields; ++i) if (field_strings[i] == "[") wfbegin = i; else if (field_strings[i] == "]") - wfend = i; + wfend = i; - if (wfbegin >= 0 && wfend > wfbegin+1) { + if (wfbegin >= 0 && wfend > wfbegin + 1) { // we have a width specification field - field.width = std::stoi(field_strings[wfbegin+1]); + field.width = std::stoi(field_strings[wfbegin + 1]); if (wfbegin > 0) apos = 0; - else if (wfend+1 < nfields) - apos = wfend+1; + else if (wfend + 1 < nfields) + apos = wfend + 1; } else if (nfields > 0) apos = 0; @@ -92,13 +90,12 @@ struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl } } - std::ostream& - print(std::ostream& os, const CaliperMetadataAccessInterface& db, const std::vector& list) { + std::ostream& print(std::ostream& os, const CaliperMetadataAccessInterface& db, const std::vector& list) + { std::vector fields; { - std::lock_guard - g(m_field_mutex); + std::lock_guard g(m_field_mutex); fields.assign(m_fields.begin(), m_fields.end()); } @@ -112,11 +109,11 @@ struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl cali_attr_type type = f.attr.type(); - f.align = (type == CALI_TYPE_DOUBLE || - type == CALI_TYPE_INT || - type == CALI_TYPE_UINT || - type == CALI_TYPE_ADDR) ? 'r' : 'l'; - + f.align = (type == CALI_TYPE_DOUBLE || type == CALI_TYPE_INT || type == CALI_TYPE_UINT + || type == CALI_TYPE_ADDR) + ? 'r' + : 'l'; + update = true; } @@ -140,7 +137,7 @@ struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl } } - static const char whitespace[80+1] = + static const char whitespace[80 + 1] = " " " "; @@ -148,14 +145,13 @@ struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl int w = len < f.width ? std::min(f.width - len, 80) : 0; if (f.align == 'r') - os << f.prefix << (w > 0 ? whitespace+(80-w) : "") << str; + os << f.prefix << (w > 0 ? whitespace + (80 - w) : "") << str; else - os << f.prefix << str << (w > 0 ? whitespace+(80-w) : ""); + os << f.prefix << str << (w > 0 ? whitespace + (80 - w) : ""); } if (update) { - std::lock_guard - g(m_field_mutex); + std::lock_guard g(m_field_mutex); m_fields.swap(fields); } @@ -164,25 +160,26 @@ struct cali::SnapshotTextFormatter::SnapshotTextFormatterImpl } }; -SnapshotTextFormatter::SnapshotTextFormatter(const std::string& format_str) - : mP(new SnapshotTextFormatterImpl) -{ +SnapshotTextFormatter::SnapshotTextFormatter(const std::string& format_str) : mP(new SnapshotTextFormatterImpl) +{ mP->parse(format_str); } SnapshotTextFormatter::~SnapshotTextFormatter() { mP.reset(); -} +} -void -SnapshotTextFormatter::reset(const std::string& format_str) +void SnapshotTextFormatter::reset(const std::string& format_str) { mP->parse(format_str); } -std::ostream& -SnapshotTextFormatter::print(std::ostream& os, const CaliperMetadataAccessInterface& db, const std::vector& list) +std::ostream& SnapshotTextFormatter::print( + std::ostream& os, + const CaliperMetadataAccessInterface& db, + const std::vector& list +) { return mP->print(os, db, list); } diff --git a/src/common/SnapshotTextFormatter.h b/src/common/SnapshotTextFormatter.h index 84ebd1fe0..35d5be6fe 100644 --- a/src/common/SnapshotTextFormatter.h +++ b/src/common/SnapshotTextFormatter.h @@ -29,13 +29,11 @@ class SnapshotTextFormatter ~SnapshotTextFormatter(); - void - reset(const std::string& format_str); + void reset(const std::string& format_str); - std::ostream& - print(std::ostream&, const CaliperMetadataAccessInterface&, const std::vector&); + std::ostream& print(std::ostream&, const CaliperMetadataAccessInterface&, const std::vector&); }; -} +} // namespace cali #endif diff --git a/src/common/StringConverter.cpp b/src/common/StringConverter.cpp index ec4a02ae5..8c8b99d76 100644 --- a/src/common/StringConverter.cpp +++ b/src/common/StringConverter.cpp @@ -12,18 +12,15 @@ #include #include - -cali_id_t -cali::StringConverter::to_id() const +cali_id_t cali::StringConverter::to_id() const { - bool ok = false; + bool ok = false; cali_id_t id = to_uint(&ok); return ok ? id : CALI_INV_ID; } -bool -cali::StringConverter::to_bool(bool* okptr) const +bool cali::StringConverter::to_bool(bool* okptr) const { bool ok = false; bool res = false; @@ -33,8 +30,7 @@ cali::StringConverter::to_bool(bool* okptr) const { std::string lower; - std::transform(m_str.begin(), m_str.end(), std::back_inserter(lower), - ::tolower); + std::transform(m_str.begin(), m_str.end(), std::back_inserter(lower), ::tolower); if (lower == "true" || lower == "t") { ok = true; @@ -54,7 +50,7 @@ cali::StringConverter::to_bool(bool* okptr) const res = (i != 0); ok = true; } catch (...) { - ok = false; + ok = false; } } @@ -64,8 +60,7 @@ cali::StringConverter::to_bool(bool* okptr) const return res; } -int -cali::StringConverter::to_int(bool* okptr) const +int cali::StringConverter::to_int(bool* okptr) const { bool ok = false; int res = 0; @@ -74,7 +69,7 @@ cali::StringConverter::to_int(bool* okptr) const res = std::stoi(m_str); ok = true; } catch (...) { - ok = false; + ok = false; } if (okptr) @@ -83,8 +78,7 @@ cali::StringConverter::to_int(bool* okptr) const return res; } -int64_t -cali::StringConverter::to_int64(bool* okptr) const +int64_t cali::StringConverter::to_int64(bool* okptr) const { bool ok = false; int64_t res = 0; @@ -93,7 +87,7 @@ cali::StringConverter::to_int64(bool* okptr) const res = std::stoll(m_str); ok = true; } catch (...) { - ok = false; + ok = false; } if (okptr) @@ -102,17 +96,16 @@ cali::StringConverter::to_int64(bool* okptr) const return res; } -uint64_t -cali::StringConverter::to_uint(bool* okptr, int base) const +uint64_t cali::StringConverter::to_uint(bool* okptr, int base) const { - bool ok = false; + bool ok = false; uint64_t res = 0; try { res = std::stoull(m_str, nullptr, base); ok = true; } catch (...) { - ok = false; + ok = false; } if (okptr) @@ -121,8 +114,7 @@ cali::StringConverter::to_uint(bool* okptr, int base) const return res; } -double -cali::StringConverter::to_double(bool* okptr) const +double cali::StringConverter::to_double(bool* okptr) const { bool ok = false; double res = 0; @@ -131,7 +123,7 @@ cali::StringConverter::to_double(bool* okptr) const res = std::stod(m_str); ok = true; } catch (...) { - ok = false; + ok = false; } if (okptr) @@ -140,11 +132,10 @@ cali::StringConverter::to_double(bool* okptr) const return res; } -std::vector -cali::StringConverter::to_stringlist(const char* separators, bool* okptr) const +std::vector cali::StringConverter::to_stringlist(const char* separators, bool* okptr) const { std::vector ret; - char c; + char c; std::istringstream is(m_str); @@ -163,17 +154,16 @@ cali::StringConverter::to_stringlist(const char* separators, bool* okptr) const return ret; } -std::vector -cali::StringConverter::rec_list(bool* okptr) const +std::vector cali::StringConverter::rec_list(bool* okptr) const { std::vector ret; - bool error = false; - char c; + bool error = false; + char c; std::istringstream is(m_str); int d = 0; - c = util::read_char(is); + c = util::read_char(is); if (c == '[') ++d; @@ -228,15 +218,14 @@ cali::StringConverter::rec_list(bool* okptr) const return ret; } -std::map -cali::StringConverter::rec_dict(bool *okptr) const +std::map cali::StringConverter::rec_dict(bool* okptr) const { std::map ret; - bool error = false; + bool error = false; std::istringstream is(m_str); - int d = 0; + int d = 0; char c = util::read_char(is); if (c == '{') @@ -250,7 +239,7 @@ cali::StringConverter::rec_dict(bool *okptr) const do { std::string key = util::read_word(is, ":"); - c = util::read_char(is); + c = util::read_char(is); if (c != ':') { if (okptr) diff --git a/src/common/Variant.cpp b/src/common/Variant.cpp index 56b23a4fe..7aeb278b9 100644 --- a/src/common/Variant.cpp +++ b/src/common/Variant.cpp @@ -21,8 +21,7 @@ using namespace cali; -cali_id_t -Variant::to_id(bool* okptr) const +cali_id_t Variant::to_id(bool* okptr) const { bool ok = false; cali_id_t id = to_uint(&ok); @@ -33,8 +32,7 @@ Variant::to_id(bool* okptr) const return ok ? id : CALI_INV_ID; } -std::string -Variant::to_string() const +std::string Variant::to_string() const { std::string ret; @@ -42,17 +40,20 @@ Variant::to_string() const case CALI_TYPE_INV: break; case CALI_TYPE_USR: - { - size_t size = this->size(); - const void* ptr = data(); + { + size_t size = this->size(); + const void* ptr = data(); - std::ostringstream os; + std::ostringstream os; - std::copy(static_cast(ptr), static_cast(ptr) + size, - std::ostream_iterator(os << std::hex << std::setw(2) << std::setfill('0'), ":")); + std::copy( + static_cast(ptr), + static_cast(ptr) + size, + std::ostream_iterator(os << std::hex << std::setw(2) << std::setfill('0'), ":") + ); - ret = os.str(); - } + ret = os.str(); + } break; case CALI_TYPE_INT: ret = std::to_string(m_v.value.v_int); @@ -61,22 +62,22 @@ Variant::to_string() const ret = std::to_string(m_v.value.v_uint); break; case CALI_TYPE_STRING: - { - const char* str = static_cast(data()); - std::size_t len = size(); + { + const char* str = static_cast(data()); + std::size_t len = size(); - if (len && str[len-1] == 0) - --len; + if (len && str[len - 1] == 0) + --len; - ret.assign(str, len); - } + ret.assign(str, len); + } break; case CALI_TYPE_ADDR: - { - std::ostringstream os; - os << std::hex << to_uint(); - ret = os.str(); - } + { + std::ostringstream os; + os << std::hex << to_uint(); + ret = os.str(); + } break; case CALI_TYPE_DOUBLE: ret = std::to_string(m_v.value.v_double); @@ -88,18 +89,17 @@ Variant::to_string() const ret = cali_type2string(to_attr_type()); break; case CALI_TYPE_PTR: - { - std::ostringstream os; - os << std::hex << reinterpret_cast(data()); - ret = os.str(); - } + { + std::ostringstream os; + os << std::hex << reinterpret_cast(data()); + ret = os.str(); + } } return ret; } -Variant -Variant::from_string(cali_attr_type type, const char* str) +Variant Variant::from_string(cali_attr_type type, const char* str) { switch (type) { case CALI_TYPE_INV: @@ -110,14 +110,14 @@ Variant::from_string(cali_attr_type type, const char* str) return Variant(CALI_TYPE_STRING, str, strlen(str)); case CALI_TYPE_INT: { - char* str_end = nullptr; - long long ll = std::strtoll(str, &str_end, 10); + char* str_end = nullptr; + long long ll = std::strtoll(str, &str_end, 10); return str != str_end ? Variant(cali_make_variant_from_int64(ll)) : Variant(); } case CALI_TYPE_ADDR: { - bool ok = false; - uint64_t u = StringConverter(str).to_uint(&ok, 16); + bool ok = false; + uint64_t u = StringConverter(str).to_uint(&ok, 16); return ok ? Variant(CALI_TYPE_ADDR, &u, sizeof(uint64_t)) : Variant(); } case CALI_TYPE_UINT: @@ -127,29 +127,27 @@ Variant::from_string(cali_attr_type type, const char* str) } case CALI_TYPE_DOUBLE: { - char* str_end = nullptr; - double d = std::strtod(str, &str_end); + char* str_end = nullptr; + double d = std::strtod(str, &str_end); return str != str_end ? Variant(d) : Variant(); } case CALI_TYPE_BOOL: { bool ok = false; - bool b = StringConverter(str).to_bool(&ok); + bool b = StringConverter(str).to_bool(&ok); return ok ? Variant(b) : Variant(); } case CALI_TYPE_TYPE: { cali_attr_type type = cali_string2type(str); return (type != CALI_TYPE_INV) ? Variant(type) : Variant(); - } } return Variant(); } -std::ostream& -Variant::write_cali(std::ostream& os) +std::ostream& Variant::write_cali(std::ostream& os) { cali_attr_type type = this->type(); @@ -178,13 +176,13 @@ Variant::write_cali(std::ostream& os) return os; } -std::ostream& cali::operator << (std::ostream& os, const Variant& v) +std::ostream& cali::operator<< (std::ostream& os, const Variant& v) { os << v.to_string(); return os; } -Variant& Variant::operator += (const Variant& val) +Variant& Variant::operator+= (const Variant& val) { cali_attr_type type = this->type(); diff --git a/src/common/cali_types.c b/src/common/cali_types.c index 608eeb84e..ac52666e1 100644 --- a/src/common/cali_types.c +++ b/src/common/cali_types.c @@ -13,100 +13,88 @@ #include static const struct typemap_t { - const char* str; cali_attr_type type; -} typemap[] = { - { "inv", CALI_TYPE_INV }, - { "usr", CALI_TYPE_USR }, - { "int", CALI_TYPE_INT }, - { "uint", CALI_TYPE_UINT }, - { "string", CALI_TYPE_STRING }, - { "addr", CALI_TYPE_ADDR }, - { "double", CALI_TYPE_DOUBLE }, - { "bool", CALI_TYPE_BOOL }, - { "type", CALI_TYPE_TYPE }, - { "ptr", CALI_TYPE_PTR }, - { NULL, CALI_TYPE_INV } -}; - -const char* -cali_type2string(cali_attr_type type) + const char* str; + cali_attr_type type; +} typemap[] = { { "inv", CALI_TYPE_INV }, { "usr", CALI_TYPE_USR }, { "int", CALI_TYPE_INT }, + { "uint", CALI_TYPE_UINT }, { "string", CALI_TYPE_STRING }, { "addr", CALI_TYPE_ADDR }, + { "double", CALI_TYPE_DOUBLE }, { "bool", CALI_TYPE_BOOL }, { "type", CALI_TYPE_TYPE }, + { "ptr", CALI_TYPE_PTR }, { NULL, CALI_TYPE_INV } }; + +const char* cali_type2string(cali_attr_type type) { - return (type <= CALI_MAXTYPE ? typemap[type].str : "inv"); + return (type <= CALI_MAXTYPE ? typemap[type].str : "inv"); } -cali_attr_type -cali_string2type(const char* str) +cali_attr_type cali_string2type(const char* str) { - for (const struct typemap_t* t = typemap; t->str; ++t) - if (strcmp(str, t->str) == 0) - return t->type; + for (const struct typemap_t* t = typemap; t->str; ++t) + if (strcmp(str, t->str) == 0) + return t->type; - return CALI_TYPE_INV; + return CALI_TYPE_INV; } static const struct propmap_t { - const char* str; int prop; int mask; -} propmap[] = { - { "default", CALI_ATTR_DEFAULT, CALI_ATTR_DEFAULT }, - { "asvalue", CALI_ATTR_ASVALUE, CALI_ATTR_ASVALUE }, - { "nomerge", CALI_ATTR_NOMERGE, CALI_ATTR_NOMERGE }, - { "process_scope", CALI_ATTR_SCOPE_PROCESS, CALI_ATTR_SCOPE_MASK }, - { "thread_scope", CALI_ATTR_SCOPE_THREAD, CALI_ATTR_SCOPE_MASK }, - { "task_scope", CALI_ATTR_SCOPE_TASK, CALI_ATTR_SCOPE_MASK }, - { "skip_events", CALI_ATTR_SKIP_EVENTS, CALI_ATTR_SKIP_EVENTS }, - { "hidden", CALI_ATTR_HIDDEN, CALI_ATTR_HIDDEN }, - { "nested", CALI_ATTR_NESTED, CALI_ATTR_NESTED }, - { "global", CALI_ATTR_GLOBAL, CALI_ATTR_GLOBAL }, - { "unaligned", CALI_ATTR_UNALIGNED, CALI_ATTR_UNALIGNED }, - { "aggregatable", CALI_ATTR_AGGREGATABLE, CALI_ATTR_AGGREGATABLE }, - { "level_0", CALI_ATTR_LEVEL_0, CALI_ATTR_LEVEL_MASK }, - { "level_1", CALI_ATTR_LEVEL_1, CALI_ATTR_LEVEL_MASK }, - { "level_2", CALI_ATTR_LEVEL_2, CALI_ATTR_LEVEL_MASK }, - { "level_3", CALI_ATTR_LEVEL_3, CALI_ATTR_LEVEL_MASK }, - { "level_4", CALI_ATTR_LEVEL_4, CALI_ATTR_LEVEL_MASK }, - { "level_5", CALI_ATTR_LEVEL_5, CALI_ATTR_LEVEL_MASK }, - { "level_6", CALI_ATTR_LEVEL_6, CALI_ATTR_LEVEL_MASK }, - { "level_7", CALI_ATTR_LEVEL_7, CALI_ATTR_LEVEL_MASK }, - { 0, CALI_ATTR_DEFAULT, CALI_ATTR_DEFAULT } -}; - -int -cali_prop2string(int prop, char* buf, size_t len) + const char* str; + int prop; + int mask; +} propmap[] = { { "default", CALI_ATTR_DEFAULT, CALI_ATTR_DEFAULT }, + { "asvalue", CALI_ATTR_ASVALUE, CALI_ATTR_ASVALUE }, + { "nomerge", CALI_ATTR_NOMERGE, CALI_ATTR_NOMERGE }, + { "process_scope", CALI_ATTR_SCOPE_PROCESS, CALI_ATTR_SCOPE_MASK }, + { "thread_scope", CALI_ATTR_SCOPE_THREAD, CALI_ATTR_SCOPE_MASK }, + { "task_scope", CALI_ATTR_SCOPE_TASK, CALI_ATTR_SCOPE_MASK }, + { "skip_events", CALI_ATTR_SKIP_EVENTS, CALI_ATTR_SKIP_EVENTS }, + { "hidden", CALI_ATTR_HIDDEN, CALI_ATTR_HIDDEN }, + { "nested", CALI_ATTR_NESTED, CALI_ATTR_NESTED }, + { "global", CALI_ATTR_GLOBAL, CALI_ATTR_GLOBAL }, + { "unaligned", CALI_ATTR_UNALIGNED, CALI_ATTR_UNALIGNED }, + { "aggregatable", CALI_ATTR_AGGREGATABLE, CALI_ATTR_AGGREGATABLE }, + { "level_0", CALI_ATTR_LEVEL_0, CALI_ATTR_LEVEL_MASK }, + { "level_1", CALI_ATTR_LEVEL_1, CALI_ATTR_LEVEL_MASK }, + { "level_2", CALI_ATTR_LEVEL_2, CALI_ATTR_LEVEL_MASK }, + { "level_3", CALI_ATTR_LEVEL_3, CALI_ATTR_LEVEL_MASK }, + { "level_4", CALI_ATTR_LEVEL_4, CALI_ATTR_LEVEL_MASK }, + { "level_5", CALI_ATTR_LEVEL_5, CALI_ATTR_LEVEL_MASK }, + { "level_6", CALI_ATTR_LEVEL_6, CALI_ATTR_LEVEL_MASK }, + { "level_7", CALI_ATTR_LEVEL_7, CALI_ATTR_LEVEL_MASK }, + { 0, CALI_ATTR_DEFAULT, CALI_ATTR_DEFAULT } }; + +int cali_prop2string(int prop, char* buf, size_t len) { - int ret = 0; + int ret = 0; - for (const struct propmap_t* p = propmap; p->str; ++p) { - if (!((prop & p->mask) == (int) p->prop)) - continue; + for (const struct propmap_t* p = propmap; p->str; ++p) { + if (!((prop & p->mask) == (int) p->prop)) + continue; - size_t slen = strlen(p->str); + size_t slen = strlen(p->str); - if ((slen + (ret>0?2:1)) > len) - return -1; - if (ret > 0) - buf[ret++] = ':'; + if ((slen + (ret > 0 ? 2 : 1)) > len) + return -1; + if (ret > 0) + buf[ret++] = ':'; - strcpy(buf+ret, p->str); + strcpy(buf + ret, p->str); - ret += slen; - buf[ret] = '\0'; - } + ret += slen; + buf[ret] = '\0'; + } - return ret; + return ret; } -int -cali_string2prop(const char* str) +int cali_string2prop(const char* str) { - int prop = 0; + int prop = 0; - for (const struct propmap_t* p = propmap; p->str; ++p) { - const char* pos = strstr(str, p->str); - size_t len = strlen(p->str); + for (const struct propmap_t* p = propmap; p->str; ++p) { + const char* pos = strstr(str, p->str); + size_t len = strlen(p->str); - if (pos && ((pos == str) || !isalnum(*(pos-1))) && !isalnum(pos[len])) - prop |= p->prop; - } + if (pos && ((pos == str) || !isalnum(*(pos - 1))) && !isalnum(pos[len])) + prop |= p->prop; + } - return prop; + return prop; } diff --git a/src/common/cali_variant.c b/src/common/cali_variant.c index 5962fce98..f2351cb18 100644 --- a/src/common/cali_variant.c +++ b/src/common/cali_variant.c @@ -16,23 +16,19 @@ #define _EXTRACT_TYPE(type_and_size) ((type_and_size) & CALI_VARIANT_TYPE_MASK) #define _EXTRACT_SIZE(type_and_size) ((type_and_size) >> 32) -extern inline bool -cali_variant_is_empty(cali_variant_t v); +extern inline bool cali_variant_is_empty(cali_variant_t v); -extern inline cali_variant_t -cali_make_empty_variant(); +extern inline cali_variant_t cali_make_empty_variant(); -cali_attr_type -cali_variant_get_type(cali_variant_t v) +cali_attr_type cali_variant_get_type(cali_variant_t v) { uint64_t t = _EXTRACT_TYPE(v.type_and_size); return (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); } -size_t -cali_variant_get_size(cali_variant_t v) +size_t cali_variant_get_size(cali_variant_t v) { - uint64_t t = _EXTRACT_TYPE(v.type_and_size); + uint64_t t = _EXTRACT_TYPE(v.type_and_size); cali_attr_type type = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); switch (type) { @@ -58,10 +54,9 @@ cali_variant_get_size(cali_variant_t v) return 0; } -const void* -cali_variant_get_data(const cali_variant_t* v) +const void* cali_variant_get_data(const cali_variant_t* v) { - uint64_t t = _EXTRACT_TYPE(v->type_and_size); + uint64_t t = _EXTRACT_TYPE(v->type_and_size); cali_attr_type type = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); switch (type) { @@ -88,8 +83,7 @@ cali_variant_get_data(const cali_variant_t* v) return NULL; } -cali_variant_t -cali_make_variant(cali_attr_type type, const void* ptr, size_t size) +cali_variant_t cali_make_variant(cali_attr_type type, const void* ptr, size_t size) { cali_variant_t v = { 0, { .v_uint = 0 } }; @@ -100,38 +94,38 @@ cali_make_variant(cali_attr_type type, const void* ptr, size_t size) break; case CALI_TYPE_USR: case CALI_TYPE_STRING: - { - uint64_t hash = 0; - - if (size > 0) { - const unsigned char* p = (const unsigned char*) ptr; - hash |= p[0]; - hash <<= 8; - hash |= p[size/2]; - hash <<= 8; - hash |= p[size-1]; - hash <<= 8; - } + { + uint64_t hash = 0; + + if (size > 0) { + const unsigned char* p = (const unsigned char*) ptr; + hash |= p[0]; + hash <<= 8; + hash |= p[size / 2]; + hash <<= 8; + hash |= p[size - 1]; + hash <<= 8; + } - v.type_and_size = (size << 32) | hash | (type & CALI_VARIANT_TYPE_MASK); - v.value.unmanaged_const_ptr = ptr; - } + v.type_and_size = (size << 32) | hash | (type & CALI_VARIANT_TYPE_MASK); + v.value.unmanaged_const_ptr = ptr; + } break; case CALI_TYPE_INT: - v.value.v_int = *((const int64_t*) ptr); + v.value.v_int = *((const int64_t*) ptr); break; case CALI_TYPE_ADDR: case CALI_TYPE_UINT: - v.value.v_uint = *((const uint64_t*) ptr); + v.value.v_uint = *((const uint64_t*) ptr); break; case CALI_TYPE_DOUBLE: v.value.v_double = *((const double*) ptr); break; case CALI_TYPE_BOOL: - v.value.v_bool = *((const bool*) ptr); + v.value.v_bool = *((const bool*) ptr); break; case CALI_TYPE_TYPE: - v.value.v_type = *((const cali_attr_type*) ptr); + v.value.v_type = *((const cali_attr_type*) ptr); break; case CALI_TYPE_PTR: v.value.unmanaged_const_ptr = ptr; @@ -141,32 +135,23 @@ cali_make_variant(cali_attr_type type, const void* ptr, size_t size) return v; } -extern inline cali_variant_t -cali_make_variant_from_bool(bool value); +extern inline cali_variant_t cali_make_variant_from_bool(bool value); -extern inline cali_variant_t -cali_make_variant_from_int(int value); +extern inline cali_variant_t cali_make_variant_from_int(int value); -extern inline cali_variant_t -cali_make_variant_from_int64(int64_t value); +extern inline cali_variant_t cali_make_variant_from_int64(int64_t value); -extern inline cali_variant_t -cali_make_variant_from_uint(uint64_t value); +extern inline cali_variant_t cali_make_variant_from_uint(uint64_t value); -extern inline cali_variant_t -cali_make_variant_from_double(double value); +extern inline cali_variant_t cali_make_variant_from_double(double value); -extern inline cali_variant_t -cali_make_variant_from_string(const char* str); +extern inline cali_variant_t cali_make_variant_from_string(const char* str); -extern inline cali_variant_t -cali_make_variant_from_type(cali_attr_type value); +extern inline cali_variant_t cali_make_variant_from_type(cali_attr_type value); -extern inline cali_variant_t -cali_make_variant_from_ptr(void* ptr); +extern inline cali_variant_t cali_make_variant_from_ptr(void* ptr); -extern inline void* -cali_variant_get_ptr(cali_variant_t v); +extern inline void* cali_variant_get_ptr(cali_variant_t v); /** \brief Return the variant's value as integer * @@ -180,8 +165,7 @@ cali_variant_get_ptr(cali_variant_t v); * \param okptr If non-NULL, indicate success or failure int the referenced variable. * \return The integer value. Zero if the conversion was unsuccesful. */ -int -cali_variant_to_int(cali_variant_t v, bool* okptr) +int cali_variant_to_int(cali_variant_t v, bool* okptr) { bool ok = true; int ret = 0; @@ -194,7 +178,7 @@ cali_variant_to_int(cali_variant_t v, bool* okptr) case CALI_TYPE_USR: case CALI_TYPE_STRING: case CALI_TYPE_PTR: - ok = false; + ok = false; break; case CALI_TYPE_INT: if (v.value.v_int >= INT_MIN && v.value.v_int <= INT_MAX) @@ -238,8 +222,7 @@ cali_variant_to_int(cali_variant_t v, bool* okptr) * \param okptr If non-NULL, indicate success or failure int the referenced variable. * \return The integer value. Zero if the conversion was unsuccesful. */ -int64_t -cali_variant_to_int64(cali_variant_t v, bool* okptr) +int64_t cali_variant_to_int64(cali_variant_t v, bool* okptr) { bool ok = true; int64_t ret = 0; @@ -252,7 +235,7 @@ cali_variant_to_int64(cali_variant_t v, bool* okptr) case CALI_TYPE_USR: case CALI_TYPE_STRING: case CALI_TYPE_PTR: - ok = false; + ok = false; break; case CALI_TYPE_INT: ret = v.value.v_int; @@ -293,8 +276,7 @@ cali_variant_to_int64(cali_variant_t v, bool* okptr) * \param okptr If non-NULL, indicate success or failure int the referenced variable. * \return The integer value. Zero if the conversion was unsuccesful. */ -uint64_t -cali_variant_to_uint(cali_variant_t v, bool* okptr) +uint64_t cali_variant_to_uint(cali_variant_t v, bool* okptr) { bool ok = true; uint64_t ret = 0; @@ -307,7 +289,7 @@ cali_variant_to_uint(cali_variant_t v, bool* okptr) case CALI_TYPE_USR: case CALI_TYPE_STRING: case CALI_TYPE_PTR: - ok = false; + ok = false; break; case CALI_TYPE_INT: if (v.value.v_int >= 0) @@ -348,13 +330,12 @@ cali_variant_to_uint(cali_variant_t v, bool* okptr) * \param okptr If non-NULL, indicate success or failure int the referenced variable. * \return The double value. Zero if the conversion was unsuccesful. */ -double -cali_variant_to_double(cali_variant_t v, bool* okptr) +double cali_variant_to_double(cali_variant_t v, bool* okptr) { bool ok = true; double ret = 0; - uint64_t t = _EXTRACT_TYPE(v.type_and_size); + uint64_t t = _EXTRACT_TYPE(v.type_and_size); cali_attr_type type = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); switch (type) { @@ -362,7 +343,7 @@ cali_variant_to_double(cali_variant_t v, bool* okptr) case CALI_TYPE_USR: case CALI_TYPE_STRING: case CALI_TYPE_PTR: - ok = false; + ok = false; break; case CALI_TYPE_INT: ret = (double) v.value.v_int; @@ -400,28 +381,27 @@ cali_variant_to_double(cali_variant_t v, bool* okptr) * \param okptr If non-NULL, indicate success or failure int the referenced variable. * \return The boolean value; `false` if the conversion was unsuccesful. */ -bool -cali_variant_to_bool(cali_variant_t v, bool* okptr) +bool cali_variant_to_bool(cali_variant_t v, bool* okptr) { bool ok = true; bool ret = false; - uint64_t t = _EXTRACT_TYPE(v.type_and_size); + uint64_t t = _EXTRACT_TYPE(v.type_and_size); cali_attr_type type = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); switch (type) { case CALI_TYPE_INT: - ret = (v.value.v_int != 0); + ret = (v.value.v_int != 0); break; case CALI_TYPE_UINT: case CALI_TYPE_ADDR: - ret = (v.value.v_uint != 0); + ret = (v.value.v_uint != 0); break; case CALI_TYPE_BOOL: ret = v.value.v_bool; break; default: - ok = false; + ok = false; } if (okptr) @@ -441,13 +421,12 @@ cali_variant_to_bool(cali_variant_t v, bool* okptr) * \param okptr If non-NULL, indicate success or failure int the referenced variable. * \return The type value. CALI_TYPE_INV if the conversion was unsuccesful. */ -cali_attr_type -cali_variant_to_type(cali_variant_t v, bool* okptr) +cali_attr_type cali_variant_to_type(cali_variant_t v, bool* okptr) { - bool ok = true; + bool ok = true; cali_attr_type ret = CALI_TYPE_INV; - uint64_t t = _EXTRACT_TYPE(v.type_and_size); + uint64_t t = _EXTRACT_TYPE(v.type_and_size); cali_attr_type type = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); switch (type) { @@ -455,7 +434,7 @@ cali_variant_to_type(cali_variant_t v, bool* okptr) ret = v.value.v_type; break; default: - ok = false; + ok = false; } if (okptr) @@ -464,18 +443,16 @@ cali_variant_to_type(cali_variant_t v, bool* okptr) return ret; } -static inline int -imin(int l, int r) +static inline int imin(int l, int r) { return l < r ? l : r; } -int -cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs) +int cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs) { - uint64_t t = _EXTRACT_TYPE(lhs.type_and_size); + uint64_t t = _EXTRACT_TYPE(lhs.type_and_size); cali_attr_type lhstype = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); - t = _EXTRACT_TYPE(rhs.type_and_size); + t = _EXTRACT_TYPE(rhs.type_and_size); cali_attr_type rhstype = (t <= CALI_MAXTYPE ? (cali_attr_type) t : CALI_TYPE_INV); if (lhstype == rhstype) { @@ -486,8 +463,7 @@ cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs) { int lhssize = (int) _EXTRACT_SIZE(lhs.type_and_size); int rhssize = (int) _EXTRACT_SIZE(rhs.type_and_size); - int cmp = memcmp(lhs.value.unmanaged_const_ptr, rhs.value.unmanaged_const_ptr, - imin(lhssize, rhssize)); + int cmp = memcmp(lhs.value.unmanaged_const_ptr, rhs.value.unmanaged_const_ptr, imin(lhssize, rhssize)); return (cmp ? cmp : (lhssize - rhssize)); } @@ -495,8 +471,7 @@ cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs) { int lhssize = (int) _EXTRACT_SIZE(lhs.type_and_size); int rhssize = (int) _EXTRACT_SIZE(rhs.type_and_size); - int cmp = strncmp(lhs.value.unmanaged_const_ptr, rhs.value.unmanaged_const_ptr, - imin(lhssize, rhssize)); + int cmp = strncmp(lhs.value.unmanaged_const_ptr, rhs.value.unmanaged_const_ptr, imin(lhssize, rhssize)); return (cmp ? cmp : (lhssize - rhssize)); } @@ -520,27 +495,30 @@ cali_variant_compare(cali_variant_t lhs, cali_variant_t rhs) case CALI_TYPE_TYPE: return ((int) lhs.value.v_type) - ((int) rhs.value.v_type); case CALI_TYPE_PTR: - return (int) ((char const*)(lhs.value.unmanaged_ptr) - (char const*)(rhs.value.unmanaged_ptr)); + return (int) ((char const*) (lhs.value.unmanaged_ptr) - (char const*) (rhs.value.unmanaged_ptr)); } } return ((int) lhstype) - ((int) rhstype); } -bool -_cali_variant_value_eq(cali_variant_t lhs, cali_variant_t rhs) +bool _cali_variant_value_eq(cali_variant_t lhs, cali_variant_t rhs) { /* We can assume that type_and_size are equal */ - switch ( _EXTRACT_TYPE(lhs.type_and_size) ) { + switch (_EXTRACT_TYPE(lhs.type_and_size)) { case CALI_TYPE_USR: case CALI_TYPE_STRING: { if (lhs.value.unmanaged_const_ptr == rhs.value.unmanaged_const_ptr) return true; else - return 0 == memcmp(lhs.value.unmanaged_const_ptr, rhs.value.unmanaged_const_ptr, - _EXTRACT_SIZE(lhs.type_and_size)); + return 0 + == memcmp( + lhs.value.unmanaged_const_ptr, + rhs.value.unmanaged_const_ptr, + _EXTRACT_SIZE(lhs.type_and_size) + ); } break; default: @@ -550,25 +528,22 @@ _cali_variant_value_eq(cali_variant_t lhs, cali_variant_t rhs) return false; } -extern inline bool -cali_variant_eq(cali_variant_t lhs, cali_variant_t rhs); +extern inline bool cali_variant_eq(cali_variant_t lhs, cali_variant_t rhs); -size_t -cali_variant_pack(cali_variant_t v, unsigned char* buf) +size_t cali_variant_pack(cali_variant_t v, unsigned char* buf) { size_t pos = 0; pos += vlenc_u64(v.type_and_size, buf); - pos += vlenc_u64(v.value.v_uint, buf+pos); + pos += vlenc_u64(v.value.v_uint, buf + pos); return pos; } -cali_variant_t -cali_variant_unpack(const unsigned char* buf, size_t* inc, bool *okptr) +cali_variant_t cali_variant_unpack(const unsigned char* buf, size_t* inc, bool* okptr) { cali_variant_t v = { 0, { .v_uint = 0 } }; - size_t p = 0; + size_t p = 0; uint64_t ts = vldec_u64(buf, &p); @@ -580,10 +555,10 @@ cali_variant_unpack(const unsigned char* buf, size_t* inc, bool *okptr) } v.type_and_size = ts; - v.value.v_uint = vldec_u64(buf+p, &p); + v.value.v_uint = vldec_u64(buf + p, &p); if (inc) - *inc += p; + *inc += p; if (okptr) *okptr = true; diff --git a/src/common/test/MockupMetadataDB.h b/src/common/test/MockupMetadataDB.h index 0039edd71..9d0cb93e7 100644 --- a/src/common/test/MockupMetadataDB.h +++ b/src/common/test/MockupMetadataDB.h @@ -1,4 +1,4 @@ -// Copyright (c) 2016, Lawrence Livermore National Security, LLC. +// Copyright (c) 2016, Lawrence Livermore National Security, LLC. // Produced at the Lawrence Livermore National Laboratory. // // This file is part of Caliper. @@ -30,7 +30,7 @@ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -/// \file MockupMetadataDB.h +/// \file MockupMetadataDB.h /// Mockup Caliper metadata access interface for testing #pragma once @@ -48,60 +48,53 @@ namespace cali /// \brief A simple mockup implementation for CaliperMetadataAccessInterface class MockupMetadataDB : public CaliperMetadataAccessInterface { - std::map m_node_map; - std::map m_attr_map; + std::map m_node_map; + std::map m_attr_map; std::map m_attr_names; // gtest FAIL() can only be called in void functions - void fail(const char* str) { - FAIL() << str; - } - + void fail(const char* str) { FAIL() << str; } + public: - MockupMetadataDB() - { } + MockupMetadataDB() {} - ~MockupMetadataDB() - { } + ~MockupMetadataDB() {} - inline void - add_node(Node* node) { - m_node_map.insert(std::make_pair(node->id(), node)); - } + inline void add_node(Node* node) { m_node_map.insert(std::make_pair(node->id(), node)); } - inline void - add_nodes(size_t n, Node* nodes[]) { + inline void add_nodes(size_t n, Node* nodes[]) + { for (size_t i = 0; i < n; ++i) add_node(nodes[i]); } - - inline void - add_attribute(const Attribute& attr) { + + inline void add_attribute(const Attribute& attr) + { m_attr_map.insert(std::make_pair(attr.id(), attr)); m_attr_names.insert(std::make_pair(attr.name(), attr)); } - - inline Node* - node(cali_id_t id) const { + + inline Node* node(cali_id_t id) const + { auto it = m_node_map.find(id); return it == m_node_map.end() ? nullptr : it->second; } - inline Attribute - get_attribute(cali_id_t id) const { + inline Attribute get_attribute(cali_id_t id) const + { auto it = m_attr_map.find(id); return it == m_attr_map.end() ? Attribute() : it->second; } - inline Attribute - get_attribute(const std::string& name) const { + inline Attribute get_attribute(const std::string& name) const + { auto it = m_attr_names.find(name); return it == m_attr_names.end() ? Attribute() : it->second; } - inline std::vector - get_all_attributes() const { + inline std::vector get_all_attributes() const + { std::vector vec; for (auto p : m_attr_map) @@ -110,28 +103,30 @@ class MockupMetadataDB : public CaliperMetadataAccessInterface return vec; } - Attribute - create_attribute(const std::string& name, - cali_attr_type type, - int prop = CALI_ATTR_DEFAULT, - int meta = 0, - const Attribute* meta_attr = nullptr, - const Variant* meta_data = nullptr) { + Attribute create_attribute( + const std::string& name, + cali_attr_type type, + int prop = CALI_ATTR_DEFAULT, + int meta = 0, + const Attribute* meta_attr = nullptr, + const Variant* meta_data = nullptr + ) + { fail("create_attribute() is not implemented in MockupMetadataDB!"); return Attribute(); } - Node* - make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent) { + Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent) + { fail("make_tree_entry() is not implemented in MockupMetadataDB!"); return nullptr; } - inline std::vector - get_globals() { + inline std::vector get_globals() + { fail("get_globals() is not implemented in MockupMetadataDB!"); return std::vector(); } }; -} // namespace +} // namespace cali diff --git a/src/common/test/test_c_variant.cpp b/src/common/test/test_c_variant.cpp index 62871646f..a32c2dd53 100644 --- a/src/common/test/test_c_variant.cpp +++ b/src/common/test/test_c_variant.cpp @@ -8,17 +8,19 @@ // --- test variant creation // -TEST(C_Variant_Test, CreateEmptyVariant) { +TEST(C_Variant_Test, CreateEmptyVariant) +{ cali_variant_t v = cali_make_empty_variant(); - + EXPECT_TRUE(cali_variant_is_empty(v)); - + EXPECT_EQ(cali_variant_get_type(v), CALI_TYPE_INV); EXPECT_EQ(cali_variant_get_size(v), 0); EXPECT_EQ(cali_variant_get_data(&v), nullptr); } -TEST(C_Variant_Test, CreateIntVariant) { +TEST(C_Variant_Test, CreateIntVariant) +{ int val = -42; cali_variant_t v = cali_make_variant_from_int(val); @@ -45,7 +47,8 @@ TEST(C_Variant_Test, CreateIntVariant) { EXPECT_EQ(cali_variant_compare(v, v2), 0); } -TEST(C_Variant_Test, CreateUintVariant) { +TEST(C_Variant_Test, CreateUintVariant) +{ uint64_t val = 0xFFFFFFFFAA; cali_variant_t v = cali_make_variant_from_uint(val); @@ -71,19 +74,20 @@ TEST(C_Variant_Test, CreateUintVariant) { // also test ADDR type here, basically the same as UINT - void* ptr = &val; - cali_variant_t v3 = cali_make_variant(CALI_TYPE_ADDR, &ptr, sizeof(void*)); + void* ptr = &val; + cali_variant_t v3 = cali_make_variant(CALI_TYPE_ADDR, &ptr, sizeof(void*)); EXPECT_EQ(cali_variant_get_type(v3), CALI_TYPE_ADDR); EXPECT_EQ(cali_variant_get_size(v3), sizeof(uint64_t)); // yes, Caliper ADDR type is always 64bit EXPECT_EQ(cali_variant_to_uint(v3, NULL), reinterpret_cast(ptr)); } -TEST(C_Variant_Test, CreateDoubleVariant) { +TEST(C_Variant_Test, CreateDoubleVariant) +{ double val = 42.42; - cali_variant_t v = cali_make_variant_from_double(val); - bool ok = false; - + cali_variant_t v = cali_make_variant_from_double(val); + bool ok = false; + EXPECT_EQ(cali_variant_get_type(v), CALI_TYPE_DOUBLE); EXPECT_EQ(cali_variant_to_double(v, &ok), val); EXPECT_TRUE(ok); @@ -102,7 +106,8 @@ TEST(C_Variant_Test, CreateDoubleVariant) { EXPECT_EQ(*((double*) cali_variant_get_data(&v2)), val); } -TEST(C_Variant_Test, CreateStringVariant) { +TEST(C_Variant_Test, CreateStringVariant) +{ const char* mystring = "My test string"; cali_variant_t v = cali_make_variant_from_string(mystring); @@ -129,13 +134,14 @@ TEST(C_Variant_Test, CreateStringVariant) { EXPECT_FALSE(ok); } -TEST(C_Variant_Test, CreateExplicitStringVariant) { +TEST(C_Variant_Test, CreateExplicitStringVariant) +{ const char* mystring = "My test string"; - cali_variant_t v = cali_make_variant(CALI_TYPE_STRING, mystring, strlen(mystring)+1); + cali_variant_t v = cali_make_variant(CALI_TYPE_STRING, mystring, strlen(mystring) + 1); EXPECT_EQ(cali_variant_get_type(v), CALI_TYPE_STRING); - EXPECT_EQ(cali_variant_get_size(v), strlen(mystring)+1); + EXPECT_EQ(cali_variant_get_size(v), strlen(mystring) + 1); EXPECT_EQ(cali_variant_get_data(&v), mystring); EXPECT_STREQ(static_cast(cali_variant_get_data(&v)), mystring); @@ -156,7 +162,8 @@ TEST(C_Variant_Test, CreateExplicitStringVariant) { EXPECT_FALSE(ok); } -TEST(C_Variant_Test, CreateBoolVariant) { +TEST(C_Variant_Test, CreateBoolVariant) +{ bool val = true; cali_variant_t v = cali_make_variant_from_bool(val); @@ -169,7 +176,8 @@ TEST(C_Variant_Test, CreateBoolVariant) { EXPECT_EQ(cali_variant_compare(v, v2), 0); } -TEST(C_Variant_Test, CreateTypeVariant) { +TEST(C_Variant_Test, CreateTypeVariant) +{ cali_attr_type val = CALI_TYPE_INT; cali_variant_t v = cali_make_variant_from_type(val); @@ -185,7 +193,8 @@ TEST(C_Variant_Test, CreateTypeVariant) { // --- comparison // -TEST(C_Variant_Test, Compare) { +TEST(C_Variant_Test, Compare) +{ cali_variant_t v_int_s = cali_make_variant_from_int(-42); cali_variant_t v_int_l = cali_make_variant_from_int(4000); @@ -232,17 +241,15 @@ TEST(C_Variant_Test, Compare) { EXPECT_NE(cali_variant_compare(v_int_s, v_dbl_s), 0); cali_variant_t v_inv = cali_make_variant(CALI_TYPE_INV, NULL, 0); - + EXPECT_EQ(cali_variant_compare(v_inv, v_inv), 0); EXPECT_NE(cali_variant_compare(v_inv, v_uint_l), 0); const char* str_ul = "abcd"; const char* str_us = "abc"; - cali_variant_t v_usr_s = - cali_make_variant(CALI_TYPE_USR, (void*) str_us, strlen(str_us)); - cali_variant_t v_usr_l = - cali_make_variant(CALI_TYPE_USR, (void*) str_ul, strlen(str_ul)); + cali_variant_t v_usr_s = cali_make_variant(CALI_TYPE_USR, (void*) str_us, strlen(str_us)); + cali_variant_t v_usr_l = cali_make_variant(CALI_TYPE_USR, (void*) str_ul, strlen(str_ul)); EXPECT_EQ(cali_variant_compare(v_usr_l, v_usr_l), 0); EXPECT_NE(cali_variant_compare(v_usr_s, v_usr_l), 0); @@ -252,7 +259,8 @@ TEST(C_Variant_Test, Compare) { // --- Pack/Unpack // -TEST(C_Variant_Test, PackUnpack) { +TEST(C_Variant_Test, PackUnpack) +{ int val_1_int = -27; uint64_t val_2_uint = 0xFFFFFFFFAA; const char* val_3_str = "My wonderful test string"; @@ -263,43 +271,43 @@ TEST(C_Variant_Test, PackUnpack) { cali_variant_t v_1_int_in = cali_make_variant_from_int(val_1_int); cali_variant_t v_2_uint_in = cali_make_variant_from_uint(val_2_uint); - cali_variant_t v_3_str_in = cali_make_variant(CALI_TYPE_STRING, val_3_str, strlen(val_3_str)+1); + cali_variant_t v_3_str_in = cali_make_variant(CALI_TYPE_STRING, val_3_str, strlen(val_3_str) + 1); cali_variant_t v_4_dbl_in = cali_make_variant_from_double(val_4_dbl); cali_variant_t v_5_inv_in = cali_make_variant(CALI_TYPE_INV, val_5_inv, 0); cali_variant_t v_6_type_in = cali_make_variant_from_type(val_6_type); cali_variant_t v_7_bool_in = cali_make_variant_from_bool(val_7_bool); unsigned char buf[144]; // must be >= 7*20 = 140 bytes - size_t pos = 0; + size_t pos = 0; memset(buf, 0xFA, 144); - pos += cali_variant_pack(v_1_int_in, buf+pos); - pos += cali_variant_pack(v_2_uint_in, buf+pos); - pos += cali_variant_pack(v_3_str_in, buf+pos); - pos += cali_variant_pack(v_4_dbl_in, buf+pos); - pos += cali_variant_pack(v_5_inv_in, buf+pos); - pos += cali_variant_pack(v_6_type_in, buf+pos); - pos += cali_variant_pack(v_7_bool_in, buf+pos); + pos += cali_variant_pack(v_1_int_in, buf + pos); + pos += cali_variant_pack(v_2_uint_in, buf + pos); + pos += cali_variant_pack(v_3_str_in, buf + pos); + pos += cali_variant_pack(v_4_dbl_in, buf + pos); + pos += cali_variant_pack(v_5_inv_in, buf + pos); + pos += cali_variant_pack(v_6_type_in, buf + pos); + pos += cali_variant_pack(v_7_bool_in, buf + pos); EXPECT_LE(pos, 140); bool ok = false; - pos = 0; + pos = 0; - cali_variant_t v_1_int_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_1_int_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_1 unpack (int)"); - cali_variant_t v_2_uint_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_2_uint_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_2 unpack (uint)"); - cali_variant_t v_3_str_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_3_str_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_3 unpack (str)"); - cali_variant_t v_4_dbl_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_4_dbl_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_4 unpack (dbl)"); - cali_variant_t v_5_inv_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_5_inv_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_5 unpack (inv)"); - cali_variant_t v_6_type_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_6_type_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_6 unpack (type)"); - cali_variant_t v_7_bool_out = cali_variant_unpack(buf+pos, &pos, &ok); + cali_variant_t v_7_bool_out = cali_variant_unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_7 unpack (bool)"); EXPECT_FALSE(cali_variant_is_empty(v_1_int_out)); @@ -314,7 +322,7 @@ TEST(C_Variant_Test, PackUnpack) { EXPECT_FALSE(cali_variant_is_empty(v_3_str_out)); EXPECT_EQ(cali_variant_get_type(v_3_str_out), CALI_TYPE_STRING); - EXPECT_EQ(cali_variant_get_size(v_3_str_out), strlen(val_3_str)+1); + EXPECT_EQ(cali_variant_get_size(v_3_str_out), strlen(val_3_str) + 1); EXPECT_STREQ(static_cast(cali_variant_get_data(&v_3_str_out)), val_3_str); EXPECT_TRUE(cali_variant_eq(v_3_str_in, v_3_str_out)); @@ -326,7 +334,7 @@ TEST(C_Variant_Test, PackUnpack) { EXPECT_TRUE(cali_variant_is_empty(v_5_inv_out)); EXPECT_EQ(cali_variant_get_type(v_5_inv_out), CALI_TYPE_INV); EXPECT_TRUE(cali_variant_eq(v_5_inv_in, v_5_inv_out)); - + EXPECT_FALSE(cali_variant_is_empty(v_6_type_out)); EXPECT_EQ(cali_variant_get_type(v_6_type_out), CALI_TYPE_TYPE); EXPECT_EQ(cali_variant_to_type(v_6_type_out, NULL), val_6_type); diff --git a/src/common/test/test_compressedsnapshotrecord.cpp b/src/common/test/test_compressedsnapshotrecord.cpp index 49be20a74..374deb06e 100644 --- a/src/common/test/test_compressedsnapshotrecord.cpp +++ b/src/common/test/test_compressedsnapshotrecord.cpp @@ -12,7 +12,8 @@ using namespace cali; // --- tests // -TEST(CompressedSnapshotRecordTest, Append) { +TEST(CompressedSnapshotRecordTest, Append) +{ cali_id_t attr_in[3] = { 7, CALI_INV_ID, 42 }; Variant data_in[3] = { Variant(CALI_TYPE_INT), Variant(), Variant(1.23) }; @@ -29,7 +30,7 @@ TEST(CompressedSnapshotRecordTest, Append) { EXPECT_EQ(rec.append(1, attr_in, data_in), static_cast(0)); EXPECT_EQ(rec.append(2, node_in), static_cast(0)); - EXPECT_EQ(rec.append(2, attr_in+1, data_in+1), static_cast(0)); + EXPECT_EQ(rec.append(2, attr_in + 1, data_in + 1), static_cast(0)); ASSERT_EQ(rec.num_nodes(), static_cast(2)); ASSERT_EQ(rec.num_immediates(), static_cast(3)); @@ -59,25 +60,21 @@ TEST(CompressedSnapshotRecordTest, Append) { delete n1; } -TEST(CompressedSnapshotRecordTest, AppendEntrylist) { +TEST(CompressedSnapshotRecordTest, AppendEntrylist) +{ // setup mock attribute keys - cali::Node meta_db_nodes[] = { - { 100, 10, Variant(CALI_ATTR_ASVALUE) }, - { 1, 9, Variant(CALI_TYPE_INT) }, - { 3, 9, Variant(CALI_TYPE_STRING) }, - { 5, 9, Variant(CALI_TYPE_DOUBLE) }, - { 200, 8, Variant("int.attr") }, - { 201, 8, Variant("str.attr") }, - { 202, 8, Variant("dbl.attr") } - }; + cali::Node meta_db_nodes[] = { { 100, 10, Variant(CALI_ATTR_ASVALUE) }, { 1, 9, Variant(CALI_TYPE_INT) }, + { 3, 9, Variant(CALI_TYPE_STRING) }, { 5, 9, Variant(CALI_TYPE_DOUBLE) }, + { 200, 8, Variant("int.attr") }, { 201, 8, Variant("str.attr") }, + { 202, 8, Variant("dbl.attr") } }; meta_db_nodes[0].append(&meta_db_nodes[1]); meta_db_nodes[0].append(&meta_db_nodes[3]); meta_db_nodes[1].append(&meta_db_nodes[4]); meta_db_nodes[2].append(&meta_db_nodes[5]); meta_db_nodes[3].append(&meta_db_nodes[6]); - + MockupMetadataDB db; for (size_t i = 0; i < 7; ++i) @@ -103,20 +100,21 @@ TEST(CompressedSnapshotRecordTest, AppendEntrylist) { n1.append(&n2); n1.append(&n3); - std::vector entrylist { - Entry(&n2), - Entry(attr_in[0], data_in[0]), - Entry(&n3), - Entry(attr_in[1], data_in[1]), - Entry(attr_in[2], data_in[2]) - }; + std::vector entrylist { Entry(&n2), + Entry(attr_in[0], data_in[0]), + Entry(&n3), + Entry(attr_in[1], data_in[1]), + Entry(attr_in[2], data_in[2]) }; CompressedSnapshotRecord rec; ASSERT_EQ(rec.append(entrylist.size(), entrylist.data()), static_cast(0)); ASSERT_EQ(rec.num_nodes(), static_cast(2)); - ASSERT_EQ(rec.num_immediates(), static_cast(2)); // adding through entrylist skips invalid entry (attr_in[1]) + ASSERT_EQ( + rec.num_immediates(), + static_cast(2) + ); // adding through entrylist skips invalid entry (attr_in[1]) cali_id_t node_out[2]; cali_id_t attr_out[2]; @@ -137,25 +135,21 @@ TEST(CompressedSnapshotRecordTest, AppendEntrylist) { EXPECT_EQ(data_out[1], data_in[2]); // data_in[1] was skipped } -TEST(CompressedSnapshotRecordTest, Decode) { +TEST(CompressedSnapshotRecordTest, Decode) +{ // setup mock attribute keys - cali::Node meta_db_nodes[] = { - { 100, 10, Variant(CALI_ATTR_ASVALUE) }, - { 1, 9, Variant(CALI_TYPE_INT) }, - { 3, 9, Variant(CALI_TYPE_STRING) }, - { 5, 9, Variant(CALI_TYPE_DOUBLE) }, - { 200, 8, Variant("int.attr") }, - { 201, 8, Variant("str.attr") }, - { 202, 8, Variant("dbl.attr") } - }; + cali::Node meta_db_nodes[] = { { 100, 10, Variant(CALI_ATTR_ASVALUE) }, { 1, 9, Variant(CALI_TYPE_INT) }, + { 3, 9, Variant(CALI_TYPE_STRING) }, { 5, 9, Variant(CALI_TYPE_DOUBLE) }, + { 200, 8, Variant("int.attr") }, { 201, 8, Variant("str.attr") }, + { 202, 8, Variant("dbl.attr") } }; meta_db_nodes[0].append(&meta_db_nodes[1]); meta_db_nodes[0].append(&meta_db_nodes[3]); meta_db_nodes[1].append(&meta_db_nodes[4]); meta_db_nodes[2].append(&meta_db_nodes[5]); meta_db_nodes[3].append(&meta_db_nodes[6]); - + MockupMetadataDB db; for (size_t i = 0; i < 7; ++i) @@ -187,7 +181,7 @@ TEST(CompressedSnapshotRecordTest, Decode) { EXPECT_EQ(rec.append(1, attr_in, data_in), static_cast(0)); EXPECT_EQ(rec.append(2, node_in), static_cast(0)); - EXPECT_EQ(rec.append(2, attr_in+1, data_in+1), static_cast(0)); + EXPECT_EQ(rec.append(2, attr_in + 1, data_in + 1), static_cast(0)); ASSERT_EQ(rec.num_nodes(), static_cast(2)); ASSERT_EQ(rec.num_immediates(), static_cast(3)); @@ -196,7 +190,7 @@ TEST(CompressedSnapshotRecordTest, Decode) { cali_id_t attr_out[3]; Variant data_out[3]; - size_t pos; + size_t pos; CompressedSnapshotRecordView view(rec.data(), &pos); ASSERT_EQ(view.num_nodes(), rec.num_nodes()); @@ -217,25 +211,21 @@ TEST(CompressedSnapshotRecordTest, Decode) { EXPECT_EQ(data_out[2], data_in[2]); } -TEST(CompressedSnapshotRecordTest, MakeEntrylist) { +TEST(CompressedSnapshotRecordTest, MakeEntrylist) +{ // setup mock attribute keys - cali::Node meta_db_nodes[] = { - { 100, 10, Variant(CALI_ATTR_ASVALUE) }, - { 1, 9, Variant(CALI_TYPE_INT) }, - { 3, 9, Variant(CALI_TYPE_STRING) }, - { 5, 9, Variant(CALI_TYPE_DOUBLE) }, - { 200, 8, Variant("int.attr") }, - { 201, 8, Variant("str.attr") }, - { 202, 8, Variant("dbl.attr") } - }; + cali::Node meta_db_nodes[] = { { 100, 10, Variant(CALI_ATTR_ASVALUE) }, { 1, 9, Variant(CALI_TYPE_INT) }, + { 3, 9, Variant(CALI_TYPE_STRING) }, { 5, 9, Variant(CALI_TYPE_DOUBLE) }, + { 200, 8, Variant("int.attr") }, { 201, 8, Variant("str.attr") }, + { 202, 8, Variant("dbl.attr") } }; meta_db_nodes[0].append(&meta_db_nodes[1]); meta_db_nodes[0].append(&meta_db_nodes[3]); meta_db_nodes[1].append(&meta_db_nodes[4]); meta_db_nodes[2].append(&meta_db_nodes[5]); meta_db_nodes[3].append(&meta_db_nodes[6]); - + MockupMetadataDB db; for (size_t i = 0; i < 7; ++i) @@ -267,7 +257,7 @@ TEST(CompressedSnapshotRecordTest, MakeEntrylist) { EXPECT_EQ(rec.append(1, attr_in, data_in), static_cast(0)); EXPECT_EQ(rec.append(2, node_in), static_cast(0)); - EXPECT_EQ(rec.append(2, attr_in+1, data_in+1), static_cast(0)); + EXPECT_EQ(rec.append(2, attr_in + 1, data_in + 1), static_cast(0)); ASSERT_EQ(rec.num_nodes(), static_cast(2)); ASSERT_EQ(rec.num_immediates(), static_cast(3)); @@ -281,13 +271,11 @@ TEST(CompressedSnapshotRecordTest, MakeEntrylist) { // Assume order in list for the test for now: exploits // implementation detail that's not guaranteed by the interface - Entry list_in[] = { - Entry(&n2), - Entry(&n3), - Entry(int_attr, data_in[0]), - Entry(Attribute(), data_in[1]), - Entry(dbl_attr, data_in[2]) - }; + Entry list_in[] = { Entry(&n2), + Entry(&n3), + Entry(int_attr, data_in[0]), + Entry(Attribute(), data_in[1]), + Entry(dbl_attr, data_in[2]) }; for (size_t i = 0; i < 5; ++i) { EXPECT_TRUE(list_in[i] == list_out[i]) << " differs for entry " << i; @@ -296,70 +284,63 @@ TEST(CompressedSnapshotRecordTest, MakeEntrylist) { namespace { - class UnpackTester { - int m_count; - int m_max_count; - - std::vector m_in_list; +class UnpackTester +{ + int m_count; + int m_max_count; - void check_entry(const Entry& e) { - auto it = std::find(m_in_list.begin(), m_in_list.end(), e); + std::vector m_in_list; - EXPECT_FALSE(it == m_in_list.end()) << " Entry no " << m_count << " (" - << e.attribute() << "," - << e.value() - << ") not found!"; + void check_entry(const Entry& e) + { + auto it = std::find(m_in_list.begin(), m_in_list.end(), e); - if (it != m_in_list.end()) - m_in_list.erase(it); - } + EXPECT_FALSE(it == m_in_list.end()) + << " Entry no " << m_count << " (" << e.attribute() << "," << e.value() << ") not found!"; - public: + if (it != m_in_list.end()) + m_in_list.erase(it); + } - UnpackTester(size_t n, const Entry in_list[], int max_count) - : m_count(0), m_max_count(max_count) - { - for (size_t i = 0; i < n; ++i) - m_in_list.push_back(in_list[i]); - } +public: - int count() const { - return m_count; - } + UnpackTester(size_t n, const Entry in_list[], int max_count) : m_count(0), m_max_count(max_count) + { + for (size_t i = 0; i < n; ++i) + m_in_list.push_back(in_list[i]); + } - bool handle_entry(const Entry& e) { - ++m_count; + int count() const { return m_count; } - if (m_max_count > 0 && m_count >= m_max_count) - return false; + bool handle_entry(const Entry& e) + { + ++m_count; - check_entry(e); + if (m_max_count > 0 && m_count >= m_max_count) + return false; - return true; - } + check_entry(e); - }; -} + return true; + } +}; +} // namespace -TEST(CompressedSnapshotRecordTest, Unpack) { +TEST(CompressedSnapshotRecordTest, Unpack) +{ // setup mock attribute keys - cali::Node meta_db_nodes[] = { - { 100, 10, Variant(CALI_ATTR_ASVALUE) }, - { 1, 9, Variant(CALI_TYPE_INT) }, - { 3, 9, Variant(CALI_TYPE_STRING) }, - { 5, 9, Variant(CALI_TYPE_DOUBLE) }, - { 200, 8, Variant("int.attr") }, - { 201, 8, Variant("str.attr") }, - { 202, 8, Variant("dbl.attr") } - }; + cali::Node meta_db_nodes[] = { { 100, 10, Variant(CALI_ATTR_ASVALUE) }, { 1, 9, Variant(CALI_TYPE_INT) }, + { 3, 9, Variant(CALI_TYPE_STRING) }, { 5, 9, Variant(CALI_TYPE_DOUBLE) }, + { 200, 8, Variant("int.attr") }, { 201, 8, Variant("str.attr") }, + { 202, 8, Variant("dbl.attr") } }; meta_db_nodes[0].append(&meta_db_nodes[1]); meta_db_nodes[0].append(&meta_db_nodes[3]); meta_db_nodes[1].append(&meta_db_nodes[4]); meta_db_nodes[2].append(&meta_db_nodes[5]); meta_db_nodes[3].append(&meta_db_nodes[6]); - + MockupMetadataDB db; for (size_t i = 0; i < 7; ++i) @@ -391,32 +372,30 @@ TEST(CompressedSnapshotRecordTest, Unpack) { EXPECT_EQ(rec.append(1, attr_in, data_in), static_cast(0)); EXPECT_EQ(rec.append(2, node_in), static_cast(0)); - EXPECT_EQ(rec.append(2, attr_in+1, data_in+1), static_cast(0)); + EXPECT_EQ(rec.append(2, attr_in + 1, data_in + 1), static_cast(0)); ASSERT_EQ(rec.num_nodes(), static_cast(2)); ASSERT_EQ(rec.num_immediates(), static_cast(3)); db.add_nodes(2, node_in); - Entry list_in[5] = { - Entry(&n2), - Entry(&n3), - Entry(int_attr, data_in[0]), - Entry(Attribute(), data_in[1]), - Entry(dbl_attr, data_in[2]) - }; + Entry list_in[5] = { Entry(&n2), + Entry(&n3), + Entry(int_attr, data_in[0]), + Entry(Attribute(), data_in[1]), + Entry(dbl_attr, data_in[2]) }; CompressedSnapshotRecordView view(rec.view()); ::UnpackTester t1(5, list_in, -1); - view.unpack(&db, [&t1](const Entry& e){ return t1.handle_entry(e); }); + view.unpack(&db, [&t1](const Entry& e) { return t1.handle_entry(e); }); EXPECT_EQ(t1.count(), 5); ::UnpackTester t2(5, list_in, 2); - view.unpack(&db, [&t2](const Entry& e){ return t2.handle_entry(e); }); + view.unpack(&db, [&t2](const Entry& e) { return t2.handle_entry(e); }); EXPECT_EQ(t2.count(), 2); } diff --git a/src/common/test/test_runtimeconfig.cpp b/src/common/test/test_runtimeconfig.cpp index 1ddb59624..9981f30a1 100644 --- a/src/common/test/test_runtimeconfig.cpp +++ b/src/common/test/test_runtimeconfig.cpp @@ -7,38 +7,40 @@ using namespace cali; namespace { -const ConfigSet::Entry test_configdata[] = { - { "string_val", CALI_TYPE_STRING, "string-default", - "Description for the string config entry", - "Long description for the string config entry" - }, - { "list_val", CALI_TYPE_STRING, "first, second, \"third,but not fourth\"", - "Description of the list config entry", - "Long description for the list config entry" - }, - { "int_val", CALI_TYPE_INT, "1337", - "Description for the int config entry", - "Long description for the int config entry" - }, - { "another_int", CALI_TYPE_INT, "4242", - "Description for another int config entry", - "Long description for another int config entry" - }, - - ConfigSet::Terminator -}; - -} // namespace [anonymous] - - -TEST(RuntimeConfigTest, ConfigFile) { +const ConfigSet::Entry test_configdata[] = { { "string_val", + CALI_TYPE_STRING, + "string-default", + "Description for the string config entry", + "Long description for the string config entry" }, + { "list_val", + CALI_TYPE_STRING, + "first, second, \"third,but not fourth\"", + "Description of the list config entry", + "Long description for the list config entry" }, + { "int_val", + CALI_TYPE_INT, + "1337", + "Description for the int config entry", + "Long description for the int config entry" }, + { "another_int", + CALI_TYPE_INT, + "4242", + "Description for another int config entry", + "Long description for another int config entry" }, + + ConfigSet::Terminator }; + +} // namespace + +TEST(RuntimeConfigTest, ConfigFile) +{ RuntimeConfig cfg; - + cfg.set("CALI_CONFIG_FILE", "caliper-common_test.config"); - + cfg.preset("CALI_TEST_STRING_VAL", "wrong value!"); cfg.set("CALI_TEST_INT_VAL", "42"); - + ConfigSet config = cfg.init("test", ::test_configdata); EXPECT_EQ(config.get("string_val").to_string(), std::string("profile1 string from file")); @@ -46,12 +48,13 @@ TEST(RuntimeConfigTest, ConfigFile) { EXPECT_EQ(config.get("another_int").to_int(), 4242); } -TEST(RuntimeConfigTest, ConfigFileProfile2) { +TEST(RuntimeConfigTest, ConfigFileProfile2) +{ RuntimeConfig cfg; - + cfg.preset("CALI_CONFIG_FILE", "caliper-common_test.config"); cfg.set("CALI_CONFIG_PROFILE", "file-profile2"); - + ConfigSet config = cfg.init("test", ::test_configdata); EXPECT_EQ(config.get("string_val").to_string(), std::string("string-default")); diff --git a/src/common/test/test_snapshotbuffer.cpp b/src/common/test/test_snapshotbuffer.cpp index 84ef49c2e..0d0e1ac40 100644 --- a/src/common/test/test_snapshotbuffer.cpp +++ b/src/common/test/test_snapshotbuffer.cpp @@ -7,7 +7,8 @@ using namespace cali; -TEST(SnapshotBufferTest, Append) { +TEST(SnapshotBufferTest, Append) +{ cali_id_t attr_in[3] = { 7, CALI_INV_ID, 42 }; Variant data_in[3] = { Variant(CALI_TYPE_INT), Variant(), Variant(1.23) }; @@ -24,7 +25,7 @@ TEST(SnapshotBufferTest, Append) { EXPECT_EQ(in_rec_1.append(1, attr_in, data_in), static_cast(0)); EXPECT_EQ(in_rec_1.append(2, node_in), static_cast(0)); - EXPECT_EQ(in_rec_1.append(2, attr_in+1, data_in+1), static_cast(0)); + EXPECT_EQ(in_rec_1.append(2, attr_in + 1, data_in + 1), static_cast(0)); ASSERT_EQ(in_rec_1.num_nodes(), static_cast(2)); ASSERT_EQ(in_rec_1.num_immediates(), static_cast(3)); @@ -47,7 +48,7 @@ TEST(SnapshotBufferTest, Append) { CompressedSnapshotRecordView out_rec[2]; size_t pos = 0; - + for (size_t i = 0; i < 2; ++i) out_rec[i] = CompressedSnapshotRecordView(in_buf.data() + pos, &pos); @@ -94,7 +95,8 @@ TEST(SnapshotBufferTest, Append) { } } -TEST(SnapshotBufferTest, Import) { +TEST(SnapshotBufferTest, Import) +{ cali_id_t attr_in[3] = { 7, CALI_INV_ID, 42 }; Variant data_in[3] = { Variant(CALI_TYPE_INT), Variant(), Variant(1.23) }; @@ -111,7 +113,7 @@ TEST(SnapshotBufferTest, Import) { EXPECT_EQ(in_rec_1.append(1, attr_in, data_in), static_cast(0)); EXPECT_EQ(in_rec_1.append(2, node_in), static_cast(0)); - EXPECT_EQ(in_rec_1.append(2, attr_in+1, data_in+1), static_cast(0)); + EXPECT_EQ(in_rec_1.append(2, attr_in + 1, data_in + 1), static_cast(0)); ASSERT_EQ(in_rec_1.num_nodes(), static_cast(2)); ASSERT_EQ(in_rec_1.num_immediates(), static_cast(3)); @@ -140,7 +142,7 @@ TEST(SnapshotBufferTest, Import) { CompressedSnapshotRecordView out_rec[2]; size_t pos = 0; - + for (size_t i = 0; i < 2; ++i) out_rec[i] = CompressedSnapshotRecordView(out_buf.data() + pos, &pos); diff --git a/src/common/test/test_snapshottextformatter.cpp b/src/common/test/test_snapshottextformatter.cpp index 2fbd80022..a00b336c9 100644 --- a/src/common/test/test_snapshottextformatter.cpp +++ b/src/common/test/test_snapshottextformatter.cpp @@ -8,16 +8,17 @@ using namespace cali; -TEST(SnapshotTextFormatterTest, FormatTest) { +TEST(SnapshotTextFormatterTest, FormatTest) +{ Node* strtype_attr_n = new Node(3, 9, Variant(CALI_TYPE_STRING)); Node* inttype_attr_n = new Node(1, 9, Variant(CALI_TYPE_INT)); - + Node* str_attr = new Node(100, 8, Variant(CALI_TYPE_STRING, "str.attr", 9)); Node* int_attr = new Node(101, 8, Variant(CALI_TYPE_STRING, "int.attr", 9)); strtype_attr_n->append(str_attr); inttype_attr_n->append(int_attr); - + MockupMetadataDB db; db.add_attribute(Attribute::make_attribute(str_attr)); @@ -39,7 +40,7 @@ TEST(SnapshotTextFormatterTest, FormatTest) { EXPECT_EQ(os.str(), std::string("whoo whee-whee42-whee - 42-end")); } - + format.reset("%[1]int.attr%%str.attr%"); { @@ -47,5 +48,5 @@ TEST(SnapshotTextFormatterTest, FormatTest) { format.print(os, db, { Entry(int_node) }); EXPECT_EQ(os.str(), std::string("42whee")); - } + } } diff --git a/src/common/test/test_stringconverter.cpp b/src/common/test/test_stringconverter.cpp index af5e15350..0bf850014 100644 --- a/src/common/test/test_stringconverter.cpp +++ b/src/common/test/test_stringconverter.cpp @@ -6,7 +6,8 @@ using namespace cali; -TEST(StringConverterTest, ConvertBool) { +TEST(StringConverterTest, ConvertBool) +{ StringConverter strconv_in_1(std::string("tRue")); StringConverter strconv_in_2(std::string("faLse")); StringConverter strconv_in_3; @@ -50,63 +51,66 @@ TEST(StringConverterTest, ConvertBool) { EXPECT_EQ(res_7, true); } -TEST(StringConverterTest, ConvertInt) { +TEST(StringConverterTest, ConvertInt) +{ bool ok = false; StringConverter strconv("42"); - int res = strconv.to_int(&ok); + int res = strconv.to_int(&ok); EXPECT_TRUE(ok); EXPECT_EQ(res, 42); - ok = false; + ok = false; strconv = StringConverter("0"); - res = strconv.to_int(&ok); + res = strconv.to_int(&ok); EXPECT_TRUE(ok); EXPECT_EQ(res, 0); - ok = false; + ok = false; strconv = StringConverter(" -14 "); - res = strconv.to_int(&ok); + res = strconv.to_int(&ok); EXPECT_TRUE(ok); EXPECT_EQ(res, -14); - ok = false; + ok = false; strconv = StringConverter("bla"); - res = strconv.to_int(&ok); + res = strconv.to_int(&ok); EXPECT_FALSE(ok); - ok = false; + ok = false; strconv = StringConverter(); - res = strconv.to_int(&ok); + res = strconv.to_int(&ok); EXPECT_FALSE(ok); } -TEST(StringConverterTest, ConvertUint) { +TEST(StringConverterTest, ConvertUint) +{ bool ok = false; StringConverter strconv("42"); - unsigned long res = strconv.to_uint(&ok); + unsigned long res = strconv.to_uint(&ok); EXPECT_TRUE(ok); EXPECT_EQ(res, 42); - ok = false; + ok = false; strconv = StringConverter("0"); - res = strconv.to_uint(&ok); + res = strconv.to_uint(&ok); EXPECT_TRUE(ok); EXPECT_EQ(res, 0); - ok = false; + ok = false; strconv = StringConverter("bla"); - res = strconv.to_uint(&ok); + res = strconv.to_uint(&ok); EXPECT_FALSE(ok); - ok = false; + ok = false; strconv = StringConverter(); - res = strconv.to_uint(&ok); + res = strconv.to_uint(&ok); EXPECT_FALSE(ok); } -TEST(StringConverterTest, ConvertStringList) { +TEST(StringConverterTest, ConvertStringList) +{ bool ok = false; StringConverter sc(" aword, b.c:cdef,, d, e\\ f\",gword \" , "); @@ -123,13 +127,14 @@ TEST(StringConverterTest, ConvertStringList) { EXPECT_EQ(res[4], std::string("e f,gword ")); } -TEST(StringConverterTest, JsonList) { +TEST(StringConverterTest, JsonList) +{ bool ok = false; - StringConverter sc("[ a, [ b, c ] , { \"this\": [ is, \"a , nested\" , list ]}, \" a string with \\\"]\\\" \" ] "); + StringConverter sc("[ a, [ b, c ] , { \"this\": [ is, \"a , nested\" , list ]}, \" a string with \\\"]\\\" \" ] " + ); - std::vector res = - sc.rec_list(&ok); + std::vector res = sc.rec_list(&ok); EXPECT_TRUE(ok); ASSERT_EQ(res.size(), 4u); @@ -139,15 +144,13 @@ TEST(StringConverterTest, JsonList) { EXPECT_EQ(res[2].to_string(), std::string("{ \"this\": [ is, \"a , nested\" , list ]}")); EXPECT_EQ(res[3].to_string(), std::string(" a string with \"]\" ")); - std::map kv = - res[2].rec_dict(&ok); + std::map kv = res[2].rec_dict(&ok); EXPECT_TRUE(ok); EXPECT_EQ(kv["this"].to_string(), std::string("[ is, \"a , nested\" , list ]")); - std::vector nested_res = - kv["this"].rec_list(&ok); + std::vector nested_res = kv["this"].rec_list(&ok); EXPECT_TRUE(ok); ASSERT_EQ(nested_res.size(), 3u); @@ -156,21 +159,20 @@ TEST(StringConverterTest, JsonList) { EXPECT_EQ(nested_res[1].to_string(), std::string("a , nested")); EXPECT_EQ(nested_res[2].to_string(), std::string("list")); - StringConverter sce("[ ]"); - std::vector e_res = - sce.rec_list(&ok); + StringConverter sce("[ ]"); + std::vector e_res = sce.rec_list(&ok); EXPECT_TRUE(ok); EXPECT_EQ(e_res.size(), 0u); } -TEST(StringConverterTest, JsonDict) { +TEST(StringConverterTest, JsonDict) +{ bool ok = false; StringConverter sc("{ \"aa\": { b : [ c, \"d }\", e], ff: \"gg\"},x:y } blagarbl "); - std::map dict = - sc.rec_dict(&ok); + std::map dict = sc.rec_dict(&ok); EXPECT_TRUE(ok); EXPECT_EQ(dict.size(), 2u); diff --git a/src/common/test/test_variant.cpp b/src/common/test/test_variant.cpp index bd76b9a9d..26ca9c054 100644 --- a/src/common/test/test_variant.cpp +++ b/src/common/test/test_variant.cpp @@ -8,46 +8,45 @@ using namespace cali; // Test some type conversions -TEST(Variant_Test, FromString) { +TEST(Variant_Test, FromString) +{ const char* teststr = "My wonderful test string"; - uint64_t uval = 0xef10; - int64_t i64val = -9876543210; + uint64_t uval = 0xef10; + int64_t i64val = -9876543210; const struct testcase_t { cali_attr_type type; const char* str; bool ok; Variant expected; - } testcases[] = { - { CALI_TYPE_INV, "42", false, Variant() }, + } testcases[] = { { CALI_TYPE_INV, "42", false, Variant() }, - { CALI_TYPE_INT, "42", true, Variant(static_cast(42)) }, - { CALI_TYPE_INT, " -10 ", true, Variant(static_cast(-10)) }, - { CALI_TYPE_INT, "bla", false, Variant() }, + { CALI_TYPE_INT, "42", true, Variant(static_cast(42)) }, + { CALI_TYPE_INT, " -10 ", true, Variant(static_cast(-10)) }, + { CALI_TYPE_INT, "bla", false, Variant() }, - { CALI_TYPE_INT, "-9876543210", true, Variant(cali_make_variant_from_int64(i64val)) }, + { CALI_TYPE_INT, "-9876543210", true, Variant(cali_make_variant_from_int64(i64val)) }, - { CALI_TYPE_STRING, teststr, true, Variant(CALI_TYPE_STRING, teststr, strlen(teststr)) }, - { CALI_TYPE_STRING, teststr, true, Variant(teststr) }, - { CALI_TYPE_STRING, "", true, Variant(CALI_TYPE_STRING, "", 0) }, + { CALI_TYPE_STRING, teststr, true, Variant(CALI_TYPE_STRING, teststr, strlen(teststr)) }, + { CALI_TYPE_STRING, teststr, true, Variant(teststr) }, + { CALI_TYPE_STRING, "", true, Variant(CALI_TYPE_STRING, "", 0) }, - { CALI_TYPE_UINT, "0", true, Variant(static_cast(0)) }, - { CALI_TYPE_UINT, "1337", true, Variant(static_cast(1337)) }, - { CALI_TYPE_ADDR, "ef10", true, Variant(CALI_TYPE_ADDR, &uval, sizeof(uval)) }, + { CALI_TYPE_UINT, "0", true, Variant(static_cast(0)) }, + { CALI_TYPE_UINT, "1337", true, Variant(static_cast(1337)) }, + { CALI_TYPE_ADDR, "ef10", true, Variant(CALI_TYPE_ADDR, &uval, sizeof(uval)) }, - { CALI_TYPE_DOUBLE, "-1.0", true, Variant(static_cast(-1.0)) }, + { CALI_TYPE_DOUBLE, "-1.0", true, Variant(static_cast(-1.0)) }, - { CALI_TYPE_BOOL, "false", true, Variant(false) }, - { CALI_TYPE_BOOL, "1", true, Variant(true) }, - { CALI_TYPE_BOOL, "bla", false, Variant() }, + { CALI_TYPE_BOOL, "false", true, Variant(false) }, + { CALI_TYPE_BOOL, "1", true, Variant(true) }, + { CALI_TYPE_BOOL, "bla", false, Variant() }, - { CALI_TYPE_TYPE, "int", true, Variant(CALI_TYPE_INT) }, - { CALI_TYPE_TYPE, "bla", false, Variant() }, + { CALI_TYPE_TYPE, "int", true, Variant(CALI_TYPE_INT) }, + { CALI_TYPE_TYPE, "bla", false, Variant() }, - { CALI_TYPE_PTR, "0", false, Variant() }, + { CALI_TYPE_PTR, "0", false, Variant() }, - { CALI_TYPE_INV, 0, false, Variant() } - }; + { CALI_TYPE_INV, 0, false, Variant() } }; for (const testcase_t* t = testcases; t->str; ++t) { Variant v(Variant::from_string(t->type, t->str)); @@ -57,25 +56,27 @@ TEST(Variant_Test, FromString) { } } -TEST(Variant_Test, UintOverloads) { - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_UINT ); - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_UINT ); - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_UINT ); - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_UINT ); - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_UINT ); - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_INT ); - EXPECT_EQ( Variant( static_cast(42) ).type(), CALI_TYPE_INT ); - EXPECT_EQ( Variant( static_cast(4.2) ).type(), CALI_TYPE_DOUBLE ); - EXPECT_EQ( Variant( static_cast(0) ).type(), CALI_TYPE_BOOL ); - EXPECT_EQ( Variant( CALI_TYPE_STRING ).type(), CALI_TYPE_TYPE ); +TEST(Variant_Test, UintOverloads) +{ + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_UINT); + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_UINT); + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_UINT); + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_UINT); + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_UINT); + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_INT); + EXPECT_EQ(Variant(static_cast(42)).type(), CALI_TYPE_INT); + EXPECT_EQ(Variant(static_cast(4.2)).type(), CALI_TYPE_DOUBLE); + EXPECT_EQ(Variant(static_cast(0)).type(), CALI_TYPE_BOOL); + EXPECT_EQ(Variant(CALI_TYPE_STRING).type(), CALI_TYPE_TYPE); } -TEST(Variant_Test, Conversions) { +TEST(Variant_Test, Conversions) +{ int val_1_int_neg = -27; int64_t val_2_i64_nlrg = -9876543210; uint64_t val_3_uint_sml = 42; uint64_t val_4_uint_lrg = 0xFFFFFFFFFFAA; - int val_5_zero = 0; + int val_5_zero = 0; cali::Variant v_1_int_neg(val_1_int_neg); cali::Variant v_2_i64_nlrg(cali_make_variant_from_int64(val_2_i64_nlrg)); @@ -83,7 +84,7 @@ TEST(Variant_Test, Conversions) { cali::Variant v_4_uint_lrg(val_4_uint_lrg); cali::Variant v_5_int_zero(val_5_zero); - EXPECT_EQ(v_1_int_neg.type(), CALI_TYPE_INT); + EXPECT_EQ(v_1_int_neg.type(), CALI_TYPE_INT); EXPECT_EQ(v_2_i64_nlrg.type(), CALI_TYPE_INT); EXPECT_EQ(v_3_uint_sml.type(), CALI_TYPE_UINT); EXPECT_EQ(v_4_uint_lrg.type(), CALI_TYPE_UINT); @@ -91,13 +92,13 @@ TEST(Variant_Test, Conversions) { { bool v_1_int_neg_to_int = false; - int val_int = v_1_int_neg.to_int(&v_1_int_neg_to_int); + int val_int = v_1_int_neg.to_int(&v_1_int_neg_to_int); EXPECT_TRUE(v_1_int_neg_to_int); EXPECT_EQ(val_int, val_1_int_neg); } { - bool v_1_int_neg_to_i64 = false; - int64_t val_i64 = v_1_int_neg.to_int64(&v_1_int_neg_to_i64); + bool v_1_int_neg_to_i64 = false; + int64_t val_i64 = v_1_int_neg.to_int64(&v_1_int_neg_to_i64); EXPECT_TRUE(v_1_int_neg_to_i64); EXPECT_EQ(val_i64, static_cast(val_1_int_neg)); } @@ -113,8 +114,8 @@ TEST(Variant_Test, Conversions) { EXPECT_FALSE(v_2_i64_nlrg_to_int); } { - bool v_2_i64_nlrg_to_i64 = false; - int64_t val_i64 = v_2_i64_nlrg.to_int64(&v_2_i64_nlrg_to_i64); + bool v_2_i64_nlrg_to_i64 = false; + int64_t val_i64 = v_2_i64_nlrg.to_int64(&v_2_i64_nlrg_to_i64); EXPECT_TRUE(v_2_i64_nlrg_to_i64); EXPECT_EQ(val_i64, val_2_i64_nlrg); } @@ -126,25 +127,25 @@ TEST(Variant_Test, Conversions) { { bool v_3_uint_sml_to_int = false; - int val_int = v_3_uint_sml.to_int(&v_3_uint_sml_to_int); + int val_int = v_3_uint_sml.to_int(&v_3_uint_sml_to_int); EXPECT_TRUE(v_3_uint_sml_to_int); EXPECT_EQ(val_int, static_cast(val_3_uint_sml)); } { - bool v_3_uint_sml_to_i64 = false; - int64_t val_i64 = v_3_uint_sml.to_int64(&v_3_uint_sml_to_i64); + bool v_3_uint_sml_to_i64 = false; + int64_t val_i64 = v_3_uint_sml.to_int64(&v_3_uint_sml_to_i64); EXPECT_TRUE(v_3_uint_sml_to_i64); EXPECT_EQ(val_i64, static_cast(val_3_uint_sml)); } { - bool v_3_uint_sml_to_uint = false; - uint64_t val_uint = v_3_uint_sml.to_uint(&v_3_uint_sml_to_uint); + bool v_3_uint_sml_to_uint = false; + uint64_t val_uint = v_3_uint_sml.to_uint(&v_3_uint_sml_to_uint); EXPECT_TRUE(v_3_uint_sml_to_uint); EXPECT_EQ(val_uint, val_3_uint_sml); } { bool v_3_uint_sml_to_bool = false; - bool val_bool = v_3_uint_sml.to_bool(&v_3_uint_sml_to_bool); + bool val_bool = v_3_uint_sml.to_bool(&v_3_uint_sml_to_bool); EXPECT_TRUE(v_3_uint_sml_to_bool); EXPECT_TRUE(val_bool); } @@ -155,99 +156,99 @@ TEST(Variant_Test, Conversions) { EXPECT_FALSE(v_4_uint_lrg_to_int); } { - bool v_4_uint_lrg_to_uint = false; - uint64_t val_uint = v_4_uint_lrg.to_uint(&v_4_uint_lrg_to_uint); + bool v_4_uint_lrg_to_uint = false; + uint64_t val_uint = v_4_uint_lrg.to_uint(&v_4_uint_lrg_to_uint); EXPECT_TRUE(v_4_uint_lrg_to_uint); EXPECT_EQ(val_uint, val_4_uint_lrg); } { bool v_5_zero_to_int = false; - int val_int = v_5_int_zero.to_int(&v_5_zero_to_int); + int val_int = v_5_int_zero.to_int(&v_5_zero_to_int); EXPECT_TRUE(v_5_zero_to_int); EXPECT_EQ(val_int, val_5_zero); } { - bool v_5_zero_to_i64 = false; - int64_t val_i64 = v_5_int_zero.to_int64(&v_5_zero_to_i64); + bool v_5_zero_to_i64 = false; + int64_t val_i64 = v_5_int_zero.to_int64(&v_5_zero_to_i64); EXPECT_TRUE(v_5_zero_to_i64); EXPECT_EQ(val_i64, static_cast(val_5_zero)); } { - bool v_5_zero_to_uint = false; - uint64_t val_uint = v_5_int_zero.to_uint(&v_5_zero_to_uint); + bool v_5_zero_to_uint = false; + uint64_t val_uint = v_5_int_zero.to_uint(&v_5_zero_to_uint); EXPECT_TRUE(v_5_zero_to_uint); EXPECT_EQ(val_uint, static_cast(val_5_zero)); } { bool v_5_zero_to_bool = false; - bool val_bool = v_5_int_zero.to_bool(&v_5_zero_to_bool); + bool val_bool = v_5_int_zero.to_bool(&v_5_zero_to_bool); EXPECT_TRUE(v_5_zero_to_bool); EXPECT_FALSE(val_bool); } - } // --- Test Variant pack/unpack -TEST(Variant_Test, PackUnpack) { - int val_1_int = -27; - uint64_t val_2_uint = 0xFFFFFFFFAA; - const char* val_3_str = "My wonderful test string"; - double val_4_dbl = 42.42; +TEST(Variant_Test, PackUnpack) +{ + int val_1_int = -27; + uint64_t val_2_uint = 0xFFFFFFFFAA; + const char* val_3_str = "My wonderful test string"; + double val_4_dbl = 42.42; // const void* val_5_inv = NULL; cali_attr_type val_6_type = CALI_TYPE_ADDR; bool val_7_bool = true; void* val_8_ptr = this; int64_t val_9_i64 = -9876543210; - cali::Variant v_1_int_in(val_1_int); - cali::Variant v_2_uint_in(CALI_TYPE_UINT, &val_2_uint, sizeof(uint64_t)); - cali::Variant v_3_str_in(CALI_TYPE_STRING, val_3_str, strlen(val_3_str)+1); - cali::Variant v_4_dbl_in(val_4_dbl); - cali::Variant v_5_inv_in; - cali::Variant v_6_type_in(val_6_type); - cali::Variant v_7_bool_in(val_7_bool); - cali::Variant v_8_ptr_in(cali_make_variant_from_ptr(val_8_ptr)); - cali::Variant v_9_i64_in(CALI_TYPE_INT, &val_9_i64, sizeof(int64_t)); + cali::Variant v_1_int_in(val_1_int); + cali::Variant v_2_uint_in(CALI_TYPE_UINT, &val_2_uint, sizeof(uint64_t)); + cali::Variant v_3_str_in(CALI_TYPE_STRING, val_3_str, strlen(val_3_str) + 1); + cali::Variant v_4_dbl_in(val_4_dbl); + cali::Variant v_5_inv_in; + cali::Variant v_6_type_in(val_6_type); + cali::Variant v_7_bool_in(val_7_bool); + cali::Variant v_8_ptr_in(cali_make_variant_from_ptr(val_8_ptr)); + cali::Variant v_9_i64_in(CALI_TYPE_INT, &val_9_i64, sizeof(int64_t)); unsigned char buf[256]; // must be >= 9*22 = 198 bytes - size_t pos = 0; + size_t pos = 0; memset(buf, 0, 256); - pos += v_1_int_in.pack(buf+pos); - pos += v_2_uint_in.pack(buf+pos); - pos += v_3_str_in.pack(buf+pos); - pos += v_4_dbl_in.pack(buf+pos); - pos += v_5_inv_in.pack(buf+pos); - pos += v_6_type_in.pack(buf+pos); - pos += v_7_bool_in.pack(buf+pos); - pos += v_8_ptr_in.pack(buf+pos); - pos += v_9_i64_in.pack(buf+pos); + pos += v_1_int_in.pack(buf + pos); + pos += v_2_uint_in.pack(buf + pos); + pos += v_3_str_in.pack(buf + pos); + pos += v_4_dbl_in.pack(buf + pos); + pos += v_5_inv_in.pack(buf + pos); + pos += v_6_type_in.pack(buf + pos); + pos += v_7_bool_in.pack(buf + pos); + pos += v_8_ptr_in.pack(buf + pos); + pos += v_9_i64_in.pack(buf + pos); EXPECT_LE(pos, 198); bool ok = false; - pos = 0; + pos = 0; - Variant v_1_int_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_1_int_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_1 unpack (int)"); - Variant v_2_uint_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_2_uint_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_2 unpack (uint)"); - Variant v_3_str_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_3_str_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_3 unpack (string)"); - Variant v_4_dbl_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_4_dbl_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_4 unpack (double)"); - Variant v_5_inv_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_5_inv_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_5 unpack (inv)"); - Variant v_6_type_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_6_type_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_6 unpack (type)"); - Variant v_7_bool_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_7_bool_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_7 unpack (bool)"); - Variant v_8_ptr_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_8_ptr_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_8 unpack (ptr)"); - Variant v_9_i64_out = Variant::unpack(buf+pos, &pos, &ok); + Variant v_9_i64_out = Variant::unpack(buf + pos, &pos, &ok); EXPECT_TRUE(ok && "v_9 unpack (int64)"); EXPECT_EQ(v_1_int_out.type(), CALI_TYPE_INT); @@ -259,7 +260,7 @@ TEST(Variant_Test, PackUnpack) { EXPECT_EQ(v_2_uint_in, v_2_uint_out); EXPECT_EQ(v_3_str_out.type(), CALI_TYPE_STRING); - EXPECT_EQ(v_3_str_out.size(), strlen(val_3_str)+1); + EXPECT_EQ(v_3_str_out.size(), strlen(val_3_str) + 1); EXPECT_EQ(v_3_str_out.data(), static_cast(val_3_str)); EXPECT_EQ(v_3_str_out.to_string(), std::string(val_3_str)); EXPECT_EQ(v_3_str_in, v_3_str_out); diff --git a/src/common/util/demangle.cpp b/src/common/util/demangle.cpp index 24ff09c9f..8864f63a3 100644 --- a/src/common/util/demangle.cpp +++ b/src/common/util/demangle.cpp @@ -24,7 +24,7 @@ std::string demangle(const char* name) return result; char* demangled = nullptr; - int status = -1; + int status = -1; if (name[0] == '_' && name[1] == 'Z') demangled = abi::__cxa_demangle(name, nullptr, 0, &status); diff --git a/src/common/util/demangle.h b/src/common/util/demangle.h index fdf357828..e5aaee3cb 100644 --- a/src/common/util/demangle.h +++ b/src/common/util/demangle.h @@ -17,6 +17,6 @@ namespace util /// \brief Return demangled name for C++ binary identifier \a name std::string demangle(const char* name); -} +} // namespace util #endif diff --git a/src/common/util/file_util.cpp b/src/common/util/file_util.cpp index d44fda6ff..f1bba0036 100644 --- a/src/common/util/file_util.cpp +++ b/src/common/util/file_util.cpp @@ -24,20 +24,21 @@ string random_string(string::size_type len) { static std::mt19937 rgen(chrono::system_clock::now().time_since_epoch().count()); - static const char characters[] = "0123456789" + static const char characters[] = + "0123456789" "abcdefghiyklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXZY"; - std::uniform_int_distribution<> random(0, sizeof(characters)-2); + std::uniform_int_distribution<> random(0, sizeof(characters) - 2); string s(len, '-'); - generate_n(s.begin(), len, [&](){ return characters[random(rgen)]; }); + generate_n(s.begin(), len, [&]() { return characters[random(rgen)]; }); return s; } -} // namespace [anonymous] +} // namespace namespace cali { @@ -51,7 +52,7 @@ std::string create_filename(const char* ext) time_t tm = chrono::system_clock::to_time_t(chrono::system_clock::now()); strftime(timestring, sizeof(timestring), "%y%m%d-%H%M%S", localtime(&tm)); - int pid = static_cast(getpid()); + int pid = static_cast(getpid()); return string(timestring) + "_" + std::to_string(pid) + "_" + random_string(12) + ext; } diff --git a/src/common/util/format_util.cpp b/src/common/util/format_util.cpp index fb3965501..80e551655 100644 --- a/src/common/util/format_util.cpp +++ b/src/common/util/format_util.cpp @@ -6,14 +6,13 @@ namespace { -const char whitespace[80+1] = +const char whitespace[80 + 1] = " " " "; } -std::ostream& -util::pad_right(std::ostream& os, const std::string& str, std::size_t width) +std::ostream& util::pad_right(std::ostream& os, const std::string& str, std::size_t width) { os << str; @@ -22,34 +21,32 @@ util::pad_right(std::ostream& os, const std::string& str, std::size_t width) else { std::size_t s = 1 + width - str.size(); - for ( ; s > 80; s -= 80) + for (; s > 80; s -= 80) os << ::whitespace; - os << ::whitespace+(80-s); + os << ::whitespace + (80 - s); } - + return os; } -std::ostream& -util::pad_left (std::ostream& os, const std::string& str, std::size_t width) +std::ostream& util::pad_left(std::ostream& os, const std::string& str, std::size_t width) { if (str.size() < width) { std::size_t s = width - str.size(); - for ( ; s > 80; s -= 80) + for (; s > 80; s -= 80) os << ::whitespace; - os << ::whitespace+(80-s); + os << ::whitespace + (80 - s); } os << str << ' '; - + return os; } -std::string -util::clamp_string(const std::string& str, std::size_t max_width) +std::string util::clamp_string(const std::string& str, std::size_t max_width) { if (str.length() <= max_width) return str; @@ -58,10 +55,10 @@ util::clamp_string(const std::string& str, std::size_t max_width) std::string ret; ret.reserve(1 + max_width); - - ret.append(str, 0, max_width/2-1); + + ret.append(str, 0, max_width / 2 - 1); ret.append("~~"); - ret.append(str, str.length()-(max_width/2-1), std::string::npos); + ret.append(str, str.length() - (max_width / 2 - 1), std::string::npos); return ret; } diff --git a/src/common/util/format_util.h b/src/common/util/format_util.h index 309a7bcbe..7d40501f7 100644 --- a/src/common/util/format_util.h +++ b/src/common/util/format_util.h @@ -14,23 +14,21 @@ namespace util { /// \brief Write a uint64_t \a value into \a os -inline std::ostream& -write_uint64(std::ostream& os, uint64_t value) +inline std::ostream& write_uint64(std::ostream& os, uint64_t value) { - char buf[24]; // max uint64 formatted length is 20 + char buf[24]; // max uint64 formatted length is 20 unsigned p = 24; do { buf[--p] = (static_cast(value % 10) + '0'); value /= 10; } while (value > 0); - os.write(buf+p, 24-p); + os.write(buf + p, 24 - p); return os; } /// \brief Write string \a str to \a os, /// escaping all characters in \a mask_chars with \a esc. -inline std::ostream& -write_json_esc_string(std::ostream& os, const char* str, std::string::size_type size) +inline std::ostream& write_json_esc_string(std::ostream& os, const char* str, std::string::size_type size) { for (size_t i = 0; i < size; ++i) { const char c = str[i]; @@ -39,7 +37,7 @@ write_json_esc_string(std::ostream& os, const char* str, std::string::size_type os.put('\\'); os.put('n'); } - if (c < 0x20) // skip control characters + if (c < 0x20) // skip control characters continue; if (c == '\\' || c == '\"') os.put('\\'); @@ -52,17 +50,16 @@ write_json_esc_string(std::ostream& os, const char* str, std::string::size_type /// \brief Write string \a str to \a os, /// escaping all characters in \a mask_chars with \a esc. -inline std::ostream& -write_cali_esc_string(std::ostream& os, const char* str, std::string::size_type size) +inline std::ostream& write_cali_esc_string(std::ostream& os, const char* str, std::string::size_type size) { for (size_t i = 0; i < size; ++i) { const char c = str[i]; - if (c == '\n') {// handle newline in string + if (c == '\n') { // handle newline in string os.put('\\'); os.put('n'); } - if (c < 0x20) // skip control characters + if (c < 0x20) // skip control characters continue; if (c == '\\' || c == ',' || c == '=') os.put('\\'); @@ -73,25 +70,20 @@ write_cali_esc_string(std::ostream& os, const char* str, std::string::size_type return os; } -inline std::ostream& -write_json_esc_string(std::ostream& os, const std::string& str) +inline std::ostream& write_json_esc_string(std::ostream& os, const std::string& str) { return write_json_esc_string(os, str.data(), str.size()); } -inline std::ostream& -write_cali_esc_string(std::ostream& os, const std::string& str) +inline std::ostream& write_cali_esc_string(std::ostream& os, const std::string& str) { return write_cali_esc_string(os, str.data(), str.size()); } -std::ostream& -pad_right(std::ostream& os, const std::string& str, std::size_t width); +std::ostream& pad_right(std::ostream& os, const std::string& str, std::size_t width); -std::ostream& -pad_left (std::ostream& os, const std::string& str, std::size_t width); +std::ostream& pad_left(std::ostream& os, const std::string& str, std::size_t width); -std::string -clamp_string(const std::string& str, std::size_t max_width); +std::string clamp_string(const std::string& str, std::size_t max_width); } // namespace util diff --git a/src/common/util/parse_util.cpp b/src/common/util/parse_util.cpp index 95b2c5e8e..1ff9bb6d5 100644 --- a/src/common/util/parse_util.cpp +++ b/src/common/util/parse_util.cpp @@ -5,11 +5,10 @@ #include -std::string -util::read_word(std::istream& is, const char* separators) +std::string util::read_word(std::istream& is, const char* separators) { std::string ret; - char c; + char c; do { c = is.get(); @@ -47,12 +46,11 @@ util::read_word(std::istream& is, const char* separators) return ret; } -std::string -util::read_nested_text(std::istream& is, char start_char, char end_char) +std::string util::read_nested_text(std::istream& is, char start_char, char end_char) { std::string ret; - bool esc = false; - int depth = 1; + bool esc = false; + int depth = 1; while (is.good()) { char c = is.get(); @@ -86,8 +84,7 @@ util::read_nested_text(std::istream& is, char start_char, char end_char) return ret; } -char -util::read_char(std::istream& is) +char util::read_char(std::istream& is) { char c = '\0'; diff --git a/src/common/util/parse_util.h b/src/common/util/parse_util.h index 5638c31d0..ec9e71496 100644 --- a/src/common/util/parse_util.h +++ b/src/common/util/parse_util.h @@ -14,8 +14,7 @@ namespace util { /// \brief Check if \a c is in \a separators -inline bool -is_one_of(char c, const char* characters) +inline bool is_one_of(char c, const char* characters) { for (const char* ptr = characters; *ptr != '\0'; ++ptr) if (*ptr == c) @@ -26,26 +25,22 @@ is_one_of(char c, const char* characters) /// \brief Read from stream \a is until whitespace or one of the characters in /// \a separators is found. -std::string -read_word(std::istream& is, const char* separators = ","); +std::string read_word(std::istream& is, const char* separators = ","); /// \brief Reads text within (start_char, end_char) region, skipping /// over any such regions nested within. -std::string -read_nested_text(std::istream& is, char start_char, char end_char); +std::string read_nested_text(std::istream& is, char start_char, char end_char); /// \brief Read character from stream \a is, skipping whitespace. -char -read_char(std::istream& is); +char read_char(std::istream& is); -inline std::pair -str_to_uint64(const char* str) +inline std::pair str_to_uint64(const char* str) { - uint64_t ret = 0; - const char* p = str; - for ( ; *p >= '0' && *p <= '9'; ++p) + uint64_t ret = 0; + const char* p = str; + for (; *p >= '0' && *p <= '9'; ++p) ret = ret * 10 + static_cast(*p - '0'); return std::make_pair(p != str, ret); } -} +} // namespace util diff --git a/src/common/util/spinlock.hpp b/src/common/util/spinlock.hpp index 8f655ea80..b73960231 100644 --- a/src/common/util/spinlock.hpp +++ b/src/common/util/spinlock.hpp @@ -11,25 +11,24 @@ namespace util { - -class spinlock { + +class spinlock +{ std::atomic_flag m_lock = ATOMIC_FLAG_INIT; public: - spinlock() - { } + spinlock() {} - void lock() { + void lock() + { while (m_lock.test_and_set(std::memory_order_acquire)) ; } - void unlock() { - m_lock.clear(std::memory_order_release); - } + void unlock() { m_lock.clear(std::memory_order_release); } }; -} // namespace +} // namespace util #endif diff --git a/src/common/util/split.hpp b/src/common/util/split.hpp index f08ea2571..489cb1c57 100644 --- a/src/common/util/split.hpp +++ b/src/common/util/split.hpp @@ -10,12 +10,12 @@ namespace util { -template +template void split(String input, Char sep, OutputIterator out) { String str; - for ( Char c : input ) { + for (Char c : input) { if (c == sep) { *out++ = str; str.clear(); @@ -28,16 +28,17 @@ void split(String input, Char sep, OutputIterator out) *out++ = str; } -template +template void tokenize(String input, const Char* tokens, OutputIterator out) { String str; - for ( Char c : input ) { + for (Char c : input) { bool is_token_char = false; - for ( const Char* tc = tokens; *tc; ++tc ) + for (const Char* tc = tokens; *tc; ++tc) if (c == *tc) { - is_token_char = true; break; + is_token_char = true; + break; } if (is_token_char) { diff --git a/src/common/util/unitfmt.c b/src/common/util/unitfmt.c index bdda873a6..dfa929b66 100644 --- a/src/common/util/unitfmt.c +++ b/src/common/util/unitfmt.c @@ -31,24 +31,21 @@ #include "unitfmt.h" -const struct unitfmt_table unitfmt_bytes[] = { - { 1, "Byte(s)" }, - { 1024, "KiB" }, - { 1024 * 1024, "MiB" }, - { 1024 * 1024 * 1024, "GiB" }, - { 0, "GiB" } -}; +const struct unitfmt_table unitfmt_bytes[] = { { 1, "Byte(s)" }, + { 1024, "KiB" }, + { 1024 * 1024, "MiB" }, + { 1024 * 1024 * 1024, "GiB" }, + { 0, "GiB" } }; -unitfmt_result -unitfmt(unsigned long long val, const struct unitfmt_table table[]) +unitfmt_result unitfmt(unsigned long long val, const struct unitfmt_table table[]) { double dval = (double) val; unitfmt_result result = { dval, table[0].symbol }; - + for (const struct unitfmt_table* p = table; p->factor && dval >= p->factor; ++p) { result.val = dval / p->factor; result.symbol = p->symbol; } - + return result; } diff --git a/src/common/util/unitfmt.h b/src/common/util/unitfmt.h index b44cb94a2..8a7b53273 100644 --- a/src/common/util/unitfmt.h +++ b/src/common/util/unitfmt.h @@ -34,12 +34,13 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif -struct unitfmt_table{ +struct unitfmt_table { unsigned long long factor; - const char* symbol; + const char* symbol; }; /*! \brief A format table for byte values (in base-2) */ @@ -50,7 +51,6 @@ typedef struct { const char* symbol; } unitfmt_result; - /*! \brief Scale values to human-readable size * * \param val Value to scale @@ -58,10 +58,9 @@ typedef struct { * * \return Conversion result. Member val is the scaled value, member * symbol points to the conversion factor symbol. - */ -unitfmt_result -unitfmt(unsigned long long val, const struct unitfmt_table table[]); - + */ +unitfmt_result unitfmt(unsigned long long val, const struct unitfmt_table table[]); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/common/util/vlenc.c b/src/common/util/vlenc.c index 460de64d7..802d15288 100644 --- a/src/common/util/vlenc.c +++ b/src/common/util/vlenc.c @@ -27,8 +27,6 @@ #include "vlenc.h" -extern inline size_t -vlenc_u64(uint64_t val, unsigned char* buf); +extern inline size_t vlenc_u64(uint64_t val, unsigned char* buf); -extern inline uint64_t -vldec_u64(const unsigned char* buf, size_t* inc); +extern inline uint64_t vldec_u64(const unsigned char* buf, size_t* inc); diff --git a/src/common/util/vlenc.h b/src/common/util/vlenc.h index b0b223ba2..45b9e3c4c 100644 --- a/src/common/util/vlenc.h +++ b/src/common/util/vlenc.h @@ -69,7 +69,8 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /*! \brief Write 64-bit value into \a buf using variable-length encoding. @@ -78,14 +79,13 @@ extern "C" { * \param buf Character buffer. Must be large enough to hold 10 bytes. * \return Number of bytes written. */ -inline size_t -vlenc_u64(uint64_t val, unsigned char* buf) +inline size_t vlenc_u64(uint64_t val, unsigned char* buf) { size_t nbytes = 0; while (val > 0x7F) { - *buf++ = (val & 0x7F) | 0x80; - val >>= 7; + *buf++ = (val & 0x7F) | 0x80; + val >>= 7; ++nbytes; } @@ -104,16 +104,15 @@ vlenc_u64(uint64_t val, unsigned char* buf) * \param inc The function increments this value by the number of bytes read. * \return The decoded value. */ -inline uint64_t -vldec_u64(const unsigned char* buf, size_t* inc) +inline uint64_t vldec_u64(const unsigned char* buf, size_t* inc) { uint64_t val = 0; size_t p = 0; - for ( ; p < 9 && (buf[p] & 0x80); ++p) - val |= ((uint64_t) (buf[p] & 0x7F) << (7*p)); + for (; p < 9 && (buf[p] & 0x80); ++p) + val |= ((uint64_t) (buf[p] & 0x7F) << (7 * p)); - val |= ((uint64_t) (buf[p] & 0x7F) << (7*p)); + val |= ((uint64_t) (buf[p] & 0x7F) << (7 * p)); ++p; *inc += p; diff --git a/src/interface/c_fortran/BufferedRegionProfile.cpp b/src/interface/c_fortran/BufferedRegionProfile.cpp index f28b409cd..4a31e38d6 100644 --- a/src/interface/c_fortran/BufferedRegionProfile.cpp +++ b/src/interface/c_fortran/BufferedRegionProfile.cpp @@ -1,24 +1,21 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #include "BufferedRegionProfile.h" using namespace cali; -struct BufferedRegionProfile::BufferedRegionProfileImpl -{ +struct BufferedRegionProfile::BufferedRegionProfileImpl { std::map reg_times; - double tot_reg_time; - double tot_time; + double tot_reg_time; + double tot_time; }; -BufferedRegionProfile::BufferedRegionProfile() - : RegionProfile(), - mP { new BufferedRegionProfileImpl } -{ } +BufferedRegionProfile::BufferedRegionProfile() : RegionProfile(), mP { new BufferedRegionProfileImpl } +{} BufferedRegionProfile::~BufferedRegionProfile() -{ } +{} void BufferedRegionProfile::start() { @@ -34,20 +31,18 @@ void BufferedRegionProfile::clear() { mP->reg_times.clear(); mP->tot_reg_time = 0.0; - mP->tot_time = 0.0; + mP->tot_time = 0.0; RegionProfile::clear(); } void BufferedRegionProfile::fetch_exclusive_region_times(const char* region_type) { - std::tie(mP->reg_times, mP->tot_reg_time, mP->tot_time) - = exclusive_region_times(region_type); + std::tie(mP->reg_times, mP->tot_reg_time, mP->tot_time) = exclusive_region_times(region_type); } void BufferedRegionProfile::fetch_inclusive_region_times(const char* region_type) { - std::tie(mP->reg_times, mP->tot_reg_time, mP->tot_time) - = inclusive_region_times(region_type); + std::tie(mP->reg_times, mP->tot_reg_time, mP->tot_time) = inclusive_region_times(region_type); } double BufferedRegionProfile::total_profiling_time() const diff --git a/src/interface/c_fortran/BufferedRegionProfile.h b/src/interface/c_fortran/BufferedRegionProfile.h index 255b9769d..883914bc9 100644 --- a/src/interface/c_fortran/BufferedRegionProfile.h +++ b/src/interface/c_fortran/BufferedRegionProfile.h @@ -13,17 +13,17 @@ class BufferedRegionProfile : private RegionProfile BufferedRegionProfile(); ~BufferedRegionProfile(); - void start(); - void stop(); - - void clear(); + void start(); + void stop(); - void fetch_exclusive_region_times(const char* region_type = ""); - void fetch_inclusive_region_times(const char* region_type = ""); + void clear(); + + void fetch_exclusive_region_times(const char* region_type = ""); + void fetch_inclusive_region_times(const char* region_type = ""); double total_profiling_time() const; double total_region_time() const; double region_time(const char* region) const; }; -} +} // namespace cali diff --git a/src/interface/c_fortran/typesCaliper.h b/src/interface/c_fortran/typesCaliper.h index 7c28474b7..4b62b2f9b 100644 --- a/src/interface/c_fortran/typesCaliper.h +++ b/src/interface/c_fortran/typesCaliper.h @@ -11,7 +11,8 @@ // splicer end types.CXX_declarations #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // splicer begin types.C_declarations @@ -19,99 +20,101 @@ extern "C" { // helper capsule_data_helper struct s_cali_SHROUD_capsule_data { - void *addr; /* address of C++ memory */ - int idtor; /* index of destructor */ + void* addr; /* address of C++ memory */ + int idtor; /* index of destructor */ }; typedef struct s_cali_SHROUD_capsule_data cali_SHROUD_capsule_data; /* helper ShroudTypeDefines */ /* Shroud type defines */ #define SH_TYPE_SIGNED_CHAR 1 -#define SH_TYPE_SHORT 2 -#define SH_TYPE_INT 3 -#define SH_TYPE_LONG 4 -#define SH_TYPE_LONG_LONG 5 -#define SH_TYPE_SIZE_T 6 - -#define SH_TYPE_UNSIGNED_SHORT SH_TYPE_SHORT + 100 -#define SH_TYPE_UNSIGNED_INT SH_TYPE_INT + 100 -#define SH_TYPE_UNSIGNED_LONG SH_TYPE_LONG + 100 -#define SH_TYPE_UNSIGNED_LONG_LONG SH_TYPE_LONG_LONG + 100 - -#define SH_TYPE_INT8_T 7 -#define SH_TYPE_INT16_T 8 -#define SH_TYPE_INT32_T 9 -#define SH_TYPE_INT64_T 10 - -#define SH_TYPE_UINT8_T SH_TYPE_INT8_T + 100 -#define SH_TYPE_UINT16_T SH_TYPE_INT16_T + 100 -#define SH_TYPE_UINT32_T SH_TYPE_INT32_T + 100 -#define SH_TYPE_UINT64_T SH_TYPE_INT64_T + 100 +#define SH_TYPE_SHORT 2 +#define SH_TYPE_INT 3 +#define SH_TYPE_LONG 4 +#define SH_TYPE_LONG_LONG 5 +#define SH_TYPE_SIZE_T 6 + +#define SH_TYPE_UNSIGNED_SHORT SH_TYPE_SHORT + 100 +#define SH_TYPE_UNSIGNED_INT SH_TYPE_INT + 100 +#define SH_TYPE_UNSIGNED_LONG SH_TYPE_LONG + 100 +#define SH_TYPE_UNSIGNED_LONG_LONG SH_TYPE_LONG_LONG + 100 + +#define SH_TYPE_INT8_T 7 +#define SH_TYPE_INT16_T 8 +#define SH_TYPE_INT32_T 9 +#define SH_TYPE_INT64_T 10 + +#define SH_TYPE_UINT8_T SH_TYPE_INT8_T + 100 +#define SH_TYPE_UINT16_T SH_TYPE_INT16_T + 100 +#define SH_TYPE_UINT32_T SH_TYPE_INT32_T + 100 +#define SH_TYPE_UINT64_T SH_TYPE_INT64_T + 100 /* least8 least16 least32 least64 */ /* fast8 fast16 fast32 fast64 */ /* intmax_t intptr_t ptrdiff_t */ -#define SH_TYPE_FLOAT 22 -#define SH_TYPE_DOUBLE 23 -#define SH_TYPE_LONG_DOUBLE 24 -#define SH_TYPE_FLOAT_COMPLEX 25 -#define SH_TYPE_DOUBLE_COMPLEX 26 +#define SH_TYPE_FLOAT 22 +#define SH_TYPE_DOUBLE 23 +#define SH_TYPE_LONG_DOUBLE 24 +#define SH_TYPE_FLOAT_COMPLEX 25 +#define SH_TYPE_DOUBLE_COMPLEX 26 #define SH_TYPE_LONG_DOUBLE_COMPLEX 27 -#define SH_TYPE_BOOL 28 -#define SH_TYPE_CHAR 29 -#define SH_TYPE_CPTR 30 -#define SH_TYPE_STRUCT 31 -#define SH_TYPE_OTHER 32 +#define SH_TYPE_BOOL 28 +#define SH_TYPE_CHAR 29 +#define SH_TYPE_CPTR 30 +#define SH_TYPE_STRUCT 31 +#define SH_TYPE_OTHER 32 // helper array_context struct s_cali_SHROUD_array { - cali_SHROUD_capsule_data cxx; /* address of C++ memory */ + cali_SHROUD_capsule_data cxx; /* address of C++ memory */ + union { - const void * base; - const char * ccharp; + const void* base; + const char* ccharp; } addr; - int type; /* type of element */ + + int type; /* type of element */ size_t elem_len; /* bytes-per-item or character len in c++ */ size_t size; /* size of data in c++ */ - int rank; /* number of dimensions, 0=scalar */ - long shape[7]; + int rank; /* number of dimensions, 0=scalar */ + long shape[7]; }; typedef struct s_cali_SHROUD_array cali_SHROUD_array; // helper capsule_cali_Annotation struct s_cali_Annotation { - void *addr; /* address of C++ memory */ - int idtor; /* index of destructor */ + void* addr; /* address of C++ memory */ + int idtor; /* index of destructor */ }; typedef struct s_cali_Annotation cali_Annotation; // helper capsule_cali_BufferedRegionProfile struct s_cali_BufferedRegionProfile { - void *addr; /* address of C++ memory */ - int idtor; /* index of destructor */ + void* addr; /* address of C++ memory */ + int idtor; /* index of destructor */ }; typedef struct s_cali_BufferedRegionProfile cali_BufferedRegionProfile; // helper capsule_cali_ConfigManager struct s_cali_ConfigManager { - void *addr; /* address of C++ memory */ - int idtor; /* index of destructor */ + void* addr; /* address of C++ memory */ + int idtor; /* index of destructor */ }; typedef struct s_cali_ConfigManager cali_ConfigManager; // helper capsule_cali_ScopeAnnotation struct s_cali_ScopeAnnotation { - void *addr; /* address of C++ memory */ - int idtor; /* index of destructor */ + void* addr; /* address of C++ memory */ + int idtor; /* index of destructor */ }; typedef struct s_cali_ScopeAnnotation cali_ScopeAnnotation; -void cali_SHROUD_memory_destructor(cali_SHROUD_capsule_data *cap); +void cali_SHROUD_memory_destructor(cali_SHROUD_capsule_data* cap); #ifdef __cplusplus } #endif -#endif // TYPESCALIPER_H +#endif // TYPESCALIPER_H diff --git a/src/interface/c_fortran/utilCaliper.cpp b/src/interface/c_fortran/utilCaliper.cpp index 9e2a9afe0..994173580 100644 --- a/src/interface/c_fortran/utilCaliper.cpp +++ b/src/interface/c_fortran/utilCaliper.cpp @@ -9,75 +9,72 @@ #include #include - #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // helper copy_string // Copy the char* or std::string in context into c_var. // Called by Fortran to deal with allocatable character. -void cali_ShroudCopyStringAndFree(cali_SHROUD_array *data, char *c_var, size_t c_var_len) { - const char *cxx_var = data->addr.ccharp; - size_t n = c_var_len; - if (data->elem_len < n) n = data->elem_len; +void cali_ShroudCopyStringAndFree(cali_SHROUD_array* data, char* c_var, size_t c_var_len) +{ + const char* cxx_var = data->addr.ccharp; + size_t n = c_var_len; + if (data->elem_len < n) + n = data->elem_len; std::strncpy(c_var, cxx_var, n); cali_SHROUD_memory_destructor(&data->cxx); // delete data->cxx.addr } - // Release library allocated memory. -void cali_SHROUD_memory_destructor(cali_SHROUD_capsule_data *cap) +void cali_SHROUD_memory_destructor(cali_SHROUD_capsule_data* cap) { - void *ptr = cap->addr; + void* ptr = cap->addr; switch (cap->idtor) { - case 0: // --none-- - { - // Nothing to delete - break; - } - case 1: // cali::ScopeAnnotation - { - cali::ScopeAnnotation *cxx_ptr = - reinterpret_cast(ptr); - delete cxx_ptr; - break; - } - case 2: // cali::Annotation - { - cali::Annotation *cxx_ptr = - reinterpret_cast(ptr); - delete cxx_ptr; - break; - } - case 3: // cali::ConfigManager - { - cali::ConfigManager *cxx_ptr = - reinterpret_cast(ptr); - delete cxx_ptr; - break; - } - case 4: // new_string - { - std::string *cxx_ptr = reinterpret_cast(ptr); - delete cxx_ptr; - break; - } - case 5: // cali::BufferedRegionProfile - { - cali::BufferedRegionProfile *cxx_ptr = - reinterpret_cast(ptr); - delete cxx_ptr; - break; - } + case 0: // --none-- + { + // Nothing to delete + break; + } + case 1: // cali::ScopeAnnotation + { + cali::ScopeAnnotation* cxx_ptr = reinterpret_cast(ptr); + delete cxx_ptr; + break; + } + case 2: // cali::Annotation + { + cali::Annotation* cxx_ptr = reinterpret_cast(ptr); + delete cxx_ptr; + break; + } + case 3: // cali::ConfigManager + { + cali::ConfigManager* cxx_ptr = reinterpret_cast(ptr); + delete cxx_ptr; + break; + } + case 4: // new_string + { + std::string* cxx_ptr = reinterpret_cast(ptr); + delete cxx_ptr; + break; + } + case 5: // cali::BufferedRegionProfile + { + cali::BufferedRegionProfile* cxx_ptr = reinterpret_cast(ptr); + delete cxx_ptr; + break; + } default: - { - // Unexpected case in destructor - break; - } + { + // Unexpected case in destructor + break; + } } - cap->addr = nullptr; - cap->idtor = 0; // avoid deleting again + cap->addr = nullptr; + cap->idtor = 0; // avoid deleting again } #ifdef __cplusplus diff --git a/src/interface/c_fortran/wrapAnnotation.cpp b/src/interface/c_fortran/wrapAnnotation.cpp index 0211aca6d..4c20e3078 100644 --- a/src/interface/c_fortran/wrapAnnotation.cpp +++ b/src/interface/c_fortran/wrapAnnotation.cpp @@ -8,88 +8,79 @@ // splicer begin class.Annotation.CXX_definitions // splicer end class.Annotation.CXX_definitions -extern "C" { +extern "C" +{ // splicer begin class.Annotation.C_definitions // splicer end class.Annotation.C_definitions -cali_Annotation * cali_Annotation_new(const char * key, - cali_Annotation * SHC_rv) +cali_Annotation* cali_Annotation_new(const char* key, cali_Annotation* SHC_rv) { // splicer begin class.Annotation.method.new - cali::Annotation *SHCXX_rv = new cali::Annotation(key); - SHC_rv->addr = static_cast(SHCXX_rv); - SHC_rv->idtor = 2; + cali::Annotation* SHCXX_rv = new cali::Annotation(key); + SHC_rv->addr = static_cast(SHCXX_rv); + SHC_rv->idtor = 2; return SHC_rv; // splicer end class.Annotation.method.new } -cali_Annotation * cali_Annotation_new_with_properties(const char * key, - int properties, cali_Annotation * SHC_rv) +cali_Annotation* cali_Annotation_new_with_properties(const char* key, int properties, cali_Annotation* SHC_rv) { // splicer begin class.Annotation.method.new_with_properties - cali::Annotation *SHCXX_rv = new cali::Annotation(key, properties); - SHC_rv->addr = static_cast(SHCXX_rv); - SHC_rv->idtor = 2; + cali::Annotation* SHCXX_rv = new cali::Annotation(key, properties); + SHC_rv->addr = static_cast(SHCXX_rv); + SHC_rv->idtor = 2; return SHC_rv; // splicer end class.Annotation.method.new_with_properties } -void cali_Annotation_delete(cali_Annotation * self) +void cali_Annotation_delete(cali_Annotation* self) { - cali::Annotation *SH_this = static_cast - (self->addr); + cali::Annotation* SH_this = static_cast(self->addr); // splicer begin class.Annotation.method.delete delete SH_this; self->addr = nullptr; // splicer end class.Annotation.method.delete } -void cali_Annotation_begin_int(cali_Annotation * self, int val) +void cali_Annotation_begin_int(cali_Annotation* self, int val) { - cali::Annotation *SH_this = static_cast - (self->addr); + cali::Annotation* SH_this = static_cast(self->addr); // splicer begin class.Annotation.method.begin_int SH_this->begin(val); // splicer end class.Annotation.method.begin_int } -void cali_Annotation_begin_string(cali_Annotation * self, - const char * val) +void cali_Annotation_begin_string(cali_Annotation* self, const char* val) { - cali::Annotation *SH_this = static_cast - (self->addr); + cali::Annotation* SH_this = static_cast(self->addr); // splicer begin class.Annotation.method.begin_string SH_this->begin(val); // splicer end class.Annotation.method.begin_string } -void cali_Annotation_set_int(cali_Annotation * self, int val) +void cali_Annotation_set_int(cali_Annotation* self, int val) { - cali::Annotation *SH_this = static_cast - (self->addr); + cali::Annotation* SH_this = static_cast(self->addr); // splicer begin class.Annotation.method.set_int SH_this->set(val); // splicer end class.Annotation.method.set_int } -void cali_Annotation_set_string(cali_Annotation * self, - const char * val) +void cali_Annotation_set_string(cali_Annotation* self, const char* val) { - cali::Annotation *SH_this = static_cast - (self->addr); + cali::Annotation* SH_this = static_cast(self->addr); // splicer begin class.Annotation.method.set_string SH_this->set(val); // splicer end class.Annotation.method.set_string } -void cali_Annotation_end(cali_Annotation * self) +void cali_Annotation_end(cali_Annotation* self) { - cali::Annotation *SH_this = static_cast - (self->addr); + cali::Annotation* SH_this = static_cast(self->addr); // splicer begin class.Annotation.method.end SH_this->end(); // splicer end class.Annotation.method.end } -} // extern "C" +} // extern "C" diff --git a/src/interface/c_fortran/wrapAnnotation.h b/src/interface/c_fortran/wrapAnnotation.h index e07c720c8..cf89578ac 100644 --- a/src/interface/c_fortran/wrapAnnotation.h +++ b/src/interface/c_fortran/wrapAnnotation.h @@ -15,34 +15,31 @@ // splicer end class.Annotation.CXX_declarations #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // splicer begin class.Annotation.C_declarations // splicer end class.Annotation.C_declarations -cali_Annotation * cali_Annotation_new(const char * key, - cali_Annotation * SHC_rv); +cali_Annotation* cali_Annotation_new(const char* key, cali_Annotation* SHC_rv); -cali_Annotation * cali_Annotation_new_with_properties(const char * key, - int properties, cali_Annotation * SHC_rv); +cali_Annotation* cali_Annotation_new_with_properties(const char* key, int properties, cali_Annotation* SHC_rv); -void cali_Annotation_delete(cali_Annotation * self); +void cali_Annotation_delete(cali_Annotation* self); -void cali_Annotation_begin_int(cali_Annotation * self, int val); +void cali_Annotation_begin_int(cali_Annotation* self, int val); -void cali_Annotation_begin_string(cali_Annotation * self, - const char * val); +void cali_Annotation_begin_string(cali_Annotation* self, const char* val); -void cali_Annotation_set_int(cali_Annotation * self, int val); +void cali_Annotation_set_int(cali_Annotation* self, int val); -void cali_Annotation_set_string(cali_Annotation * self, - const char * val); +void cali_Annotation_set_string(cali_Annotation* self, const char* val); -void cali_Annotation_end(cali_Annotation * self); +void cali_Annotation_end(cali_Annotation* self); #ifdef __cplusplus } #endif -#endif // WRAPANNOTATION_H +#endif // WRAPANNOTATION_H diff --git a/src/interface/c_fortran/wrapBufferedRegionProfile.cpp b/src/interface/c_fortran/wrapBufferedRegionProfile.cpp index b656279a5..275091cae 100644 --- a/src/interface/c_fortran/wrapBufferedRegionProfile.cpp +++ b/src/interface/c_fortran/wrapBufferedRegionProfile.cpp @@ -8,112 +8,96 @@ // splicer begin class.BufferedRegionProfile.CXX_definitions // splicer end class.BufferedRegionProfile.CXX_definitions -extern "C" { +extern "C" +{ // splicer begin class.BufferedRegionProfile.C_definitions // splicer end class.BufferedRegionProfile.C_definitions -cali_BufferedRegionProfile * cali_BufferedRegionProfile_new( - cali_BufferedRegionProfile * SHC_rv) +cali_BufferedRegionProfile* cali_BufferedRegionProfile_new(cali_BufferedRegionProfile* SHC_rv) { // splicer begin class.BufferedRegionProfile.method.new - cali::BufferedRegionProfile *SHCXX_rv = - new cali::BufferedRegionProfile(); - SHC_rv->addr = static_cast(SHCXX_rv); - SHC_rv->idtor = 5; + cali::BufferedRegionProfile* SHCXX_rv = new cali::BufferedRegionProfile(); + SHC_rv->addr = static_cast(SHCXX_rv); + SHC_rv->idtor = 5; return SHC_rv; // splicer end class.BufferedRegionProfile.method.new } -void cali_BufferedRegionProfile_delete( - cali_BufferedRegionProfile * self) +void cali_BufferedRegionProfile_delete(cali_BufferedRegionProfile* self) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.delete delete SH_this; self->addr = nullptr; // splicer end class.BufferedRegionProfile.method.delete } -void cali_BufferedRegionProfile_start(cali_BufferedRegionProfile * self) +void cali_BufferedRegionProfile_start(cali_BufferedRegionProfile* self) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.start SH_this->start(); // splicer end class.BufferedRegionProfile.method.start } -void cali_BufferedRegionProfile_stop(cali_BufferedRegionProfile * self) +void cali_BufferedRegionProfile_stop(cali_BufferedRegionProfile* self) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.stop SH_this->stop(); // splicer end class.BufferedRegionProfile.method.stop } -void cali_BufferedRegionProfile_clear(cali_BufferedRegionProfile * self) +void cali_BufferedRegionProfile_clear(cali_BufferedRegionProfile* self) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.clear SH_this->clear(); // splicer end class.BufferedRegionProfile.method.clear } -void cali_BufferedRegionProfile_fetch_exclusive_region_times( - cali_BufferedRegionProfile * self) +void cali_BufferedRegionProfile_fetch_exclusive_region_times(cali_BufferedRegionProfile* self) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.fetch_exclusive_region_times SH_this->fetch_exclusive_region_times(); // splicer end class.BufferedRegionProfile.method.fetch_exclusive_region_times } -void cali_BufferedRegionProfile_fetch_inclusive_region_times( - cali_BufferedRegionProfile * self) +void cali_BufferedRegionProfile_fetch_inclusive_region_times(cali_BufferedRegionProfile* self) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.fetch_inclusive_region_times SH_this->fetch_inclusive_region_times(); // splicer end class.BufferedRegionProfile.method.fetch_inclusive_region_times } -double cali_BufferedRegionProfile_total_profiling_time( - const cali_BufferedRegionProfile * self) +double cali_BufferedRegionProfile_total_profiling_time(const cali_BufferedRegionProfile* self) { - const cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + const cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.total_profiling_time double SHC_rv = SH_this->total_profiling_time(); return SHC_rv; // splicer end class.BufferedRegionProfile.method.total_profiling_time } -double cali_BufferedRegionProfile_total_region_time( - const cali_BufferedRegionProfile * self) +double cali_BufferedRegionProfile_total_region_time(const cali_BufferedRegionProfile* self) { - const cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + const cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.total_region_time double SHC_rv = SH_this->total_region_time(); return SHC_rv; // splicer end class.BufferedRegionProfile.method.total_region_time } -double cali_BufferedRegionProfile_region_time( - cali_BufferedRegionProfile * self, const char * region) +double cali_BufferedRegionProfile_region_time(cali_BufferedRegionProfile* self, const char* region) { - cali::BufferedRegionProfile *SH_this = - static_cast(self->addr); + cali::BufferedRegionProfile* SH_this = static_cast(self->addr); // splicer begin class.BufferedRegionProfile.method.region_time double SHC_rv = SH_this->region_time(region); return SHC_rv; // splicer end class.BufferedRegionProfile.method.region_time } -} // extern "C" +} // extern "C" diff --git a/src/interface/c_fortran/wrapBufferedRegionProfile.h b/src/interface/c_fortran/wrapBufferedRegionProfile.h index 09d136ea6..ee7219672 100644 --- a/src/interface/c_fortran/wrapBufferedRegionProfile.h +++ b/src/interface/c_fortran/wrapBufferedRegionProfile.h @@ -15,43 +15,35 @@ // splicer end class.BufferedRegionProfile.CXX_declarations #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // splicer begin class.BufferedRegionProfile.C_declarations // splicer end class.BufferedRegionProfile.C_declarations -cali_BufferedRegionProfile * cali_BufferedRegionProfile_new( - cali_BufferedRegionProfile * SHC_rv); +cali_BufferedRegionProfile* cali_BufferedRegionProfile_new(cali_BufferedRegionProfile* SHC_rv); -void cali_BufferedRegionProfile_delete( - cali_BufferedRegionProfile * self); +void cali_BufferedRegionProfile_delete(cali_BufferedRegionProfile* self); -void cali_BufferedRegionProfile_start( - cali_BufferedRegionProfile * self); +void cali_BufferedRegionProfile_start(cali_BufferedRegionProfile* self); -void cali_BufferedRegionProfile_stop(cali_BufferedRegionProfile * self); +void cali_BufferedRegionProfile_stop(cali_BufferedRegionProfile* self); -void cali_BufferedRegionProfile_clear( - cali_BufferedRegionProfile * self); +void cali_BufferedRegionProfile_clear(cali_BufferedRegionProfile* self); -void cali_BufferedRegionProfile_fetch_exclusive_region_times( - cali_BufferedRegionProfile * self); +void cali_BufferedRegionProfile_fetch_exclusive_region_times(cali_BufferedRegionProfile* self); -void cali_BufferedRegionProfile_fetch_inclusive_region_times( - cali_BufferedRegionProfile * self); +void cali_BufferedRegionProfile_fetch_inclusive_region_times(cali_BufferedRegionProfile* self); -double cali_BufferedRegionProfile_total_profiling_time( - const cali_BufferedRegionProfile * self); +double cali_BufferedRegionProfile_total_profiling_time(const cali_BufferedRegionProfile* self); -double cali_BufferedRegionProfile_total_region_time( - const cali_BufferedRegionProfile * self); +double cali_BufferedRegionProfile_total_region_time(const cali_BufferedRegionProfile* self); -double cali_BufferedRegionProfile_region_time( - cali_BufferedRegionProfile * self, const char * region); +double cali_BufferedRegionProfile_region_time(cali_BufferedRegionProfile* self, const char* region); #ifdef __cplusplus } #endif -#endif // WRAPBUFFEREDREGIONPROFILE_H +#endif // WRAPBUFFEREDREGIONPROFILE_H diff --git a/src/interface/c_fortran/wrapCaliper.h b/src/interface/c_fortran/wrapCaliper.h index 05d233b93..e3bf52ab3 100644 --- a/src/interface/c_fortran/wrapCaliper.h +++ b/src/interface/c_fortran/wrapCaliper.h @@ -15,48 +15,48 @@ // splicer end CXX_declarations #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // typedef cali::cali_id_t // splicer begin typedef.cali_id_t typedef uint64_t cali_cali_id_t; + // splicer end typedef.cali_id_t // cali::cali_attr_properties enum cali_cali_attr_properties { - cali_CALI_ATTR_DEFAULT = 0, - cali_CALI_ATTR_ASVALUE = 1, - cali_CALI_ATTR_NOMERGE = 2, + cali_CALI_ATTR_DEFAULT = 0, + cali_CALI_ATTR_ASVALUE = 1, + cali_CALI_ATTR_NOMERGE = 2, cali_CALI_ATTR_SCOPE_PROCESS = 12, - cali_CALI_ATTR_SCOPE_THREAD = 20, - cali_CALI_ATTR_SCOPE_TASK = 24, - cali_CALI_ATTR_SKIP_EVENTS = 64, - cali_CALI_ATTR_HIDDEN = 128, - cali_CALI_ATTR_NESTED = 256, - cali_CALI_ATTR_GLOBAL = 512, - cali_CALI_ATTR_UNALIGNED = 1024, - cali_CALI_ATTR_AGGREGATABLE = 2048 + cali_CALI_ATTR_SCOPE_THREAD = 20, + cali_CALI_ATTR_SCOPE_TASK = 24, + cali_CALI_ATTR_SKIP_EVENTS = 64, + cali_CALI_ATTR_HIDDEN = 128, + cali_CALI_ATTR_NESTED = 256, + cali_CALI_ATTR_GLOBAL = 512, + cali_CALI_ATTR_UNALIGNED = 1024, + cali_CALI_ATTR_AGGREGATABLE = 2048 }; // cali::cali_attr_type enum cali_cali_attr_type { - cali_CALI_TYPE_INV = 0, - cali_CALI_TYPE_USR = 1, - cali_CALI_TYPE_INT = 2, - cali_CALI_TYPE_UINT = 3, + cali_CALI_TYPE_INV = 0, + cali_CALI_TYPE_USR = 1, + cali_CALI_TYPE_INT = 2, + cali_CALI_TYPE_UINT = 3, cali_CALI_TYPE_STRING = 4, - cali_CALI_TYPE_ADDR = 5, + cali_CALI_TYPE_ADDR = 5, cali_CALI_TYPE_DOUBLE = 6, - cali_CALI_TYPE_BOOL = 7, - cali_CALI_TYPE_TYPE = 8, - cali_CALI_TYPE_PTR = 9 + cali_CALI_TYPE_BOOL = 7, + cali_CALI_TYPE_TYPE = 8, + cali_CALI_TYPE_PTR = 9 }; // cali::cali_flush_opts -enum cali_cali_flush_opts { - cali_CALI_FLUSH_CLEAR_BUFFERS = 1 -}; +enum cali_cali_flush_opts { cali_CALI_FLUSH_CLEAR_BUFFERS = 1 }; // splicer begin C_declarations // splicer end C_declarations @@ -65,4 +65,4 @@ enum cali_cali_flush_opts { } #endif -#endif // WRAPCALIPER_H +#endif // WRAPCALIPER_H diff --git a/src/interface/c_fortran/wrapConfigManager.cpp b/src/interface/c_fortran/wrapConfigManager.cpp index 2fa981ae5..81e670fc8 100644 --- a/src/interface/c_fortran/wrapConfigManager.cpp +++ b/src/interface/c_fortran/wrapConfigManager.cpp @@ -10,147 +10,135 @@ // splicer begin class.ConfigManager.CXX_definitions // splicer end class.ConfigManager.CXX_definitions -extern "C" { - +extern "C" +{ // helper ShroudStrToArray // Save str metadata into array to allow Fortran to access values. // CHARACTER(len=elem_size) src -static void ShroudStrToArray(cali_SHROUD_array *array, const std::string * src, int idtor) +static void ShroudStrToArray(cali_SHROUD_array* array, const std::string* src, int idtor) { - array->cxx.addr = const_cast(src); + array->cxx.addr = const_cast(src); array->cxx.idtor = idtor; if (src->empty()) { array->addr.ccharp = NULL; - array->elem_len = 0; + array->elem_len = 0; } else { array->addr.ccharp = src->data(); - array->elem_len = src->length(); + array->elem_len = src->length(); } array->size = 1; - array->rank = 0; // scalar + array->rank = 0; // scalar } + // splicer begin class.ConfigManager.C_definitions // splicer end class.ConfigManager.C_definitions -cali_ConfigManager * cali_ConfigManager_new(cali_ConfigManager * SHC_rv) +cali_ConfigManager* cali_ConfigManager_new(cali_ConfigManager* SHC_rv) { // splicer begin class.ConfigManager.method.new - cali::ConfigManager *SHCXX_rv = new cali::ConfigManager(); - SHC_rv->addr = static_cast(SHCXX_rv); - SHC_rv->idtor = 3; + cali::ConfigManager* SHCXX_rv = new cali::ConfigManager(); + SHC_rv->addr = static_cast(SHCXX_rv); + SHC_rv->idtor = 3; return SHC_rv; // splicer end class.ConfigManager.method.new } -void cali_ConfigManager_delete(cali_ConfigManager * self) +void cali_ConfigManager_delete(cali_ConfigManager* self) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.delete delete SH_this; self->addr = nullptr; // splicer end class.ConfigManager.method.delete } -void cali_ConfigManager_set_default_parameter(cali_ConfigManager * self, - const char * option, const char * val) +void cali_ConfigManager_set_default_parameter(cali_ConfigManager* self, const char* option, const char* val) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.set_default_parameter SH_this->set_default_parameter(option, val); // splicer end class.ConfigManager.method.set_default_parameter } void cali_ConfigManager_set_default_parameter_for_config( - cali_ConfigManager * self, const char * config, const char * option, - const char * val) + cali_ConfigManager* self, + const char* config, + const char* option, + const char* val +) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.set_default_parameter_for_config SH_this->set_default_parameter_for_config(config, option, val); // splicer end class.ConfigManager.method.set_default_parameter_for_config } -void cali_ConfigManager_add_config_spec(cali_ConfigManager * self, - const char * spec) +void cali_ConfigManager_add_config_spec(cali_ConfigManager* self, const char* spec) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.add_config_spec SH_this->add_config_spec(spec); // splicer end class.ConfigManager.method.add_config_spec } -void cali_ConfigManager_add_option_spec(cali_ConfigManager * self, - const char * spec) +void cali_ConfigManager_add_option_spec(cali_ConfigManager* self, const char* spec) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.add_option_spec SH_this->add_option_spec(spec); // splicer end class.ConfigManager.method.add_option_spec } -void cali_ConfigManager_add(cali_ConfigManager * self, - const char * config) +void cali_ConfigManager_add(cali_ConfigManager* self, const char* config) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.add SH_this->add(config); // splicer end class.ConfigManager.method.add } -bool cali_ConfigManager_error(cali_ConfigManager * self) +bool cali_ConfigManager_error(cali_ConfigManager* self) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.error bool SHC_rv = SH_this->error(); return SHC_rv; // splicer end class.ConfigManager.method.error } -void cali_ConfigManager_error_msg_bufferify(cali_ConfigManager * self, - cali_SHROUD_array *SHT_rv_cdesc) +void cali_ConfigManager_error_msg_bufferify(cali_ConfigManager* self, cali_SHROUD_array* SHT_rv_cdesc) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.error_msg_bufferify - std::string * SHCXX_rv = new std::string; - *SHCXX_rv = SH_this->error_msg(); + std::string* SHCXX_rv = new std::string; + *SHCXX_rv = SH_this->error_msg(); ShroudStrToArray(SHT_rv_cdesc, SHCXX_rv, 4); // splicer end class.ConfigManager.method.error_msg_bufferify } -void cali_ConfigManager_start(cali_ConfigManager * self) +void cali_ConfigManager_start(cali_ConfigManager* self) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.start SH_this->start(); // splicer end class.ConfigManager.method.start } -void cali_ConfigManager_stop(cali_ConfigManager * self) +void cali_ConfigManager_stop(cali_ConfigManager* self) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.stop SH_this->stop(); // splicer end class.ConfigManager.method.stop } -void cali_ConfigManager_flush(cali_ConfigManager * self) +void cali_ConfigManager_flush(cali_ConfigManager* self) { - cali::ConfigManager *SH_this = static_cast - (self->addr); + cali::ConfigManager* SH_this = static_cast(self->addr); // splicer begin class.ConfigManager.method.flush SH_this->flush(); // splicer end class.ConfigManager.method.flush } -} // extern "C" +} // extern "C" diff --git a/src/interface/c_fortran/wrapConfigManager.h b/src/interface/c_fortran/wrapConfigManager.h index 719f29776..7ea2a3f60 100644 --- a/src/interface/c_fortran/wrapConfigManager.h +++ b/src/interface/c_fortran/wrapConfigManager.h @@ -18,46 +18,44 @@ // splicer end class.ConfigManager.CXX_declarations #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // splicer begin class.ConfigManager.C_declarations // splicer end class.ConfigManager.C_declarations -cali_ConfigManager * cali_ConfigManager_new( - cali_ConfigManager * SHC_rv); +cali_ConfigManager* cali_ConfigManager_new(cali_ConfigManager* SHC_rv); -void cali_ConfigManager_delete(cali_ConfigManager * self); +void cali_ConfigManager_delete(cali_ConfigManager* self); -void cali_ConfigManager_set_default_parameter(cali_ConfigManager * self, - const char * option, const char * val); +void cali_ConfigManager_set_default_parameter(cali_ConfigManager* self, const char* option, const char* val); void cali_ConfigManager_set_default_parameter_for_config( - cali_ConfigManager * self, const char * config, const char * option, - const char * val); + cali_ConfigManager* self, + const char* config, + const char* option, + const char* val +); -void cali_ConfigManager_add_config_spec(cali_ConfigManager * self, - const char * spec); +void cali_ConfigManager_add_config_spec(cali_ConfigManager* self, const char* spec); -void cali_ConfigManager_add_option_spec(cali_ConfigManager * self, - const char * spec); +void cali_ConfigManager_add_option_spec(cali_ConfigManager* self, const char* spec); -void cali_ConfigManager_add(cali_ConfigManager * self, - const char * config); +void cali_ConfigManager_add(cali_ConfigManager* self, const char* config); -bool cali_ConfigManager_error(cali_ConfigManager * self); +bool cali_ConfigManager_error(cali_ConfigManager* self); -void cali_ConfigManager_error_msg_bufferify(cali_ConfigManager * self, - cali_SHROUD_array *SHT_rv_cdesc); +void cali_ConfigManager_error_msg_bufferify(cali_ConfigManager* self, cali_SHROUD_array* SHT_rv_cdesc); -void cali_ConfigManager_start(cali_ConfigManager * self); +void cali_ConfigManager_start(cali_ConfigManager* self); -void cali_ConfigManager_stop(cali_ConfigManager * self); +void cali_ConfigManager_stop(cali_ConfigManager* self); -void cali_ConfigManager_flush(cali_ConfigManager * self); +void cali_ConfigManager_flush(cali_ConfigManager* self); #ifdef __cplusplus } #endif -#endif // WRAPCONFIGMANAGER_H +#endif // WRAPCONFIGMANAGER_H diff --git a/src/interface/c_fortran/wrapScopeAnnotation.cpp b/src/interface/c_fortran/wrapScopeAnnotation.cpp index 1f34125a7..4871834ba 100644 --- a/src/interface/c_fortran/wrapScopeAnnotation.cpp +++ b/src/interface/c_fortran/wrapScopeAnnotation.cpp @@ -8,30 +8,29 @@ // splicer begin class.ScopeAnnotation.CXX_definitions // splicer end class.ScopeAnnotation.CXX_definitions -extern "C" { +extern "C" +{ // splicer begin class.ScopeAnnotation.C_definitions // splicer end class.ScopeAnnotation.C_definitions -cali_ScopeAnnotation * cali_ScopeAnnotation_begin(const char * name, - cali_ScopeAnnotation * SHC_rv) +cali_ScopeAnnotation* cali_ScopeAnnotation_begin(const char* name, cali_ScopeAnnotation* SHC_rv) { // splicer begin class.ScopeAnnotation.method.begin - cali::ScopeAnnotation *SHCXX_rv = new cali::ScopeAnnotation(name); - SHC_rv->addr = static_cast(SHCXX_rv); - SHC_rv->idtor = 1; + cali::ScopeAnnotation* SHCXX_rv = new cali::ScopeAnnotation(name); + SHC_rv->addr = static_cast(SHCXX_rv); + SHC_rv->idtor = 1; return SHC_rv; // splicer end class.ScopeAnnotation.method.begin } -void cali_ScopeAnnotation_end(cali_ScopeAnnotation * self) +void cali_ScopeAnnotation_end(cali_ScopeAnnotation* self) { - cali::ScopeAnnotation *SH_this = - static_cast(self->addr); + cali::ScopeAnnotation* SH_this = static_cast(self->addr); // splicer begin class.ScopeAnnotation.method.end delete SH_this; self->addr = nullptr; // splicer end class.ScopeAnnotation.method.end } -} // extern "C" +} // extern "C" diff --git a/src/interface/c_fortran/wrapScopeAnnotation.h b/src/interface/c_fortran/wrapScopeAnnotation.h index 16322bca7..b1beb6603 100644 --- a/src/interface/c_fortran/wrapScopeAnnotation.h +++ b/src/interface/c_fortran/wrapScopeAnnotation.h @@ -15,19 +15,19 @@ // splicer end class.ScopeAnnotation.CXX_declarations #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // splicer begin class.ScopeAnnotation.C_declarations // splicer end class.ScopeAnnotation.C_declarations -cali_ScopeAnnotation * cali_ScopeAnnotation_begin(const char * name, - cali_ScopeAnnotation * SHC_rv); +cali_ScopeAnnotation* cali_ScopeAnnotation_begin(const char* name, cali_ScopeAnnotation* SHC_rv); -void cali_ScopeAnnotation_end(cali_ScopeAnnotation * self); +void cali_ScopeAnnotation_end(cali_ScopeAnnotation* self); #ifdef __cplusplus } #endif -#endif // WRAPSCOPEANNOTATION_H +#endif // WRAPSCOPEANNOTATION_H diff --git a/src/interface/python/annotation.cpp b/src/interface/python/annotation.cpp index 04f620528..e50732716 100644 --- a/src/interface/python/annotation.cpp +++ b/src/interface/python/annotation.cpp @@ -1,117 +1,133 @@ #include "annotation.h" -namespace cali { +namespace cali +{ -PythonAnnotation::PythonAnnotation(const char *name) - : cali::Annotation(name, CALI_ATTR_DEFAULT) {} +PythonAnnotation::PythonAnnotation(const char* name) : cali::Annotation(name, CALI_ATTR_DEFAULT) +{} -PythonAnnotation::PythonAnnotation(const char *name, cali_attr_properties opt) - : cali::Annotation(name, opt) {} +PythonAnnotation::PythonAnnotation(const char* name, cali_attr_properties opt) : cali::Annotation(name, opt) +{} -PythonAnnotation &PythonAnnotation::begin() { - cali::Annotation::begin(); - return *this; +PythonAnnotation& PythonAnnotation::begin() +{ + cali::Annotation::begin(); + return *this; } -PythonAnnotation &PythonAnnotation::begin(int data) { - cali::Annotation::begin(data); - return *this; +PythonAnnotation& PythonAnnotation::begin(int data) +{ + cali::Annotation::begin(data); + return *this; } -PythonAnnotation &PythonAnnotation::begin(double data) { - cali::Annotation::begin(data); - return *this; +PythonAnnotation& PythonAnnotation::begin(double data) +{ + cali::Annotation::begin(data); + return *this; } -PythonAnnotation &PythonAnnotation::begin(const char *data) { - cali::Annotation::begin(data); - return *this; +PythonAnnotation& PythonAnnotation::begin(const char* data) +{ + cali::Annotation::begin(data); + return *this; } -PythonAnnotation &PythonAnnotation::begin(cali_attr_type type, - const std::string &data) { - cali::Annotation::begin(type, data.data(), data.size()); - return *this; +PythonAnnotation& PythonAnnotation::begin(cali_attr_type type, const std::string& data) +{ + cali::Annotation::begin(type, data.data(), data.size()); + return *this; } -PythonAnnotation &PythonAnnotation::set(int data) { - cali::Annotation::set(data); - return *this; +PythonAnnotation& PythonAnnotation::set(int data) +{ + cali::Annotation::set(data); + return *this; } -PythonAnnotation &PythonAnnotation::set(double data) { - cali::Annotation::set(data); - return *this; +PythonAnnotation& PythonAnnotation::set(double data) +{ + cali::Annotation::set(data); + return *this; } -PythonAnnotation &PythonAnnotation::set(const char *data) { - cali::Annotation::set(data); - return *this; +PythonAnnotation& PythonAnnotation::set(const char* data) +{ + cali::Annotation::set(data); + return *this; } -PythonAnnotation &PythonAnnotation::set(cali_attr_type type, - const std::string &data) { - cali::Annotation::set(type, data.data(), data.size()); - return *this; +PythonAnnotation& PythonAnnotation::set(cali_attr_type type, const std::string& data) +{ + cali::Annotation::set(type, data.data(), data.size()); + return *this; } -void create_caliper_annotation_mod(py::module_ &caliper_annotation_mod) { - py::class_ annotation_type(caliper_annotation_mod, - "Annotation"); - annotation_type.def(py::init(), - "Creates an annotation object to manipulate the context " - "attribute with the given name.", - py::arg()); - annotation_type.def(py::init(), - "Creates an annotation object to manipulate the context " - "attribute with the given name", - py::arg(), py::arg("opt")); - annotation_type.def( - "end", &PythonAnnotation::end, - "Close the top-most open region for the associated context attribute."); - annotation_type.def("begin", - static_cast( - &PythonAnnotation::begin), - "Begin the region for the associated context attribute"); - annotation_type.def("begin", - static_cast( - &PythonAnnotation::begin), - "Begin the region for the associated context attribute " - "with an integer value"); - annotation_type.def( - "begin", - static_cast( - &PythonAnnotation::begin), - "Begin the region for the associated context attribute " - "with a str/bytes value"); - annotation_type.def( - "begin", - static_cast( - &PythonAnnotation::begin), - "Begin the region for the associated context attribute " - "with a str/bytes value"); - annotation_type.def("set", - static_cast( - &PythonAnnotation::set), - "Exports an entry for the associated context attribute " - "with an integer value. The top-most prior open value " - "for the attribute, if any, will be overwritten."); - annotation_type.def( - "set", - static_cast( - &PythonAnnotation::set), - "Exports an entry for the associated context attribute " - "with a str/bytes value. The top-most prior open value " - "for the attribute, if any, will be overwritten."); - annotation_type.def( - "set", - static_cast( - &PythonAnnotation::set), - "Exports an entry for the associated context attribute " - "with a str/bytes value. The top-most prior open value " - "for the attribute, if any, will be overwritten."); +void create_caliper_annotation_mod(py::module_& caliper_annotation_mod) +{ + py::class_ annotation_type(caliper_annotation_mod, "Annotation"); + annotation_type.def( + py::init(), + "Creates an annotation object to manipulate the context " + "attribute with the given name.", + py::arg() + ); + annotation_type.def( + py::init(), + "Creates an annotation object to manipulate the context " + "attribute with the given name", + py::arg(), + py::arg("opt") + ); + annotation_type + .def("end", &PythonAnnotation::end, "Close the top-most open region for the associated context attribute."); + annotation_type.def( + "begin", + static_cast(&PythonAnnotation::begin), + "Begin the region for the associated context attribute" + ); + annotation_type.def( + "begin", + static_cast(&PythonAnnotation::begin), + "Begin the region for the associated context attribute " + "with an integer value" + ); + annotation_type.def( + "begin", + static_cast(&PythonAnnotation::begin), + "Begin the region for the associated context attribute " + "with a str/bytes value" + ); + annotation_type.def( + "begin", + static_cast( + &PythonAnnotation::begin + ), + "Begin the region for the associated context attribute " + "with a str/bytes value" + ); + annotation_type.def( + "set", + static_cast(&PythonAnnotation::set), + "Exports an entry for the associated context attribute " + "with an integer value. The top-most prior open value " + "for the attribute, if any, will be overwritten." + ); + annotation_type.def( + "set", + static_cast(&PythonAnnotation::set), + "Exports an entry for the associated context attribute " + "with a str/bytes value. The top-most prior open value " + "for the attribute, if any, will be overwritten." + ); + annotation_type.def( + "set", + static_cast(&PythonAnnotation::set + ), + "Exports an entry for the associated context attribute " + "with a str/bytes value. The top-most prior open value " + "for the attribute, if any, will be overwritten." + ); } } // namespace cali \ No newline at end of file diff --git a/src/interface/python/annotation.h b/src/interface/python/annotation.h index 9e1e09557..74ef449d5 100644 --- a/src/interface/python/annotation.h +++ b/src/interface/python/annotation.h @@ -3,34 +3,37 @@ #include "common.h" -namespace cali { +namespace cali +{ -class PythonAnnotation : public cali::Annotation { +class PythonAnnotation : public cali::Annotation +{ public: - PythonAnnotation(const char *name); - PythonAnnotation(const char *name, cali_attr_properties opt); + PythonAnnotation(const char* name); - PythonAnnotation &begin(); + PythonAnnotation(const char* name, cali_attr_properties opt); - PythonAnnotation &begin(int data); + PythonAnnotation& begin(); - PythonAnnotation &begin(double data); + PythonAnnotation& begin(int data); - PythonAnnotation &begin(const char *data); + PythonAnnotation& begin(double data); - PythonAnnotation &begin(cali_attr_type type, const std::string &data); + PythonAnnotation& begin(const char* data); - PythonAnnotation &set(int data); + PythonAnnotation& begin(cali_attr_type type, const std::string& data); - PythonAnnotation &set(double data); + PythonAnnotation& set(int data); - PythonAnnotation &set(const char *data); + PythonAnnotation& set(double data); - PythonAnnotation &set(cali_attr_type type, const std::string &data); + PythonAnnotation& set(const char* data); + + PythonAnnotation& set(cali_attr_type type, const std::string& data); }; -void create_caliper_annotation_mod(py::module_ &caliper_annotation_mod); +void create_caliper_annotation_mod(py::module_& caliper_annotation_mod); } // namespace cali diff --git a/src/interface/python/config_manager.cpp b/src/interface/python/config_manager.cpp index ad4f8a5c3..29661480d 100644 --- a/src/interface/python/config_manager.cpp +++ b/src/interface/python/config_manager.cpp @@ -1,141 +1,157 @@ #include "config_manager.h" #include -namespace cali { +namespace cali +{ -PythonConfigManager::PythonConfigManager() : cali::ConfigManager() {} +PythonConfigManager::PythonConfigManager() : cali::ConfigManager() +{} -PythonConfigManager::PythonConfigManager(const char *config_str) - : cali::ConfigManager(config_str) {} +PythonConfigManager::PythonConfigManager(const char* config_str) : cali::ConfigManager(config_str) +{} -void PythonConfigManager::add_config_spec(py::dict json) { - add_config_spec(py::str(json)); +void PythonConfigManager::add_config_spec(py::dict json) +{ + add_config_spec(py::str(json)); } -void PythonConfigManager::add_option_spec(py::dict json) { - add_option_spec(py::str(json)); +void PythonConfigManager::add_option_spec(py::dict json) +{ + add_option_spec(py::str(json)); } -void PythonConfigManager::py_add(const char *config_string) { - cali::ConfigManager::add(config_string); +void PythonConfigManager::py_add(const char* config_string) +{ + cali::ConfigManager::add(config_string); } -void PythonConfigManager::check(const char *config_str) { - std::string err_msg = cali::ConfigManager::check(config_str); - if (err_msg.size() != 0) { - throw std::runtime_error(err_msg); - } +void PythonConfigManager::check(const char* config_str) +{ + std::string err_msg = cali::ConfigManager::check(config_str); + if (err_msg.size() != 0) { + throw std::runtime_error(err_msg); + } } -void create_caliper_config_manager_mod( - py::module_ &caliper_config_manager_mod) { - py::class_ config_mgr_type(caliper_config_manager_mod, - "ConfigManager"); - config_mgr_type.def(py::init<>(), "Create a ConfigManager."); - config_mgr_type.def( - py::init(), - "Create a ConfigManager with the provide configuration string."); - config_mgr_type.def( - "add_config_spec", - static_cast( - &cali::ConfigManager::add_config_spec), - "Add a custom config spec to this ConfigManager." - "" - "Adds a new Caliper configuration spec for this ConfigManager" - "using a custom ChannelController or option checking function."); - config_mgr_type.def( - "add_config_spec", - static_cast( - &PythonConfigManager::add_config_spec), - "Add a JSON config spec to this ConfigManager" - "" - "Adds a new Caliper configuration specification for this ConfigManager" - "using a basic ChannelController." - "" - "See the C++ docs for more details"); - config_mgr_type.def( - "add_option_spec", - static_cast( - &cali::ConfigManager::add_option_spec), - "Add a JSON option spec to this ConfigManager" - "" - "Allows one to define options for any config in a matching category." - "Option specifications must be added before querying or creating any" - "configurations to be effective." - "" - "See the C++ docs for more details"); - config_mgr_type.def( - "add_option_spec", - static_cast( - &PythonConfigManager::add_option_spec), - "Add a JSON option spec to this ConfigManager" - "" - "Allows one to define options for any config in a matching category." - "Option specifications must be added before querying or creating any" - "configurations to be effective." - "" - "See the C++ docs for more details"); - config_mgr_type.def("add", &PythonConfigManager::py_add, - "Parse the provided configuration string and create the " - "specified configuration channels."); - config_mgr_type.def( - "load", - static_cast( - &cali::ConfigManager::load), - "Load config and option specs from the provided filename."); - config_mgr_type.def( - "set_default_parameter", - static_cast( - &cali::ConfigManager::set_default_parameter), - "Pre-set a key-value pair for all configurations."); - config_mgr_type.def( - "set_default_parameter_for_config", - static_cast( - &cali::ConfigManager::set_default_parameter_for_config), - "Pre-set a key-value pair for the specified configuration."); - config_mgr_type.def( - "error", - static_cast( - &cali::ConfigManager::error), - "Returns true if there was an error while parsing configuration."); - config_mgr_type.def("error_msg", - static_cast( - &cali::ConfigManager::error_msg), - "Returns an error message if there was an error while " - "parsing configuration."); - config_mgr_type.def("__repr__", - static_cast( - &cali::ConfigManager::error_msg)); - config_mgr_type.def( - "start", - static_cast(&cali::ConfigManager::start), - "Start all configured measurement channels, or re-start paused ones."); - config_mgr_type.def( - "stop", - static_cast(&cali::ConfigManager::stop), - "Pause all configured measurement channels."); - config_mgr_type.def( - "flush", - static_cast(&cali::ConfigManager::flush), - "Flush all configured measurement channels."); - config_mgr_type.def("check", &PythonConfigManager::check, - "Check if the given config string is valid."); - config_mgr_type.def( - "available_config_specs", - static_cast (PythonConfigManager::*)() const>( - &cali::ConfigManager::available_config_specs), - "Return names of available config specs."); - config_mgr_type.def( - "get_documentation_for_spec", - static_cast( - &cali::ConfigManager::get_documentation_for_spec), - "Return short description for the given config spec."); - config_mgr_type.def_static( - "get_config_docstrings", - static_cast (*)()>( - &cali::ConfigManager::get_config_docstrings), - "Return descriptions for available global configs."); +void create_caliper_config_manager_mod(py::module_& caliper_config_manager_mod) +{ + py::class_ config_mgr_type(caliper_config_manager_mod, "ConfigManager"); + config_mgr_type.def(py::init<>(), "Create a ConfigManager."); + config_mgr_type.def(py::init(), "Create a ConfigManager with the provide configuration string."); + config_mgr_type.def( + "add_config_spec", + static_cast(&cali::ConfigManager::add_config_spec), + "Add a custom config spec to this ConfigManager." + "" + "Adds a new Caliper configuration spec for this ConfigManager" + "using a custom ChannelController or option checking function." + ); + config_mgr_type.def( + "add_config_spec", + static_cast(&PythonConfigManager::add_config_spec), + "Add a JSON config spec to this ConfigManager" + "" + "Adds a new Caliper configuration specification for this ConfigManager" + "using a basic ChannelController." + "" + "See the C++ docs for more details" + ); + config_mgr_type.def( + "add_option_spec", + static_cast(&cali::ConfigManager::add_option_spec), + "Add a JSON option spec to this ConfigManager" + "" + "Allows one to define options for any config in a matching category." + "Option specifications must be added before querying or creating any" + "configurations to be effective." + "" + "See the C++ docs for more details" + ); + config_mgr_type.def( + "add_option_spec", + static_cast(&PythonConfigManager::add_option_spec), + "Add a JSON option spec to this ConfigManager" + "" + "Allows one to define options for any config in a matching category." + "Option specifications must be added before querying or creating any" + "configurations to be effective." + "" + "See the C++ docs for more details" + ); + config_mgr_type.def( + "add", + &PythonConfigManager::py_add, + "Parse the provided configuration string and create the " + "specified configuration channels." + ); + config_mgr_type.def( + "load", + static_cast(&cali::ConfigManager::load), + "Load config and option specs from the provided filename." + ); + config_mgr_type.def( + "set_default_parameter", + static_cast(&cali::ConfigManager::set_default_parameter + ), + "Pre-set a key-value pair for all configurations." + ); + config_mgr_type.def( + "set_default_parameter_for_config", + static_cast( + &cali::ConfigManager::set_default_parameter_for_config + ), + "Pre-set a key-value pair for the specified configuration." + ); + config_mgr_type.def( + "error", + static_cast(&cali::ConfigManager::error), + "Returns true if there was an error while parsing configuration." + ); + config_mgr_type.def( + "error_msg", + static_cast(&cali::ConfigManager::error_msg), + "Returns an error message if there was an error while " + "parsing configuration." + ); + config_mgr_type.def( + "__repr__", + static_cast(&cali::ConfigManager::error_msg) + ); + config_mgr_type.def( + "start", + static_cast(&cali::ConfigManager::start), + "Start all configured measurement channels, or re-start paused ones." + ); + config_mgr_type.def( + "stop", + static_cast(&cali::ConfigManager::stop), + "Pause all configured measurement channels." + ); + config_mgr_type.def( + "flush", + static_cast(&cali::ConfigManager::flush), + "Flush all configured measurement channels." + ); + config_mgr_type.def("check", &PythonConfigManager::check, "Check if the given config string is valid."); + config_mgr_type.def( + "available_config_specs", + static_cast (PythonConfigManager::*)() const>( + &cali::ConfigManager::available_config_specs + ), + "Return names of available config specs." + ); + config_mgr_type.def( + "get_documentation_for_spec", + static_cast( + &cali::ConfigManager::get_documentation_for_spec + ), + "Return short description for the given config spec." + ); + config_mgr_type.def_static( + "get_config_docstrings", + static_cast (*)()>(&cali::ConfigManager::get_config_docstrings), + "Return descriptions for available global configs." + ); } } // namespace cali \ No newline at end of file diff --git a/src/interface/python/config_manager.h b/src/interface/python/config_manager.h index 9f18ee7da..f5a7d2dd0 100644 --- a/src/interface/python/config_manager.h +++ b/src/interface/python/config_manager.h @@ -5,51 +5,54 @@ #include -namespace cali { -class PythonConfigManager : public cali::ConfigManager { +namespace cali +{ +class PythonConfigManager : public cali::ConfigManager +{ public: - PythonConfigManager(); - PythonConfigManager(const char *config_str); + PythonConfigManager(); - // void add_config_spec(const char *json); + PythonConfigManager(const char* config_str); - void add_config_spec(py::dict json); + // void add_config_spec(const char *json); - // void add_option_spec(const char *json); + void add_config_spec(py::dict json); - void add_option_spec(py::dict json); + // void add_option_spec(const char *json); - void py_add(const char *config_string); + void add_option_spec(py::dict json); - // void load(const char *filename); + void py_add(const char* config_string); - // void set_default_parameter(const char *key, const char *value); + // void load(const char *filename); - // void set_default_parameter_for_config(const char *config, const char *key, - // const char *value); + // void set_default_parameter(const char *key, const char *value); - // bool error() const; + // void set_default_parameter_for_config(const char *config, const char *key, + // const char *value); - // Set to __repr__ - // bool error_msg() const; + // bool error() const; - // void start(); + // Set to __repr__ + // bool error_msg() const; - // void stop(); + // void start(); - // void flush(); + // void stop(); - void check(const char *config_str); + // void flush(); - // std::vector available_config_specs() const; + void check(const char* config_str); - // std::string get_documentation_for_spec(const char *name) const; + // std::vector available_config_specs() const; - // static std::vector get_config_docstrings(); + // std::string get_documentation_for_spec(const char *name) const; + + // static std::vector get_config_docstrings(); }; -void create_caliper_config_manager_mod(py::module_ &caliper_config_manager_mod); +void create_caliper_config_manager_mod(py::module_& caliper_config_manager_mod); } // namespace cali diff --git a/src/interface/python/instrumentation.cpp b/src/interface/python/instrumentation.cpp index ecfdc64ad..0af553ab4 100644 --- a/src/interface/python/instrumentation.cpp +++ b/src/interface/python/instrumentation.cpp @@ -2,196 +2,235 @@ #include -namespace cali { - -PythonAttribute::PythonAttribute(const char *name, cali_attr_type type) - : m_attr_id(cali_create_attribute(name, type, CALI_ATTR_DEFAULT)) { - if (m_attr_id == CALI_INV_ID) { - throw std::runtime_error("Failed to create attribute"); - } -} - -PythonAttribute::PythonAttribute(const char *name, cali_attr_type type, - cali_attr_properties opt) - : m_attr_id(cali_create_attribute(name, type, opt)) { - if (m_attr_id == CALI_INV_ID) { - throw std::runtime_error("Failed to create attribute"); - } -} - -PythonAttribute::PythonAttribute(cali_id_t id) { - if (id == CALI_INV_ID) { - throw std::runtime_error("Invalid attribute"); - } - m_attr_id = id; -} - -PythonAttribute PythonAttribute::find_attribute(const char *name) { - PythonAttribute found_attr{cali_find_attribute(name)}; - return found_attr; -} - -const char *PythonAttribute::name() const { - return cali_attribute_name(m_attr_id); -} - -cali_attr_type PythonAttribute::type() const { - return cali_attribute_type(m_attr_id); -} - -cali_attr_properties PythonAttribute::properties() const { - return static_cast( - cali_attribute_properties(m_attr_id)); -} - -void PythonAttribute::begin() { cali_begin(m_attr_id); } - -void PythonAttribute::begin(int val) { cali_begin_int(m_attr_id, val); } - -void PythonAttribute::begin(double val) { cali_begin_double(m_attr_id, val); } - -void PythonAttribute::begin(const char *val) { - cali_begin_string(m_attr_id, val); -} - -void PythonAttribute::set(int val) { cali_set_int(m_attr_id, val); } - -void PythonAttribute::set(double val) { cali_set_double(m_attr_id, val); } - -void PythonAttribute::set(const char *val) { cali_set_string(m_attr_id, val); } - -void PythonAttribute::end() { cali_end(m_attr_id); } - -void create_caliper_instrumentation_mod( - py::module_ &caliper_instrumentation_mod) { - // PythonAttribute bindings - py::class_ cali_attribute_type(caliper_instrumentation_mod, - "Attribute"); - cali_attribute_type.def(py::init(), - "Create Caliper Attribute with name and type.", - py::arg(), py::arg()); - cali_attribute_type.def( - py::init(), - "Create Caliper Attribute with name, type, and properties.", py::arg(), - py::arg(), py::arg("opt")); - cali_attribute_type.def_static("find_attribute", - &PythonAttribute::find_attribute, - "Get Caliper Attribute by name."); - cali_attribute_type.def_property_readonly("name", &PythonAttribute::name, - "Name of the Caliper Attribute."); - cali_attribute_type.def_property_readonly("type", &PythonAttribute::type, - "Type of the Caliper Attribute."); - cali_attribute_type.def_property_readonly( - "properties", &PythonAttribute::properties, - "Properties of the Caliper Attribute."); - cali_attribute_type.def( - "begin", - static_cast(&PythonAttribute::begin), - "Begin region where the value for the Attribute is 'true' on the " - "blackboard."); - cali_attribute_type.def( - "begin", - static_cast(&PythonAttribute::begin), - "Begin integer region for attribute on the blackboard."); - cali_attribute_type.def( - "begin", - static_cast(&PythonAttribute::begin), - "Begin float region for attribute on the blackboard."); - cali_attribute_type.def( - "begin", - static_cast( - &PythonAttribute::begin), - "Begin str/bytes region for attribute on the blackboard."); - cali_attribute_type.def( - "set", static_cast(&PythonAttribute::set), - "Set integer value for attribute on the blackboard."); - cali_attribute_type.def( - "set", - static_cast(&PythonAttribute::set), - "Set double value for attribute on the blackboard."); - cali_attribute_type.def( - "set", - static_cast( - &PythonAttribute::set), - "Set str/bytes value for attribute on the blackboard."); - cali_attribute_type.def( - "end", &PythonAttribute::end, - "End innermost open region for attribute on the blackboard."); - - // Bindings for region begin/end functions - caliper_instrumentation_mod.def( - "begin_region", &cali_begin_region, - "Begin nested region by name." - "" - "Begins nested region using the built-in annotation attribute."); - caliper_instrumentation_mod.def( - "end_region", &cali_end_region, - "End nested region by name." - "" - "Ends nested region using built-in annotation attribute." - "Prints an error if the name does not match the currently open region."); - caliper_instrumentation_mod.def( - "begin_phase", &cali_begin_phase, - "Begin phase region by name." - "" - "A phase marks high-level, long(er)-running code regions. While regular " - "regions" - "use the \"region\" attribute with annotation level 0, phase regions use " - "the" - "\"phase\" attribute with annotation level 4. Otherwise, phases behave" - "identical to regular Caliper regions."); - caliper_instrumentation_mod.def("end_phase", &cali_end_phase, - "End phase region by name."); - caliper_instrumentation_mod.def( - "begin_comm_region", &cali_begin_comm_region, - "Begin communication region by name." - "" - "A communication region can be used to mark communication operations" - "(e.g., MPI calls) that belong to a single communication pattern." - "They can be used to summarize communication pattern statistics." - "Otherwise, they behave identical to regular Caliper regions."); - caliper_instrumentation_mod.def("end_comm_region", &cali_end_comm_region, - "End communication region by name."); - - // Bindings for "_byname" functions - caliper_instrumentation_mod.def( - "begin_byname", &cali_begin_byname, - "Same as Annotation.begin, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "begin_byname", &cali_begin_double_byname, - "Same as Annotation.begin, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "begin_byname", &cali_begin_int_byname, - "Same as Annotation.begin, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "begin_byname", &cali_begin_string_byname, - "Same as Annotation.begin, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "set_byname", &cali_set_double_byname, - "Same as Annotation.set, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "set_byname", &cali_set_int_byname, - "Same as Annotation.set, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "set_byname", &cali_set_string_byname, - "Same as Annotation.set, but refers to annotation by name."); - caliper_instrumentation_mod.def( - "end_byname", &cali_end_byname, - "Same as Annotation.end, but refers to annotation by name."); - - // Bindings for global "_byname" functions - caliper_instrumentation_mod.def( - "set_global_byname", &cali_set_global_double_byname, - "Set a global attribute with a given name to the given value."); - caliper_instrumentation_mod.def( - "set_global_byname", &cali_set_global_int_byname, - "Set a global attribute with a given name to the given value."); - caliper_instrumentation_mod.def( - "set_global_byname", &cali_set_global_string_byname, - "Set a global attribute with a given name to the given value."); - caliper_instrumentation_mod.def( - "set_global_byname", &cali_set_global_uint_byname, - "Set a global attribute with a given name to the given value."); +namespace cali +{ + +PythonAttribute::PythonAttribute(const char* name, cali_attr_type type) + : m_attr_id(cali_create_attribute(name, type, CALI_ATTR_DEFAULT)) +{ + if (m_attr_id == CALI_INV_ID) { + throw std::runtime_error("Failed to create attribute"); + } +} + +PythonAttribute::PythonAttribute(const char* name, cali_attr_type type, cali_attr_properties opt) + : m_attr_id(cali_create_attribute(name, type, opt)) +{ + if (m_attr_id == CALI_INV_ID) { + throw std::runtime_error("Failed to create attribute"); + } +} + +PythonAttribute::PythonAttribute(cali_id_t id) +{ + if (id == CALI_INV_ID) { + throw std::runtime_error("Invalid attribute"); + } + m_attr_id = id; +} + +PythonAttribute PythonAttribute::find_attribute(const char* name) +{ + PythonAttribute found_attr { cali_find_attribute(name) }; + return found_attr; +} + +const char* PythonAttribute::name() const +{ + return cali_attribute_name(m_attr_id); +} + +cali_attr_type PythonAttribute::type() const +{ + return cali_attribute_type(m_attr_id); +} + +cali_attr_properties PythonAttribute::properties() const +{ + return static_cast(cali_attribute_properties(m_attr_id)); +} + +void PythonAttribute::begin() +{ + cali_begin(m_attr_id); +} + +void PythonAttribute::begin(int val) +{ + cali_begin_int(m_attr_id, val); +} + +void PythonAttribute::begin(double val) +{ + cali_begin_double(m_attr_id, val); +} + +void PythonAttribute::begin(const char* val) +{ + cali_begin_string(m_attr_id, val); +} + +void PythonAttribute::set(int val) +{ + cali_set_int(m_attr_id, val); +} + +void PythonAttribute::set(double val) +{ + cali_set_double(m_attr_id, val); +} + +void PythonAttribute::set(const char* val) +{ + cali_set_string(m_attr_id, val); +} + +void PythonAttribute::end() +{ + cali_end(m_attr_id); +} + +void create_caliper_instrumentation_mod(py::module_& caliper_instrumentation_mod) +{ + // PythonAttribute bindings + py::class_ cali_attribute_type(caliper_instrumentation_mod, "Attribute"); + cali_attribute_type.def( + py::init(), + "Create Caliper Attribute with name and type.", + py::arg(), + py::arg() + ); + cali_attribute_type.def( + py::init(), + "Create Caliper Attribute with name, type, and properties.", + py::arg(), + py::arg(), + py::arg("opt") + ); + cali_attribute_type + .def_static("find_attribute", &PythonAttribute::find_attribute, "Get Caliper Attribute by name."); + cali_attribute_type.def_property_readonly("name", &PythonAttribute::name, "Name of the Caliper Attribute."); + cali_attribute_type.def_property_readonly("type", &PythonAttribute::type, "Type of the Caliper Attribute."); + cali_attribute_type + .def_property_readonly("properties", &PythonAttribute::properties, "Properties of the Caliper Attribute."); + cali_attribute_type.def( + "begin", + static_cast(&PythonAttribute::begin), + "Begin region where the value for the Attribute is 'true' on the " + "blackboard." + ); + cali_attribute_type.def( + "begin", + static_cast(&PythonAttribute::begin), + "Begin integer region for attribute on the blackboard." + ); + cali_attribute_type.def( + "begin", + static_cast(&PythonAttribute::begin), + "Begin float region for attribute on the blackboard." + ); + cali_attribute_type.def( + "begin", + static_cast(&PythonAttribute::begin), + "Begin str/bytes region for attribute on the blackboard." + ); + cali_attribute_type.def( + "set", + static_cast(&PythonAttribute::set), + "Set integer value for attribute on the blackboard." + ); + cali_attribute_type.def( + "set", + static_cast(&PythonAttribute::set), + "Set double value for attribute on the blackboard." + ); + cali_attribute_type.def( + "set", + static_cast(&PythonAttribute::set), + "Set str/bytes value for attribute on the blackboard." + ); + cali_attribute_type.def("end", &PythonAttribute::end, "End innermost open region for attribute on the blackboard."); + + // Bindings for region begin/end functions + caliper_instrumentation_mod.def( + "begin_region", + &cali_begin_region, + "Begin nested region by name." + "" + "Begins nested region using the built-in annotation attribute." + ); + caliper_instrumentation_mod.def( + "end_region", + &cali_end_region, + "End nested region by name." + "" + "Ends nested region using built-in annotation attribute." + "Prints an error if the name does not match the currently open region." + ); + caliper_instrumentation_mod.def( + "begin_phase", + &cali_begin_phase, + "Begin phase region by name." + "" + "A phase marks high-level, long(er)-running code regions. While regular " + "regions" + "use the \"region\" attribute with annotation level 0, phase regions use " + "the" + "\"phase\" attribute with annotation level 4. Otherwise, phases behave" + "identical to regular Caliper regions." + ); + caliper_instrumentation_mod.def("end_phase", &cali_end_phase, "End phase region by name."); + caliper_instrumentation_mod.def( + "begin_comm_region", + &cali_begin_comm_region, + "Begin communication region by name." + "" + "A communication region can be used to mark communication operations" + "(e.g., MPI calls) that belong to a single communication pattern." + "They can be used to summarize communication pattern statistics." + "Otherwise, they behave identical to regular Caliper regions." + ); + caliper_instrumentation_mod.def("end_comm_region", &cali_end_comm_region, "End communication region by name."); + + // Bindings for "_byname" functions + caliper_instrumentation_mod + .def("begin_byname", &cali_begin_byname, "Same as Annotation.begin, but refers to annotation by name."); + caliper_instrumentation_mod + .def("begin_byname", &cali_begin_double_byname, "Same as Annotation.begin, but refers to annotation by name."); + caliper_instrumentation_mod + .def("begin_byname", &cali_begin_int_byname, "Same as Annotation.begin, but refers to annotation by name."); + caliper_instrumentation_mod + .def("begin_byname", &cali_begin_string_byname, "Same as Annotation.begin, but refers to annotation by name."); + caliper_instrumentation_mod + .def("set_byname", &cali_set_double_byname, "Same as Annotation.set, but refers to annotation by name."); + caliper_instrumentation_mod + .def("set_byname", &cali_set_int_byname, "Same as Annotation.set, but refers to annotation by name."); + caliper_instrumentation_mod + .def("set_byname", &cali_set_string_byname, "Same as Annotation.set, but refers to annotation by name."); + caliper_instrumentation_mod + .def("end_byname", &cali_end_byname, "Same as Annotation.end, but refers to annotation by name."); + + // Bindings for global "_byname" functions + caliper_instrumentation_mod.def( + "set_global_byname", + &cali_set_global_double_byname, + "Set a global attribute with a given name to the given value." + ); + caliper_instrumentation_mod.def( + "set_global_byname", + &cali_set_global_int_byname, + "Set a global attribute with a given name to the given value." + ); + caliper_instrumentation_mod.def( + "set_global_byname", + &cali_set_global_string_byname, + "Set a global attribute with a given name to the given value." + ); + caliper_instrumentation_mod.def( + "set_global_byname", + &cali_set_global_uint_byname, + "Set a global attribute with a given name to the given value." + ); } } // namespace cali \ No newline at end of file diff --git a/src/interface/python/instrumentation.h b/src/interface/python/instrumentation.h index 461f1cdbe..8a76904de 100644 --- a/src/interface/python/instrumentation.h +++ b/src/interface/python/instrumentation.h @@ -3,49 +3,51 @@ #include "common.h" -namespace cali { +namespace cali +{ -class PythonAttribute { +class PythonAttribute +{ public: - PythonAttribute(const char *name, cali_attr_type type); - PythonAttribute(const char *name, cali_attr_type type, - cali_attr_properties opt); + PythonAttribute(const char* name, cali_attr_type type); - static PythonAttribute find_attribute(const char *name); + PythonAttribute(const char* name, cali_attr_type type, cali_attr_properties opt); - const char *name() const; + static PythonAttribute find_attribute(const char* name); - cali_attr_type type() const; + const char* name() const; - cali_attr_properties properties() const; + cali_attr_type type() const; - void begin(); + cali_attr_properties properties() const; - void begin(int val); + void begin(); - void begin(double val); + void begin(int val); - void begin(const char *val); + void begin(double val); - void set(int val); + void begin(const char* val); - void set(double val); + void set(int val); - void set(const char *val); + void set(double val); - void end(); + void set(const char* val); + + void end(); private: - PythonAttribute(cali_id_t id); - cali_id_t m_attr_id; + PythonAttribute(cali_id_t id); + + cali_id_t m_attr_id; }; // TODO add "byname" functions to module -void create_caliper_instrumentation_mod( - py::module_ &caliper_instrumentation_mod); +void create_caliper_instrumentation_mod(py::module_& caliper_instrumentation_mod); } // namespace cali diff --git a/src/interface/python/loop.cpp b/src/interface/python/loop.cpp index c86c74b34..746a457e6 100644 --- a/src/interface/python/loop.cpp +++ b/src/interface/python/loop.cpp @@ -1,29 +1,39 @@ #include "loop.h" -namespace cali { +namespace cali +{ -PythonLoop::PythonLoop(const char *name) { - if (cali_loop_attr_id == CALI_INV_ID) { - cali_init(); - } - cali_begin_string(cali_loop_attr_id, name); - m_iter_attr = cali_make_loop_iteration_attribute(name); +PythonLoop::PythonLoop(const char* name) +{ + if (cali_loop_attr_id == CALI_INV_ID) { + cali_init(); + } + cali_begin_string(cali_loop_attr_id, name); + m_iter_attr = cali_make_loop_iteration_attribute(name); } -void PythonLoop::start_iteration(int i) { cali_begin_int(m_iter_attr, i); } +void PythonLoop::start_iteration(int i) +{ + cali_begin_int(m_iter_attr, i); +} -void PythonLoop::end_iteration() { cali_end(m_iter_attr); } +void PythonLoop::end_iteration() +{ + cali_end(m_iter_attr); +} -void PythonLoop::end() { cali_end(cali_loop_attr_id); } +void PythonLoop::end() +{ + cali_end(cali_loop_attr_id); +} -void create_caliper_loop_mod(py::module_ &caliper_loop_mod) { - py::class_ loop_type(caliper_loop_mod, "Loop"); - loop_type.def(py::init(), "Create a loop annotation."); - loop_type.def("start_iteration", &PythonLoop::start_iteration, - "Start a loop iteration."); - loop_type.def("end_iteration", &PythonLoop::end_iteration, - "End a loop iteration."); - loop_type.def("end", &PythonLoop::end, "End the loop annotation."); +void create_caliper_loop_mod(py::module_& caliper_loop_mod) +{ + py::class_ loop_type(caliper_loop_mod, "Loop"); + loop_type.def(py::init(), "Create a loop annotation."); + loop_type.def("start_iteration", &PythonLoop::start_iteration, "Start a loop iteration."); + loop_type.def("end_iteration", &PythonLoop::end_iteration, "End a loop iteration."); + loop_type.def("end", &PythonLoop::end, "End the loop annotation."); } } // namespace cali \ No newline at end of file diff --git a/src/interface/python/loop.h b/src/interface/python/loop.h index eb483c073..5bc740638 100644 --- a/src/interface/python/loop.h +++ b/src/interface/python/loop.h @@ -3,23 +3,27 @@ #include "common.h" -namespace cali { +namespace cali +{ -class PythonLoop { +class PythonLoop +{ public: - PythonLoop(const char *name); - void start_iteration(int i); + PythonLoop(const char* name); - void end_iteration(); + void start_iteration(int i); - void end(); + void end_iteration(); + + void end(); private: - cali_id_t m_iter_attr; + + cali_id_t m_iter_attr; }; -void create_caliper_loop_mod(py::module_ &caliper_loop_mod); +void create_caliper_loop_mod(py::module_& caliper_loop_mod); } // namespace cali diff --git a/src/interface/python/mod.cpp b/src/interface/python/mod.cpp index 83f53e0f6..c93bf7747 100644 --- a/src/interface/python/mod.cpp +++ b/src/interface/python/mod.cpp @@ -3,21 +3,28 @@ #include "instrumentation.h" #include "loop.h" -bool pycaliper_is_initialized() { return cali_is_initialized() != 0; } +bool pycaliper_is_initialized() +{ + return cali_is_initialized() != 0; +} -PYBIND11_MODULE(__pycaliper_impl, m) { - m.attr("__version__") = cali_caliper_version(); +PYBIND11_MODULE(__pycaliper_impl, m) +{ + m.attr("__version__") = cali_caliper_version(); - m.def( - "config_preset", - [](std::map &preset_map) { - for (auto kv : preset_map) { - cali_config_preset(kv.first, kv.second); - } - }, - "Pre-set a config entry in the default config." - "The entry can still be overwritten by environment variables."); - m.def("init", &cali_init, + m.def( + "config_preset", + [](std::map& preset_map) { + for (auto kv : preset_map) { + cali_config_preset(kv.first, kv.second); + } + }, + "Pre-set a config entry in the default config." + "The entry can still be overwritten by environment variables." + ); + m.def( + "init", + &cali_init, "Initialize Caliper." "Typically, it is not necessary to initialize Caliper explicitly." "Caliper will lazily initialize itself on the first Caliper API call." @@ -25,62 +32,57 @@ PYBIND11_MODULE(__pycaliper_impl, m) { "to ensure that Caliper's pre-defined annotation attributes are" "initialized." "It can also be used to avoid high initialization costs in the first" - "Caliper API call."); - m.def("is_initialized", &pycaliper_is_initialized, - "Check if Caliper is initialized on this process."); + "Caliper API call." + ); + m.def("is_initialized", &pycaliper_is_initialized, "Check if Caliper is initialized on this process."); - auto types_mod = m.def_submodule( - "types", "Special types used by lower-level Caliper APIs."); + auto types_mod = m.def_submodule("types", "Special types used by lower-level Caliper APIs."); - py::enum_ c_attr_type(types_mod, "AttrType"); - c_attr_type.value("CALI_TYPE_INV", CALI_TYPE_INV); - c_attr_type.value("CALI_TYPE_USR", CALI_TYPE_USR); - c_attr_type.value("CALI_TYPE_INT", CALI_TYPE_INT); - c_attr_type.value("CALI_TYPE_UINT", CALI_TYPE_UINT); - c_attr_type.value("CALI_TYPE_STRING", CALI_TYPE_STRING); - c_attr_type.value("CALI_TYPE_ADDR", CALI_TYPE_ADDR); - c_attr_type.value("CALI_TYPE_DOUBLE", CALI_TYPE_DOUBLE); - c_attr_type.value("CALI_TYPE_BOOL", CALI_TYPE_BOOL); - c_attr_type.value("CALI_TYPE_TYPE", CALI_TYPE_TYPE); - c_attr_type.value("CALI_TYPE_PTR", CALI_TYPE_PTR); - c_attr_type.export_values(); + py::enum_ c_attr_type(types_mod, "AttrType"); + c_attr_type.value("CALI_TYPE_INV", CALI_TYPE_INV); + c_attr_type.value("CALI_TYPE_USR", CALI_TYPE_USR); + c_attr_type.value("CALI_TYPE_INT", CALI_TYPE_INT); + c_attr_type.value("CALI_TYPE_UINT", CALI_TYPE_UINT); + c_attr_type.value("CALI_TYPE_STRING", CALI_TYPE_STRING); + c_attr_type.value("CALI_TYPE_ADDR", CALI_TYPE_ADDR); + c_attr_type.value("CALI_TYPE_DOUBLE", CALI_TYPE_DOUBLE); + c_attr_type.value("CALI_TYPE_BOOL", CALI_TYPE_BOOL); + c_attr_type.value("CALI_TYPE_TYPE", CALI_TYPE_TYPE); + c_attr_type.value("CALI_TYPE_PTR", CALI_TYPE_PTR); + c_attr_type.export_values(); - py::enum_ c_attr_properties(types_mod, "AttrProperties", - py::arithmetic()); - c_attr_properties.value("CALI_ATTR_DEFAULT", CALI_ATTR_DEFAULT); - c_attr_properties.value("CALI_ATTR_ASVALUE", CALI_ATTR_ASVALUE); - c_attr_properties.value("CALI_ATTR_NOMERGE", CALI_ATTR_NOMERGE); - c_attr_properties.value("CALI_ATTR_SCOPE_PROCESS", CALI_ATTR_SCOPE_PROCESS); - c_attr_properties.value("CALI_ATTR_SCOPE_THREAD", CALI_ATTR_SCOPE_THREAD); - c_attr_properties.value("CALI_ATTR_SCOPE_TASK", CALI_ATTR_SCOPE_TASK); - c_attr_properties.value("CALI_ATTR_SKIP_EVENTS", CALI_ATTR_SKIP_EVENTS); - c_attr_properties.value("CALI_ATTR_HIDDEN", CALI_ATTR_HIDDEN); - c_attr_properties.value("CALI_ATTR_NESTED", CALI_ATTR_NESTED); - c_attr_properties.value("CALI_ATTR_GLOBAL", CALI_ATTR_GLOBAL); - c_attr_properties.value("CALI_ATTR_UNALIGNED", CALI_ATTR_UNALIGNED); - c_attr_properties.value("CALI_ATTR_AGGREGATABLE", CALI_ATTR_AGGREGATABLE); - c_attr_properties.value("CALI_ATTR_LEVEL_1", CALI_ATTR_LEVEL_1); - c_attr_properties.value("CALI_ATTR_LEVEL_2", CALI_ATTR_LEVEL_2); - c_attr_properties.value("CALI_ATTR_LEVEL_3", CALI_ATTR_LEVEL_3); - c_attr_properties.value("CALI_ATTR_LEVEL_4", CALI_ATTR_LEVEL_4); - c_attr_properties.value("CALI_ATTR_LEVEL_5", CALI_ATTR_LEVEL_5); - c_attr_properties.value("CALI_ATTR_LEVEL_6", CALI_ATTR_LEVEL_6); - c_attr_properties.value("CALI_ATTR_LEVEL_7", CALI_ATTR_LEVEL_7); - c_attr_properties.export_values(); + py::enum_ c_attr_properties(types_mod, "AttrProperties", py::arithmetic()); + c_attr_properties.value("CALI_ATTR_DEFAULT", CALI_ATTR_DEFAULT); + c_attr_properties.value("CALI_ATTR_ASVALUE", CALI_ATTR_ASVALUE); + c_attr_properties.value("CALI_ATTR_NOMERGE", CALI_ATTR_NOMERGE); + c_attr_properties.value("CALI_ATTR_SCOPE_PROCESS", CALI_ATTR_SCOPE_PROCESS); + c_attr_properties.value("CALI_ATTR_SCOPE_THREAD", CALI_ATTR_SCOPE_THREAD); + c_attr_properties.value("CALI_ATTR_SCOPE_TASK", CALI_ATTR_SCOPE_TASK); + c_attr_properties.value("CALI_ATTR_SKIP_EVENTS", CALI_ATTR_SKIP_EVENTS); + c_attr_properties.value("CALI_ATTR_HIDDEN", CALI_ATTR_HIDDEN); + c_attr_properties.value("CALI_ATTR_NESTED", CALI_ATTR_NESTED); + c_attr_properties.value("CALI_ATTR_GLOBAL", CALI_ATTR_GLOBAL); + c_attr_properties.value("CALI_ATTR_UNALIGNED", CALI_ATTR_UNALIGNED); + c_attr_properties.value("CALI_ATTR_AGGREGATABLE", CALI_ATTR_AGGREGATABLE); + c_attr_properties.value("CALI_ATTR_LEVEL_1", CALI_ATTR_LEVEL_1); + c_attr_properties.value("CALI_ATTR_LEVEL_2", CALI_ATTR_LEVEL_2); + c_attr_properties.value("CALI_ATTR_LEVEL_3", CALI_ATTR_LEVEL_3); + c_attr_properties.value("CALI_ATTR_LEVEL_4", CALI_ATTR_LEVEL_4); + c_attr_properties.value("CALI_ATTR_LEVEL_5", CALI_ATTR_LEVEL_5); + c_attr_properties.value("CALI_ATTR_LEVEL_6", CALI_ATTR_LEVEL_6); + c_attr_properties.value("CALI_ATTR_LEVEL_7", CALI_ATTR_LEVEL_7); + c_attr_properties.export_values(); - auto annotation_mod = - m.def_submodule("annotation", "Support for Caliper annotation APIs."); - cali::create_caliper_annotation_mod(annotation_mod); + auto annotation_mod = m.def_submodule("annotation", "Support for Caliper annotation APIs."); + cali::create_caliper_annotation_mod(annotation_mod); - auto instrumentation_mod = - m.def_submodule("instrumentation", - "Support for higher-level Caliper instrumentation APIs"); - cali::create_caliper_instrumentation_mod(instrumentation_mod); + auto instrumentation_mod = + m.def_submodule("instrumentation", "Support for higher-level Caliper instrumentation APIs"); + cali::create_caliper_instrumentation_mod(instrumentation_mod); - auto loop_mod = m.def_submodule("loop", "Support for loop annotations."); - cali::create_caliper_loop_mod(loop_mod); + auto loop_mod = m.def_submodule("loop", "Support for loop annotations."); + cali::create_caliper_loop_mod(loop_mod); - auto config_mgr_mod = m.def_submodule( - "config_manager", "Support for dynamic configuration of Caliper."); - cali::create_caliper_config_manager_mod(config_mgr_mod); + auto config_mgr_mod = m.def_submodule("config_manager", "Support for dynamic configuration of Caliper."); + cali::create_caliper_config_manager_mod(config_mgr_mod); } \ No newline at end of file diff --git a/src/reader/Aggregator.cpp b/src/reader/Aggregator.cpp index 15b445de0..1ebc79cdc 100644 --- a/src/reader/Aggregator.cpp +++ b/src/reader/Aggregator.cpp @@ -31,10 +31,12 @@ using namespace std; namespace { -std::vector -make_key(std::vector::const_iterator nodes_begin, - std::vector::const_iterator nodes_end, - const std::vector& immediates, CaliperMetadataAccessInterface& db) +std::vector make_key( + std::vector::const_iterator nodes_begin, + std::vector::const_iterator nodes_end, + const std::vector& immediates, + CaliperMetadataAccessInterface& db +) { std::vector key; key.reserve(immediates.size() + 1); @@ -52,8 +54,7 @@ make_key(std::vector::const_iterator nodes_begin, return key; } -std::size_t -hash_key(const std::vector& key) +std::size_t hash_key(const std::vector& key) { std::size_t hash = 0; for (const Entry& e : key) { @@ -64,32 +65,28 @@ hash_key(const std::vector& key) return hash; } - class AggregateKernelConfig; -class AggregateKernel { +class AggregateKernel +{ public: - virtual ~AggregateKernel() - { } + virtual ~AggregateKernel() {} virtual const AggregateKernelConfig* config() = 0; // For inclusive metrics, parent_aggregate is invoked for parent nodes - virtual void parent_aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - aggregate(db, list); - } + virtual void parent_aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { aggregate(db, list); } virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) = 0; - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) = 0; + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) = 0; }; class AggregateKernelConfig { public: - virtual ~AggregateKernelConfig() - { } + virtual ~AggregateKernelConfig() {} virtual bool is_inclusive() const { return false; } @@ -100,40 +97,37 @@ class AggregateKernelConfig // --- CountKernel // -class CountKernel : public AggregateKernel { +class CountKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { + class Config : public AggregateKernelConfig + { Attribute m_attr; public: - Attribute attribute(CaliperMetadataAccessInterface& db) { + Attribute attribute(CaliperMetadataAccessInterface& db) + { if (!m_attr) m_attr = db.create_attribute("count", CALI_TYPE_UINT, CALI_ATTR_ASVALUE); return m_attr; } - AggregateKernel* make_kernel() { - return new CountKernel(this); - } + AggregateKernel* make_kernel() { return new CountKernel(this); } - Config() - { } + Config() {} - static AggregateKernelConfig* create(const std::vector&) { - return new Config; - } + static AggregateKernelConfig* create(const std::vector&) { return new Config; } }; - CountKernel(Config* config) - : m_count(0), m_config(config) - { } + CountKernel(Config* config) : m_count(0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { + void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { cali_id_t count_attr_id = m_config->attribute(db).id(); for (const Entry& e : list) @@ -145,78 +139,74 @@ class CountKernel : public AggregateKernel { ++m_count; } - void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { uint64_t count = m_count.load(); if (count > 0) - list.push_back(Entry(m_config->attribute(db), - Variant(CALI_TYPE_UINT, &count, sizeof(uint64_t)))); + list.push_back(Entry(m_config->attribute(db), Variant(CALI_TYPE_UINT, &count, sizeof(uint64_t)))); } private: std::atomic m_count; - Config* m_config; + Config* m_config; }; -class ScaledCountKernel : public AggregateKernel { +class ScaledCountKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { - Attribute m_count_attr; - Attribute m_res_attr; + class Config : public AggregateKernelConfig + { + Attribute m_count_attr; + Attribute m_res_attr; double m_scale; std::string m_scale_str; public: - Attribute get_count_attr(CaliperMetadataAccessInterface& db) { + Attribute get_count_attr(CaliperMetadataAccessInterface& db) + { if (!m_count_attr) { - m_count_attr = - db.create_attribute(std::string("scount#")+m_scale_str, CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN); + m_count_attr = db.create_attribute( + std::string("scount#") + m_scale_str, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); } return m_count_attr; } - Attribute get_result_attr(CaliperMetadataAccessInterface& db) { + Attribute get_result_attr(CaliperMetadataAccessInterface& db) + { if (!m_res_attr) - m_res_attr = - db.create_attribute(std::string("scount"), CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE); + m_res_attr = db.create_attribute(std::string("scount"), CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); return m_res_attr; } - double get_scale() const { return m_scale; } - - AggregateKernel* make_kernel() { - return new ScaledCountKernel(this); - } + double get_scale() const { return m_scale; } - explicit Config(const std::vector& cfg) - : m_scale(0.0), m_scale_str(cfg[0]) - { - m_scale = std::stod(m_scale_str); - } + AggregateKernel* make_kernel() { return new ScaledCountKernel(this); } - static AggregateKernelConfig* create(const std::vector& cfg) { - return new Config(cfg); + explicit Config(const std::vector& cfg) : m_scale(0.0), m_scale_str(cfg[0]) + { + m_scale = std::stod(m_scale_str); } + + static AggregateKernelConfig* create(const std::vector& cfg) { return new Config(cfg); } }; - ScaledCountKernel(Config* config) - : m_count(0), m_config(config) - { } + ScaledCountKernel(Config* config) : m_count(0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); Attribute count_attr = m_config->get_count_attr(db); @@ -229,9 +219,10 @@ class ScaledCountKernel : public AggregateKernel { ++m_count; } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { if (m_count > 0) { - list.push_back(Entry(m_config->get_count_attr(db), Variant(m_count))); + list.push_back(Entry(m_config->get_count_attr(db), Variant(m_count))); list.push_back(Entry(m_config->get_result_attr(db), Variant(m_config->get_scale() * m_count))); } } @@ -241,69 +232,70 @@ class ScaledCountKernel : public AggregateKernel { uint64_t m_count; std::mutex m_lock; - Config* m_config; + Config* m_config; }; // // --- SumKernel // -class SumKernel : public AggregateKernel { +class SumKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { + class Config : public AggregateKernelConfig + { std::string m_target_attr_name; - Attribute m_target_attr; - Attribute m_sum_attr; + Attribute m_target_attr; + Attribute m_sum_attr; - bool m_is_inclusive; + bool m_is_inclusive; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - Attribute get_sum_attr(CaliperMetadataAccessInterface& db) { + Attribute get_sum_attr(CaliperMetadataAccessInterface& db) + { if (!m_sum_attr) - m_sum_attr = - db.create_attribute(std::string(m_is_inclusive ? "inclusive#" : "sum#") + m_target_attr_name, m_target_attr.type(), - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); + m_sum_attr = db.create_attribute( + std::string(m_is_inclusive ? "inclusive#" : "sum#") + m_target_attr_name, + m_target_attr.type(), + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE + ); return m_sum_attr; } bool is_inclusive() const { return m_is_inclusive; } - AggregateKernel* make_kernel() { - return new SumKernel(this); - } + AggregateKernel* make_kernel() { return new SumKernel(this); } - Config(const std::string& name, bool inclusive) - : m_target_attr_name(name), - m_is_inclusive(inclusive) - { } + Config(const std::string& name, bool inclusive) : m_target_attr_name(name), m_is_inclusive(inclusive) {} - static AggregateKernelConfig* create(const std::vector& cfg) { + static AggregateKernelConfig* create(const std::vector& cfg) + { return new Config(cfg.front(), false); } - static AggregateKernelConfig* create_inclusive(const std::vector& cfg) { + static AggregateKernelConfig* create_inclusive(const std::vector& cfg) + { return new Config(cfg.front(), true); } }; - SumKernel(Config* config) - : m_count(0), m_config(config) - { } + SumKernel(Config* config) : m_count(0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& rec) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& rec) + { + std::lock_guard g(m_lock); Attribute target_attr = m_config->get_target_attr(db); @@ -324,7 +316,8 @@ class SumKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& rec) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& rec) + { if (m_count > 0) rec.push_back(Entry(m_config->get_sum_attr(db), m_sum)); } @@ -337,81 +330,83 @@ class SumKernel : public AggregateKernel { Config* m_config; }; -class ScaledSumKernel : public AggregateKernel { +class ScaledSumKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { + class Config : public AggregateKernelConfig + { std::string m_target_attr_name; Attribute m_target_attr; Attribute m_sum_attr; - Attribute m_res_attr; + Attribute m_res_attr; - double m_scale; - bool m_inclusive; + double m_scale; + bool m_inclusive; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - Attribute get_sum_attr(CaliperMetadataAccessInterface& db) { + Attribute get_sum_attr(CaliperMetadataAccessInterface& db) + { if (!m_sum_attr) - m_sum_attr = - db.create_attribute(std::string(m_inclusive ? "iscsum#" : "scsum#")+m_target_attr_name, CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN); + m_sum_attr = db.create_attribute( + std::string(m_inclusive ? "iscsum#" : "scsum#") + m_target_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); return m_sum_attr; } - Attribute get_result_attr(CaliperMetadataAccessInterface& db) { + Attribute get_result_attr(CaliperMetadataAccessInterface& db) + { if (!m_res_attr) - m_res_attr = - db.create_attribute(std::string(m_inclusive ? "iscale#" : "scale#")+m_target_attr_name, CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE); + m_res_attr = db.create_attribute( + std::string(m_inclusive ? "iscale#" : "scale#") + m_target_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE + ); return m_res_attr; } - double get_scale() const { return m_scale; } - bool is_inclusive() const { return m_inclusive; } + double get_scale() const { return m_scale; } - AggregateKernel* make_kernel() { - return new ScaledSumKernel(this); - } + bool is_inclusive() const { return m_inclusive; } - Config(const std::vector& cfg, bool inclusive) - : m_target_attr_name(cfg[0]), - m_scale(0.0), - m_inclusive(inclusive) - { - if (cfg.size() > 1) - m_scale = std::stod(cfg[1]); - } + AggregateKernel* make_kernel() { return new ScaledSumKernel(this); } - static AggregateKernelConfig* create(const std::vector& cfg) { - return new Config(cfg, false); + Config(const std::vector& cfg, bool inclusive) + : m_target_attr_name(cfg[0]), m_scale(0.0), m_inclusive(inclusive) + { + if (cfg.size() > 1) + m_scale = std::stod(cfg[1]); } - static AggregateKernelConfig* create_inclusive(const std::vector& cfg) { + static AggregateKernelConfig* create(const std::vector& cfg) { return new Config(cfg, false); } + + static AggregateKernelConfig* create_inclusive(const std::vector& cfg) + { return new Config(cfg, true); } }; - ScaledSumKernel(Config* config) - : m_count(0), m_sum(0.0), m_config(config) - { } + ScaledSumKernel(Config* config) : m_count(0), m_sum(0.0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); Attribute target_attr = m_config->get_target_attr(db); - Attribute sum_attr = m_config->get_sum_attr(db); + Attribute sum_attr = m_config->get_sum_attr(db); for (const Entry& e : list) { if (e.attribute() == target_attr.id() || e.attribute() == sum_attr.id()) { @@ -421,51 +416,58 @@ class ScaledSumKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { if (m_count > 0) { - list.push_back(Entry(m_config->get_sum_attr(db), Variant(m_sum))); + list.push_back(Entry(m_config->get_sum_attr(db), Variant(m_sum))); list.push_back(Entry(m_config->get_result_attr(db), Variant(m_config->get_scale() * m_sum))); } } private: - unsigned m_count; - double m_sum; + unsigned m_count; + double m_sum; std::mutex m_lock; - Config* m_config; + Config* m_config; }; -class MinKernel : public AggregateKernel { +class MinKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { - std::string m_target_attr_name; - Attribute m_target_attr; - Attribute m_min_attr; + class Config : public AggregateKernelConfig + { + std::string m_target_attr_name; + Attribute m_target_attr; + Attribute m_min_attr; - bool m_inclusive; + bool m_inclusive; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - Attribute get_min_attr(CaliperMetadataAccessInterface& db) { + Attribute get_min_attr(CaliperMetadataAccessInterface& db) + { if (!m_min_attr) { if (m_target_attr) { cali_attr_type type = m_target_attr.type(); int prop = CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE; - m_min_attr = - db.create_attribute(std::string(m_inclusive ? "imin#" : "min#") + m_target_attr_name, type, prop); + m_min_attr = db.create_attribute( + std::string(m_inclusive ? "imin#" : "min#") + m_target_attr_name, + type, + prop + ); } - } return m_min_attr; @@ -473,35 +475,31 @@ class MinKernel : public AggregateKernel { bool is_inclusive() const { return m_inclusive; } - AggregateKernel* make_kernel() { - return new MinKernel(this); - } + AggregateKernel* make_kernel() { return new MinKernel(this); } - Config(const std::string& name, bool inclusive) - : m_target_attr_name(name), - m_inclusive(inclusive) - { } + Config(const std::string& name, bool inclusive) : m_target_attr_name(name), m_inclusive(inclusive) {} - static AggregateKernelConfig* create(const std::vector& cfg) { + static AggregateKernelConfig* create(const std::vector& cfg) + { return new Config(cfg.front(), false); } - static AggregateKernelConfig* create_inclusive(const std::vector& cfg) { + + static AggregateKernelConfig* create_inclusive(const std::vector& cfg) + { return new Config(cfg.front(), true); } }; - MinKernel(Config* config) - : m_config(config) - { } + MinKernel(Config* config) : m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); Attribute target_attr = m_config->get_target_attr(db); - Attribute min_attr = m_config->get_min_attr(db); + Attribute min_attr = m_config->get_min_attr(db); if (!min_attr) return; @@ -512,7 +510,8 @@ class MinKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { if (!m_min.empty()) list.push_back(Entry(m_config->get_min_attr(db), m_min)); } @@ -524,33 +523,39 @@ class MinKernel : public AggregateKernel { Config* m_config; }; -class MaxKernel : public AggregateKernel { +class MaxKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { - std::string m_target_attr_name; - Attribute m_target_attr; - Attribute m_max_attr; - bool m_inclusive; + class Config : public AggregateKernelConfig + { + std::string m_target_attr_name; + Attribute m_target_attr; + Attribute m_max_attr; + bool m_inclusive; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - Attribute get_max_attr(CaliperMetadataAccessInterface& db) { + Attribute get_max_attr(CaliperMetadataAccessInterface& db) + { if (!m_max_attr) { if (m_target_attr) { cali_attr_type type = m_target_attr.type(); int prop = CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE; - m_max_attr = - db.create_attribute(std::string(m_inclusive ? "imax#" : "max#") + m_target_attr_name, type, prop); + m_max_attr = db.create_attribute( + std::string(m_inclusive ? "imax#" : "max#") + m_target_attr_name, + type, + prop + ); } - } return m_max_attr; @@ -558,35 +563,31 @@ class MaxKernel : public AggregateKernel { bool is_inclusive() const { return m_inclusive; } - AggregateKernel* make_kernel() { - return new MaxKernel(this); - } + AggregateKernel* make_kernel() { return new MaxKernel(this); } - Config(const std::string& name, bool inclusive) - : m_target_attr_name(name), - m_inclusive(inclusive) - { } + Config(const std::string& name, bool inclusive) : m_target_attr_name(name), m_inclusive(inclusive) {} - static AggregateKernelConfig* create(const std::vector& cfg) { + static AggregateKernelConfig* create(const std::vector& cfg) + { return new Config(cfg.front(), false); } - static AggregateKernelConfig* create_inclusive(const std::vector& cfg) { + + static AggregateKernelConfig* create_inclusive(const std::vector& cfg) + { return new Config(cfg.front(), true); } }; - MaxKernel(Config* config) - : m_config(config) - { } + MaxKernel(Config* config) : m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); Attribute target_attr = m_config->get_target_attr(db); - Attribute max_attr = m_config->get_max_attr(db); + Attribute max_attr = m_config->get_max_attr(db); if (!max_attr) return; @@ -597,7 +598,8 @@ class MaxKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { if (!m_max.empty()) list.push_back(Entry(m_config->get_max_attr(db), m_max)); } @@ -609,7 +611,8 @@ class MaxKernel : public AggregateKernel { Config* m_config; }; -class AvgKernel : public AggregateKernel { +class AvgKernel : public AggregateKernel +{ public: struct StatisticsAttributes { @@ -618,21 +621,24 @@ class AvgKernel : public AggregateKernel { Attribute count; }; - class Config : public AggregateKernelConfig { - std::string m_target_attr_name; - Attribute m_target_attr; + class Config : public AggregateKernelConfig + { + std::string m_target_attr_name; + Attribute m_target_attr; StatisticsAttributes m_stat_attrs; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - bool get_statistics_attributes(CaliperMetadataAccessInterface& db, StatisticsAttributes& a) { + bool get_statistics_attributes(CaliperMetadataAccessInterface& db, StatisticsAttributes& a) + { if (!m_target_attr) return false; if (a.sum) { @@ -642,41 +648,32 @@ class AvgKernel : public AggregateKernel { int prop = CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE; - m_stat_attrs.avg = - db.create_attribute("avg#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop); + m_stat_attrs.avg = db.create_attribute("avg#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop); m_stat_attrs.count = - db.create_attribute("avg.count#" + m_target_attr_name, CALI_TYPE_UINT, prop | CALI_ATTR_HIDDEN); + db.create_attribute("avg.count#" + m_target_attr_name, CALI_TYPE_UINT, prop | CALI_ATTR_HIDDEN); m_stat_attrs.sum = - db.create_attribute("avg.sum#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop | CALI_ATTR_HIDDEN); + db.create_attribute("avg.sum#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop | CALI_ATTR_HIDDEN); a = m_stat_attrs; return true; } - AggregateKernel* make_kernel() { - return new AvgKernel(this); - } + AggregateKernel* make_kernel() { return new AvgKernel(this); } - Config(const std::string& name) - : m_target_attr_name(name) - { } + Config(const std::string& name) : m_target_attr_name(name) {} - static AggregateKernelConfig* create(const std::vector& cfg) { - return new Config(cfg.front()); - } + static AggregateKernelConfig* create(const std::vector& cfg) { return new Config(cfg.front()); } }; - AvgKernel(Config* config) - : m_count(0), m_sum(0.0), m_config(config) - { } + AvgKernel(Config* config) : m_count(0), m_sum(0.0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); - Attribute target_attr = m_config->get_target_attr(db); + Attribute target_attr = m_config->get_target_attr(db); StatisticsAttributes stat_attr; if (!m_config->get_statistics_attributes(db, stat_attr)) @@ -694,7 +691,8 @@ class AvgKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { if (m_count > 0) { StatisticsAttributes stat_attr; @@ -709,107 +707,109 @@ class AvgKernel : public AggregateKernel { private: - unsigned m_count; - double m_sum; + unsigned m_count; + double m_sum; std::mutex m_lock; - Config* m_config; + Config* m_config; }; // // --- ScaledRatioKernel // -class ScaledRatioKernel : public AggregateKernel { +class ScaledRatioKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { + class Config : public AggregateKernelConfig + { std::string m_tgt1_attr_name; std::string m_tgt2_attr_name; - Attribute m_tgt1_attr; - Attribute m_tgt2_attr; - Attribute m_sum1_attr; - Attribute m_sum2_attr; + Attribute m_tgt1_attr; + Attribute m_tgt2_attr; + Attribute m_sum1_attr; + Attribute m_sum2_attr; - Attribute m_ratio_attr; + Attribute m_ratio_attr; - double m_scale; - bool m_inclusive; + double m_scale; + bool m_inclusive; public: - double get_scale() const { return m_scale; } - bool is_inclusive() const { return m_inclusive; } + double get_scale() const { return m_scale; } - std::pair get_target_attributes(CaliperMetadataAccessInterface& db) { + bool is_inclusive() const { return m_inclusive; } + + std::pair get_target_attributes(CaliperMetadataAccessInterface& db) + { if (!m_tgt1_attr) m_tgt1_attr = db.get_attribute(m_tgt1_attr_name); if (!m_tgt2_attr) m_tgt2_attr = db.get_attribute(m_tgt2_attr_name); - return std::pair(m_tgt1_attr, m_tgt2_attr); + return std::pair(m_tgt1_attr, m_tgt2_attr); } - std::pair get_sum_attributes(CaliperMetadataAccessInterface& db) { + std::pair get_sum_attributes(CaliperMetadataAccessInterface& db) + { if (!m_sum1_attr) - m_sum1_attr = - db.create_attribute(std::string(m_inclusive ? "isr.sum#" : "sr.sum#") + m_tgt1_attr_name, CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN); + m_sum1_attr = db.create_attribute( + std::string(m_inclusive ? "isr.sum#" : "sr.sum#") + m_tgt1_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); if (!m_sum2_attr) - m_sum2_attr = - db.create_attribute(std::string(m_inclusive ? "isr.sum#" : "sr.sum#") + m_tgt2_attr_name, CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN); + m_sum2_attr = db.create_attribute( + std::string(m_inclusive ? "isr.sum#" : "sr.sum#") + m_tgt2_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); return std::make_pair(m_sum1_attr, m_sum2_attr); } - Attribute get_ratio_attribute(CaliperMetadataAccessInterface& db) { + Attribute get_ratio_attribute(CaliperMetadataAccessInterface& db) + { if (!m_ratio_attr) - m_ratio_attr = - db.create_attribute(std::string(m_inclusive ? "iratio#" : "ratio#") + m_tgt1_attr_name + "/" + m_tgt2_attr_name, - CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); + m_ratio_attr = db.create_attribute( + std::string(m_inclusive ? "iratio#" : "ratio#") + m_tgt1_attr_name + "/" + m_tgt2_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE + ); return m_ratio_attr; } - AggregateKernel* make_kernel() { - return new ScaledRatioKernel(this); - } + AggregateKernel* make_kernel() { return new ScaledRatioKernel(this); } Config(const std::vector& cfg, bool is_inclusive) : m_tgt1_attr_name(cfg[0]), // We have already checked that there are two strings given m_tgt2_attr_name(cfg[1]), m_scale(1.0), m_inclusive(is_inclusive) - { - if (cfg.size() > 2) - m_scale = std::stod(cfg[2]); - } - - static AggregateKernelConfig* create(const std::vector& cfg) { - return new Config(cfg, false); + { + if (cfg.size() > 2) + m_scale = std::stod(cfg[2]); } - static AggregateKernelConfig* create_inclusive(const std::vector& cfg) { + static AggregateKernelConfig* create(const std::vector& cfg) { return new Config(cfg, false); } + + static AggregateKernelConfig* create_inclusive(const std::vector& cfg) + { return new Config(cfg, true); } }; - ScaledRatioKernel(Config* config) - : m_sum1(0), m_sum2(0), m_count(0), m_config(config) - { } + ScaledRatioKernel(Config* config) : m_sum1(0), m_sum2(0), m_count(0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); auto tattrs = m_config->get_target_attributes(db); auto sattrs = m_config->get_sum_attributes(db); @@ -817,7 +817,7 @@ class ScaledRatioKernel : public AggregateKernel { for (const Entry& e : list) { cali_id_t attr = e.attribute(); - if (attr == tattrs.first.id() || attr == sattrs.first.id()) { + if (attr == tattrs.first.id() || attr == sattrs.first.id()) { m_sum1 += e.value().to_double(); ++m_count; } else if (attr == tattrs.second.id() || attr == sattrs.second.id()) { @@ -826,25 +826,27 @@ class ScaledRatioKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { auto sum_attrs = m_config->get_sum_attributes(db); if (m_count > 0 && m_sum1 > 0) - list.push_back(Entry(sum_attrs.first, Variant(m_sum1))); + list.push_back(Entry(sum_attrs.first, Variant(m_sum1))); if (m_sum2 > 0) { list.push_back(Entry(sum_attrs.second, Variant(m_sum2))); if (m_count > 0) - list.push_back(Entry(m_config->get_ratio_attribute(db), - Variant(m_config->get_scale() * m_sum1 / m_sum2))); + list.push_back( + Entry(m_config->get_ratio_attribute(db), Variant(m_config->get_scale() * m_sum1 / m_sum2)) + ); } } private: - double m_sum1; - double m_sum2; - int m_count; + double m_sum1; + double m_sum2; + int m_count; std::mutex m_lock; @@ -855,97 +857,95 @@ class ScaledRatioKernel : public AggregateKernel { // --- PercentTotalKernel // -class PercentTotalKernel : public AggregateKernel { +class PercentTotalKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { + class Config : public AggregateKernelConfig + { std::string m_target_attr_name; Attribute m_target_attr; Attribute m_sum_attr; - Attribute m_percentage_attr; + Attribute m_percentage_attr; - std::mutex m_total_lock; - double m_total; + std::mutex m_total_lock; + double m_total; - bool m_is_inclusive; + bool m_is_inclusive; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - bool get_percentage_attribute(CaliperMetadataAccessInterface& db, - Attribute& percentage_attr, - Attribute& sum_attr) { + bool get_percentage_attribute( + CaliperMetadataAccessInterface& db, + Attribute& percentage_attr, + Attribute& sum_attr + ) + { if (!m_target_attr) return false; if (m_percentage_attr) { percentage_attr = m_percentage_attr; - sum_attr = m_sum_attr; + sum_attr = m_sum_attr; return true; } - m_percentage_attr = - db.create_attribute(std::string(m_is_inclusive ? "ipercent_total#" : "percent_total#") + m_target_attr_name, - CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); + m_percentage_attr = db.create_attribute( + std::string(m_is_inclusive ? "ipercent_total#" : "percent_total#") + m_target_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE + ); - m_sum_attr = - db.create_attribute(std::string(m_is_inclusive ? "ipct.sum#" : "pct.sum#") + m_target_attr_name, - CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN); + m_sum_attr = db.create_attribute( + std::string(m_is_inclusive ? "ipct.sum#" : "pct.sum#") + m_target_attr_name, + CALI_TYPE_DOUBLE, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); percentage_attr = m_percentage_attr; - sum_attr = m_sum_attr; + sum_attr = m_sum_attr; return true; } - AggregateKernel* make_kernel() { - return new PercentTotalKernel(this); - } + AggregateKernel* make_kernel() { return new PercentTotalKernel(this); } - void add(double val) { - std::lock_guard - g(m_total_lock); + void add(double val) + { + std::lock_guard g(m_total_lock); m_total += val; } - double get_total() { - return m_total; - } + double get_total() { return m_total; } - bool is_inclusive() const { - return m_is_inclusive; - } + bool is_inclusive() const { return m_is_inclusive; } Config(const std::vector& names, bool inclusive) - : m_target_attr_name(names.front()), - m_total(0), - m_is_inclusive(inclusive) - { } + : m_target_attr_name(names.front()), m_total(0), m_is_inclusive(inclusive) + {} - static AggregateKernelConfig* create(const std::vector& cfg) { - return new Config(cfg, false); - } + static AggregateKernelConfig* create(const std::vector& cfg) { return new Config(cfg, false); } - static AggregateKernelConfig* create_inclusive(const std::vector& cfg) { + static AggregateKernelConfig* create_inclusive(const std::vector& cfg) + { return new Config(cfg, true); } }; - PercentTotalKernel(Config* config) - : m_sum(0), m_isum(0), m_config(config) - { } + PercentTotalKernel(Config* config) : m_sum(0), m_isum(0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { + void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { Attribute target_attr = m_config->get_target_attr(db); Attribute percentage_attr, sum_attr; @@ -960,14 +960,15 @@ class PercentTotalKernel : public AggregateKernel { if (id == target_id || id == sum_id) { double val = e.value().to_double(); - m_sum += val; + m_sum += val; m_isum += val; m_config->add(val); } } } - void parent_aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { + void parent_aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { Attribute target_attr = m_config->get_target_attr(db); Attribute percentage_attr, sum_attr; @@ -985,7 +986,8 @@ class PercentTotalKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { double total = m_config->get_total(); if (total > 0) { @@ -1001,8 +1003,8 @@ class PercentTotalKernel : public AggregateKernel { private: - double m_sum; - double m_isum; // inclusive sum + double m_sum; + double m_isum; // inclusive sum std::mutex m_lock; Config* m_config; @@ -1012,55 +1014,57 @@ class PercentTotalKernel : public AggregateKernel { // --- AnyKernel // -class AnyKernel : public AggregateKernel { +class AnyKernel : public AggregateKernel +{ public: - class Config : public AggregateKernelConfig { + class Config : public AggregateKernelConfig + { std::string m_target_attr_name; - Attribute m_target_attr; - Attribute m_any_attr; + Attribute m_target_attr; + Attribute m_any_attr; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - Attribute get_any_attr(CaliperMetadataAccessInterface& db) { + Attribute get_any_attr(CaliperMetadataAccessInterface& db) + { if (!m_any_attr) { if (m_target_attr) - m_any_attr = db.create_attribute(std::string("any#") + m_target_attr_name, m_target_attr.type(), - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); + m_any_attr = db.create_attribute( + std::string("any#") + m_target_attr_name, + m_target_attr.type(), + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE + ); } return m_any_attr; } - AggregateKernel* make_kernel() { - return new AnyKernel(this); - } + AggregateKernel* make_kernel() { return new AnyKernel(this); } - Config(const std::string& name, bool inclusive) - : m_target_attr_name(name) - { } + Config(const std::string& name, bool inclusive) : m_target_attr_name(name) {} - static AggregateKernelConfig* create(const std::vector& cfg) { + static AggregateKernelConfig* create(const std::vector& cfg) + { return new Config(cfg.front(), false); } }; - AnyKernel(Config* config) - : m_count(0), m_config(config) - { } + AnyKernel(Config* config) : m_count(0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& rec) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& rec) + { + std::lock_guard g(m_lock); if (m_val.empty()) { Attribute target_attr = m_config->get_target_attr(db); @@ -1083,7 +1087,8 @@ class AnyKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& rec) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& rec) + { if (m_count > 0) rec.push_back(Entry(m_config->get_any_attr(db), m_val)); } @@ -1096,7 +1101,8 @@ class AnyKernel : public AggregateKernel { Config* m_config; }; -class VarianceKernel : public AggregateKernel { +class VarianceKernel : public AggregateKernel +{ public: struct StatisticsAttributes { @@ -1106,21 +1112,24 @@ class VarianceKernel : public AggregateKernel { Attribute count; }; - class Config : public AggregateKernelConfig { - std::string m_target_attr_name; - Attribute m_target_attr; + class Config : public AggregateKernelConfig + { + std::string m_target_attr_name; + Attribute m_target_attr; StatisticsAttributes m_stat_attrs; public: - Attribute get_target_attr(CaliperMetadataAccessInterface& db) { + Attribute get_target_attr(CaliperMetadataAccessInterface& db) + { if (!m_target_attr) m_target_attr = db.get_attribute(m_target_attr_name); return m_target_attr; } - bool get_statistics_attributes(CaliperMetadataAccessInterface& db, StatisticsAttributes& a) { + bool get_statistics_attributes(CaliperMetadataAccessInterface& db, StatisticsAttributes& a) + { if (!m_target_attr) return false; if (a.sum) { @@ -1130,12 +1139,11 @@ class VarianceKernel : public AggregateKernel { int prop = CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE; - m_stat_attrs.variance = - db.create_attribute("variance#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop); + m_stat_attrs.variance = db.create_attribute("variance#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop); m_stat_attrs.count = - db.create_attribute("var.count#" + m_target_attr_name, CALI_TYPE_UINT, prop | CALI_ATTR_HIDDEN); + db.create_attribute("var.count#" + m_target_attr_name, CALI_TYPE_UINT, prop | CALI_ATTR_HIDDEN); m_stat_attrs.sum = - db.create_attribute("var.sum#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop | CALI_ATTR_HIDDEN); + db.create_attribute("var.sum#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop | CALI_ATTR_HIDDEN); m_stat_attrs.sqsum = db.create_attribute("var.sqsum#" + m_target_attr_name, CALI_TYPE_DOUBLE, prop | CALI_ATTR_HIDDEN); @@ -1143,30 +1151,22 @@ class VarianceKernel : public AggregateKernel { return true; } - AggregateKernel* make_kernel() { - return new VarianceKernel(this); - } + AggregateKernel* make_kernel() { return new VarianceKernel(this); } - Config(const std::string& name) - : m_target_attr_name(name) - { } + Config(const std::string& name) : m_target_attr_name(name) {} - static AggregateKernelConfig* create(const std::vector& cfg) { - return new Config(cfg.front()); - } + static AggregateKernelConfig* create(const std::vector& cfg) { return new Config(cfg.front()); } }; - VarianceKernel(Config* config) - : m_count(0), m_sum(0.0), m_sqsum(0.0), m_config(config) - { } + VarianceKernel(Config* config) : m_count(0), m_sum(0.0), m_sqsum(0.0), m_config(config) {} const AggregateKernelConfig* config() { return m_config; } - virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_lock); + virtual void aggregate(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_lock); - Attribute target_attr = m_config->get_target_attr(db); + Attribute target_attr = m_config->get_target_attr(db); StatisticsAttributes stat_attr; if (!m_config->get_statistics_attributes(db, stat_attr)) @@ -1175,8 +1175,8 @@ class VarianceKernel : public AggregateKernel { for (const Entry& e : list) { if (e.attribute() == target_attr.id()) { double v = e.value().to_double(); - m_sum += v; - m_sqsum += (v*v); + m_sum += v; + m_sqsum += (v * v); ++m_count; } else if (e.attribute() == stat_attr.sum.id()) { m_sum += e.value().to_double(); @@ -1188,15 +1188,16 @@ class VarianceKernel : public AggregateKernel { } } - virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) { + virtual void append_result(CaliperMetadataAccessInterface& db, EntryList& list) + { if (m_count > 0) { StatisticsAttributes stat_attr; if (!m_config->get_statistics_attributes(db, stat_attr)) return; - double avg = m_sum/m_count; - list.push_back(Entry(stat_attr.variance, Variant(m_sqsum/m_count - (avg*avg)))); + double avg = m_sum / m_count; + list.push_back(Entry(stat_attr.variance, Variant(m_sqsum / m_count - (avg * avg)))); list.push_back(Entry(stat_attr.sum, Variant(m_sum))); list.push_back(Entry(stat_attr.sqsum, Variant(m_sqsum))); list.push_back(Entry(stat_attr.count, Variant(cali_make_variant_from_uint(m_count)))); @@ -1205,13 +1206,13 @@ class VarianceKernel : public AggregateKernel { private: - unsigned m_count; - double m_sum; - double m_sqsum; + unsigned m_count; + double m_sum; + double m_sqsum; std::mutex m_lock; - Config* m_config; + Config* m_config; }; enum KernelID { @@ -1236,29 +1237,29 @@ enum KernelID { #define MAX_KERNEL_ID Variance -const char* kernel_args[] = { "attribute" }; -const char* sratio_args[] = { "numerator", "denominator", "scale" }; -const char* scale_args[] = { "attribute", "scale" }; -const char* scount_args[] = { "scale" }; +const char* kernel_args[] = { "attribute" }; +const char* sratio_args[] = { "numerator", "denominator", "scale" }; +const char* scale_args[] = { "attribute", "scale" }; +const char* scount_args[] = { "scale" }; const QuerySpec::FunctionSignature kernel_signatures[] = { - { KernelID::Count, "count", 0, 0, nullptr }, - { KernelID::Sum, "sum", 1, 1, kernel_args }, - { KernelID::ScaledRatio, "ratio", 2, 3, sratio_args }, - { KernelID::PercentTotal, "percent_total", 1, 1, kernel_args }, - { KernelID::InclusiveSum, "inclusive_sum", 1, 1, kernel_args }, - { KernelID::Min, "min", 1, 1, kernel_args }, - { KernelID::Max, "max", 1, 1, kernel_args }, - { KernelID::Avg, "avg", 1, 1, kernel_args }, - { KernelID::ScaledSum, "scale", 2, 2, scale_args }, - { KernelID::IScaledSum, "inclusive_scale", 2, 2, scale_args }, + { KernelID::Count, "count", 0, 0, nullptr }, + { KernelID::Sum, "sum", 1, 1, kernel_args }, + { KernelID::ScaledRatio, "ratio", 2, 3, sratio_args }, + { KernelID::PercentTotal, "percent_total", 1, 1, kernel_args }, + { KernelID::InclusiveSum, "inclusive_sum", 1, 1, kernel_args }, + { KernelID::Min, "min", 1, 1, kernel_args }, + { KernelID::Max, "max", 1, 1, kernel_args }, + { KernelID::Avg, "avg", 1, 1, kernel_args }, + { KernelID::ScaledSum, "scale", 2, 2, scale_args }, + { KernelID::IScaledSum, "inclusive_scale", 2, 2, scale_args }, { KernelID::IPercentTotal, "inclusive_percent_total", 1, 1, kernel_args }, - { KernelID::Any, "any", 1, 1, kernel_args }, - { KernelID::ScaledCount, "scale_count", 1, 1, scount_args }, - { KernelID::IRatio, "inclusive_ratio", 2, 3, sratio_args }, - { KernelID::IMin, "inclusive_min", 1, 1, kernel_args }, - { KernelID::IMax, "inclusive_max", 1, 1, kernel_args }, - { KernelID::Variance, "variance", 1, 1, kernel_args }, + { KernelID::Any, "any", 1, 1, kernel_args }, + { KernelID::ScaledCount, "scale_count", 1, 1, scount_args }, + { KernelID::IRatio, "inclusive_ratio", 2, 3, sratio_args }, + { KernelID::IMin, "inclusive_min", 1, 1, kernel_args }, + { KernelID::IMax, "inclusive_max", 1, 1, kernel_args }, + { KernelID::Variance, "variance", 1, 1, kernel_args }, QuerySpec::FunctionSignatureTerminator }; @@ -1266,60 +1267,57 @@ const QuerySpec::FunctionSignature kernel_signatures[] = { const struct KernelInfo { const char* name; AggregateKernelConfig* (*create)(const std::vector& cfg); -} kernel_list[] = { - { "count", CountKernel::Config::create }, - { "sum", SumKernel::Config::create }, - { "ratio", ScaledRatioKernel::Config::create }, - { "percent_total", PercentTotalKernel::Config::create }, - { "inclusive_sum", SumKernel::Config::create_inclusive }, - { "min", MinKernel::Config::create }, - { "max", MaxKernel::Config::create }, - { "avg", AvgKernel::Config::create }, - { "scale", ScaledSumKernel::Config::create }, - { "inclusive_scale", ScaledSumKernel::Config::create_inclusive }, - { "inclusive_percent_total", PercentTotalKernel::Config::create_inclusive }, - { "any", AnyKernel::Config::create }, - { "scale_count", ScaledCountKernel::Config::create }, - { "inclusive_ratio", ScaledRatioKernel::Config::create_inclusive }, - { "inclusive_min", MinKernel::Config::create_inclusive }, - { "inclusive_max", MaxKernel::Config::create_inclusive }, - { "variance", VarianceKernel::Config::create }, - - { 0, 0 } -}; - -} // namespace [anonymous] - - -struct Aggregator::AggregatorImpl -{ +} kernel_list[] = { { "count", CountKernel::Config::create }, + { "sum", SumKernel::Config::create }, + { "ratio", ScaledRatioKernel::Config::create }, + { "percent_total", PercentTotalKernel::Config::create }, + { "inclusive_sum", SumKernel::Config::create_inclusive }, + { "min", MinKernel::Config::create }, + { "max", MaxKernel::Config::create }, + { "avg", AvgKernel::Config::create }, + { "scale", ScaledSumKernel::Config::create }, + { "inclusive_scale", ScaledSumKernel::Config::create_inclusive }, + { "inclusive_percent_total", PercentTotalKernel::Config::create_inclusive }, + { "any", AnyKernel::Config::create }, + { "scale_count", ScaledCountKernel::Config::create }, + { "inclusive_ratio", ScaledRatioKernel::Config::create_inclusive }, + { "inclusive_min", MinKernel::Config::create_inclusive }, + { "inclusive_max", MaxKernel::Config::create_inclusive }, + { "variance", VarianceKernel::Config::create }, + + { 0, 0 } }; + +} // namespace + +struct Aggregator::AggregatorImpl { // --- data - vector m_key_strings; - vector m_key_attrs; - std::mutex m_key_lock; + vector m_key_strings; + vector m_key_attrs; + std::mutex m_key_lock; - bool m_select_all; - bool m_select_nested; + bool m_select_all; + bool m_select_nested; vector m_kernel_configs; struct AggregateEntry { - std::vector key; - std::vector< std::unique_ptr > kernels; - std::size_t next_entry_idx; + std::vector key; + std::vector> kernels; + std::size_t next_entry_idx; }; - std::vector< std::shared_ptr > m_entries; + std::vector> m_entries; std::vector m_hashmap; - std::mutex m_entries_lock; + std::mutex m_entries_lock; // // --- parse config // - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { // // --- key config // @@ -1333,13 +1331,12 @@ struct Aggregator::AggregatorImpl switch (spec.groupby.selection) { case QuerySpec::AttributeSelection::Default: case QuerySpec::AttributeSelection::All: - m_select_all = true; + m_select_all = true; break; case QuerySpec::AttributeSelection::List: m_key_strings = spec.groupby.list; break; - default: - ; + default:; } // @@ -1359,9 +1356,8 @@ struct Aggregator::AggregatorImpl if (k.op.id >= 0 && k.op.id <= MAX_KERNEL_ID) { m_kernel_configs.push_back((*::kernel_list[k.op.id].create)(k.args)); } else { - Log(0).stream() << "aggregator: Error: Unknown aggregation kernel " - << k.op.id << " (" << (k.op.name ? k.op.name : "") << ")" - << std::endl; + Log(0).stream() << "aggregator: Error: Unknown aggregation kernel " << k.op.id << " (" + << (k.op.name ? k.op.name : "") << ")" << std::endl; } } break; @@ -1374,9 +1370,9 @@ struct Aggregator::AggregatorImpl // --- snapshot processing // - std::vector update_key_attributes(CaliperMetadataAccessInterface& db) { - std::lock_guard - g(m_key_lock); + std::vector update_key_attributes(CaliperMetadataAccessInterface& db) + { + std::lock_guard g(m_key_lock); auto it = m_key_strings.begin(); @@ -1393,7 +1389,12 @@ struct Aggregator::AggregatorImpl return m_key_attrs; } - inline bool is_key(const CaliperMetadataAccessInterface& db, const std::vector& key_attrs, cali_id_t attr_id) { + inline bool is_key( + const CaliperMetadataAccessInterface& db, + const std::vector& key_attrs, + cali_id_t attr_id + ) + { Attribute attr = db.get_attribute(attr_id); if (m_select_nested && attr.is_nested()) @@ -1409,18 +1410,18 @@ struct Aggregator::AggregatorImpl return false; } - std::shared_ptr - get_aggregation_entry(std::vector::const_iterator nodes_begin, - std::vector::const_iterator nodes_end, - const std::vector& immediates, - CaliperMetadataAccessInterface& db) + std::shared_ptr get_aggregation_entry( + std::vector::const_iterator nodes_begin, + std::vector::const_iterator nodes_end, + const std::vector& immediates, + CaliperMetadataAccessInterface& db + ) { - std::vector key = make_key(nodes_begin, nodes_end, immediates, db); - std::size_t hash = hash_key(key) % m_hashmap.size(); + std::vector key = make_key(nodes_begin, nodes_end, immediates, db); + std::size_t hash = hash_key(key) % m_hashmap.size(); { - std::lock_guard - g(m_entries_lock); + std::lock_guard g(m_entries_lock); for (size_t i = m_hashmap[hash]; i; i = m_entries[i]->next_entry_idx) { auto e = m_entries[i]; @@ -1429,19 +1430,18 @@ struct Aggregator::AggregatorImpl } } - std::vector< std::unique_ptr > kernels; + std::vector> kernels; kernels.reserve(m_kernel_configs.size()); for (AggregateKernelConfig* k_cfg : m_kernel_configs) kernels.emplace_back(k_cfg->make_kernel()); - std::lock_guard - g(m_entries_lock); + std::lock_guard g(m_entries_lock); auto e = std::make_shared(); - e->key = std::move(key); - e->kernels = std::move(kernels); + e->key = std::move(key); + e->kernels = std::move(kernels); e->next_entry_idx = m_hashmap[hash]; size_t idx = m_entries.size(); @@ -1451,13 +1451,14 @@ struct Aggregator::AggregatorImpl return e; } - void process(CaliperMetadataAccessInterface& db, const EntryList& rec) { - std::vector key_attrs = update_key_attributes(db); + void process(CaliperMetadataAccessInterface& db, const EntryList& rec) + { + std::vector key_attrs = update_key_attributes(db); // --- Unravel nodes, filter for key attributes std::vector nodes; - std::vector immediates; + std::vector immediates; nodes.reserve(80); immediates.reserve(key_attrs.size()); @@ -1479,12 +1480,15 @@ struct Aggregator::AggregatorImpl // Keeps nested attributes separate. auto nonnested_begin = std::stable_partition(nodes.begin(), nodes.end(), [&db](const Node* node) { - return db.get_attribute(node->attribute()).is_nested(); } ); + return db.get_attribute(node->attribute()).is_nested(); + }); std::stable_sort(nonnested_begin, nodes.end(), [](const Node* a, const Node* b) { - return a->attribute() < b->attribute(); } ); - std::sort(immediates.begin(), immediates.end(), [](const Entry& a, const Entry& b){ - return a.attribute() < b.attribute(); } ); + return a->attribute() < b->attribute(); + }); + std::sort(immediates.begin(), immediates.end(), [](const Entry& a, const Entry& b) { + return a.attribute() < b.attribute(); + }); auto entry = get_aggregation_entry(nodes.begin(), nodes.end(), immediates, db); @@ -1516,7 +1520,8 @@ struct Aggregator::AggregatorImpl // --- Flush // - void flush(CaliperMetadataAccessInterface& db, const SnapshotProcessFn push) { + void flush(CaliperMetadataAccessInterface& db, const SnapshotProcessFn push) + { // NOTE: No locking: we assume flush() runs serially! for (auto entry : m_entries) { @@ -1525,23 +1530,21 @@ struct Aggregator::AggregatorImpl std::vector rec(entry->key); - for (auto const &k : entry->kernels) + for (auto const& k : entry->kernels) k->append_result(db, rec); push(db, rec); } } - AggregatorImpl() - : m_select_all(false) + AggregatorImpl() : m_select_all(false) { m_entries.reserve(4096); m_hashmap.assign(4096, static_cast(0)); m_entries.push_back(std::shared_ptr(nullptr)); } - AggregatorImpl(const QuerySpec& spec) - : m_select_all(false) + AggregatorImpl(const QuerySpec& spec) : m_select_all(false) { configure(spec); @@ -1550,7 +1553,8 @@ struct Aggregator::AggregatorImpl m_entries.push_back(std::shared_ptr(nullptr)); } - ~AggregatorImpl() { + ~AggregatorImpl() + { for (AggregateKernelConfig* c : m_kernel_configs) delete c; @@ -1558,36 +1562,30 @@ struct Aggregator::AggregatorImpl } }; -Aggregator::Aggregator(const QuerySpec& spec) - : mP { new AggregatorImpl(spec) } -{ -} +Aggregator::Aggregator(const QuerySpec& spec) : mP { new AggregatorImpl(spec) } +{} Aggregator::~Aggregator() { mP.reset(); } -void -Aggregator::flush(CaliperMetadataAccessInterface& db, SnapshotProcessFn push) +void Aggregator::flush(CaliperMetadataAccessInterface& db, SnapshotProcessFn push) { mP->flush(db, push); } -void -Aggregator::add(CaliperMetadataAccessInterface& db, const EntryList& list) +void Aggregator::add(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->process(db, list); } -const QuerySpec::FunctionSignature* -Aggregator::aggregation_defs() +const QuerySpec::FunctionSignature* Aggregator::aggregation_defs() { return ::kernel_signatures; } -std::string -Aggregator::get_aggregation_attribute_name(const QuerySpec::AggregationOp& op) +std::string Aggregator::get_aggregation_attribute_name(const QuerySpec::AggregationOp& op) { switch (op.op.id) { case KernelID::Count: diff --git a/src/reader/CalQLParser.cpp b/src/reader/CalQLParser.cpp index 368a2dc1a..87c15eaee 100644 --- a/src/reader/CalQLParser.cpp +++ b/src/reader/CalQLParser.cpp @@ -18,8 +18,7 @@ using namespace cali; namespace { -int -get_definition_id(const std::string& w, const QuerySpec::FunctionSignature* defs) +int get_definition_id(const std::string& w, const QuerySpec::FunctionSignature* defs) { if (!defs || w.empty()) return -1; @@ -29,7 +28,7 @@ get_definition_id(const std::string& w, const QuerySpec::FunctionSignature* defs int retid = 0; - for ( ; defs[retid].name && wl != defs[retid].name; ++retid) + for (; defs[retid].name && wl != defs[retid].name; ++retid) ; return defs[retid].name ? retid : -1; @@ -40,7 +39,7 @@ std::pair get_keyval(const std::string& str) std::istringstream is(str); std::string first = util::read_word(is, "="); - char c = util::read_char(is); + char c = util::read_char(is); if (c == '=') { std::string val = util::read_word(is); @@ -78,42 +77,26 @@ bool extract_path_keyword(std::vector& list) return ret; } -} // namespace [anonymous] +} // namespace +struct CalQLParser::CalQLParserImpl { + QuerySpec spec; -struct CalQLParser::CalQLParserImpl -{ - QuerySpec spec; - - bool error; - std::string error_msg; + bool error; + std::string error_msg; std::istream::pos_type error_pos; - enum Clause { - None = 0, - Aggregate, - Format, - Group, - Select, - Sort, - Where, - Let - }; - - Clause get_clause_from_word(const std::string& w) { + enum Clause { None = 0, Aggregate, Format, Group, Select, Sort, Where, Let }; + + Clause get_clause_from_word(const std::string& w) + { const struct keyword_map_t { - const char* name; Clause clause; - } keywords[] = { - { "aggregate", Aggregate }, - { "format", Format }, - { "group", Group }, - { "select", Select }, - { "order", Sort }, - { "where", Where }, - { "let", Let }, - - { nullptr, None } - }; + const char* name; + Clause clause; + } keywords[] = { { "aggregate", Aggregate }, { "format", Format }, { "group", Group }, { "select", Select }, + { "order", Sort }, { "where", Where }, { "let", Let }, + + { nullptr, None } }; std::string wl(w); std::transform(w.begin(), w.end(), wl.begin(), ::tolower); @@ -125,19 +108,18 @@ struct CalQLParser::CalQLParserImpl return Clause::None; } - void - set_error(const std::string& s, std::istream& is) { + void set_error(const std::string& s, std::istream& is) + { error = true; error_pos = is.tellg(); error_msg = s; } /// \brief Parse "(arg1, arg2, ...)" argument list, ignoring whitespace - std::vector - parse_arglist(std::istream& is) + std::vector parse_arglist(std::istream& is) { std::vector ret; - std::string word; + std::string word; char c = util::read_char(is); @@ -151,7 +133,7 @@ struct CalQLParser::CalQLParserImpl do { std::string str = util::read_word(is, ",;()\n"); - c = util::read_char(is); + c = util::read_char(is); if (!str.empty() && (c == ',' || c == ')')) ret.push_back(str); @@ -166,11 +148,14 @@ struct CalQLParser::CalQLParserImpl return ret; } - std::map - make_kwargs(const QuerySpec::FunctionSignature& sig, const std::vector& args, std::istream& is) + std::map make_kwargs( + const QuerySpec::FunctionSignature& sig, + const std::vector& args, + std::istream& is + ) { std::map ret; - int n = std::min(static_cast(args.size()), sig.max_args); + int n = std::min(static_cast(args.size()), sig.max_args); for (int i = 0; i < n; ++i) { auto p = get_keyval(args[i]); @@ -193,18 +178,18 @@ struct CalQLParser::CalQLParserImpl return ret; } - void - parse_aggregate(std::istream& is) { + void parse_aggregate(std::istream& is) + { const QuerySpec::FunctionSignature* defs = Aggregator::aggregation_defs(); - char c = '\0'; + char c = '\0'; do { - std::string w = util::read_word(is, ",;=<>()\n"); + std::string w = util::read_word(is, ",;=<>()\n"); std::vector args = parse_arglist(is); // check if this is an aggregation function - int i = get_definition_id(w, defs); + int i = get_definition_id(w, defs); int argsize = static_cast(args.size()); if (i >= 0) { @@ -225,8 +210,8 @@ struct CalQLParser::CalQLParserImpl is.unget(); } - void - parse_format(std::istream& is) { + void parse_format(std::istream& is) + { const QuerySpec::FunctionSignature* defs = FormatProcessor::formatter_defs(); std::string fname = util::read_word(is, ",;=<>()\n"); @@ -234,16 +219,16 @@ struct CalQLParser::CalQLParserImpl int i = 0; - for ( ; defs[i].name && fname != defs[i].name; ++i) + for (; defs[i].name && fname != defs[i].name; ++i) ; if (!defs[i].name) { - set_error(std::string("Unknown formatter ") + fname, is); + set_error(std::string("Unknown formatter ") + fname, is); return; } - std::vector args = parse_arglist(is); - int argsize = static_cast(args.size()); + std::vector args = parse_arglist(is); + int argsize = static_cast(args.size()); if (defs[i].min_args > argsize || defs[i].max_args < argsize) set_error(std::string("Invalid number of arguments for formatter ") + fname, is); @@ -254,8 +239,8 @@ struct CalQLParser::CalQLParserImpl } } - void - parse_groupby(std::istream& is) { + void parse_groupby(std::istream& is) + { char c = 0; do { @@ -275,17 +260,17 @@ struct CalQLParser::CalQLParserImpl spec.groupby.use_path = extract_path_keyword(spec.groupby.list); } - void - parse_select(std::istream& is) { + void parse_select(std::istream& is) + { // SELECT selection, selection, ... // selection : label [AS alias] // | aggregation_function(parameters) [AS alias] [UNIT unit] // | * const QuerySpec::FunctionSignature* defs = Aggregator::aggregation_defs(); - char c = '\0'; + char c = '\0'; - std::string next_keyword; + std::string next_keyword; std::vector selection_list; do { @@ -309,8 +294,8 @@ struct CalQLParser::CalQLParserImpl int i = get_definition_id(w, defs); if (i >= 0) { - std::vector args = parse_arglist(is); - int argsize = static_cast(args.size()); + std::vector args = parse_arglist(is); + int argsize = static_cast(args.size()); if (defs[i].min_args > argsize || defs[i].max_args < argsize) { set_error(std::string("Invalid number of arguments for ") + defs[i].name, is); @@ -329,7 +314,7 @@ struct CalQLParser::CalQLParserImpl } } } else { - set_error(std::string("Unknown aggregation function ")+w, is); + set_error(std::string("Unknown aggregation function ") + w, is); } } else { // not an aggregation function: add to selection list @@ -393,10 +378,10 @@ struct CalQLParser::CalQLParserImpl parse_clause_from_word(next_keyword, is); } - void - parse_sort(std::istream& is) { + void parse_sort(std::istream& is) + { std::string next_keyword; - char c = 0; + char c = 0; do { c = 0; @@ -412,7 +397,7 @@ struct CalQLParser::CalQLParserImpl std::string tmp = util::read_word(is, ",;=<>()\n"); std::transform(tmp.begin(), tmp.end(), std::back_inserter(next_keyword), ::tolower); - if (next_keyword == "asc" ) { + if (next_keyword == "asc") { spec.sort.selection = QuerySpec::SortSelection::List; spec.sort.list.push_back(QuerySpec::SortSpec(arg, QuerySpec::SortSpec::Ascending)); next_keyword.clear(); @@ -437,8 +422,8 @@ struct CalQLParser::CalQLParserImpl parse_clause_from_word(next_keyword, is); } - QuerySpec::Condition - parse_filter_clause(std::istream& is) { + QuerySpec::Condition parse_filter_clause(std::istream& is) + { std::string w = util::read_word(is, ",;=<>()\n"); std::string wl(w); @@ -448,7 +433,7 @@ struct CalQLParser::CalQLParserImpl if (wl == "not") { negate = true; - w = util::read_word(is, ",;=<>()\n"); + w = util::read_word(is, ",;=<>()\n"); } QuerySpec::Condition cond; @@ -491,7 +476,7 @@ struct CalQLParser::CalQLParserImpl if (w.empty()) set_error("Argument expected for '>'", is); else { - cond.op = (negate ? QuerySpec::Condition::LessOrEqual : QuerySpec::Condition::GreaterThan); + cond.op = (negate ? QuerySpec::Condition::LessOrEqual : QuerySpec::Condition::GreaterThan); cond.value = w; } @@ -508,8 +493,8 @@ struct CalQLParser::CalQLParserImpl return cond; } - void - parse_where(std::istream& is) { + void parse_where(std::istream& is) + { char c = '\0'; do { @@ -527,14 +512,14 @@ struct CalQLParser::CalQLParserImpl is.unget(); } - void - parse_let(std::istream& is) { + void parse_let(std::istream& is) + { std::string next_keyword; - char c = 0; + char c = 0; do { const QuerySpec::FunctionSignature* defs = Preprocessor::preprocess_defs(); - QuerySpec::PreprocessSpec pspec; + QuerySpec::PreprocessSpec pspec; std::string target = util::read_word(is, ",;=<>()\n"); @@ -550,28 +535,29 @@ struct CalQLParser::CalQLParserImpl int i = 0; - for ( ; defs[i].name && opname != defs[i].name; ++i) + for (; defs[i].name && opname != defs[i].name; ++i) ; if (!defs[i].name) { - set_error(std::string("Unknown operator ") + opname, is); + set_error(std::string("Unknown operator ") + opname, is); return; } - std::vector args = parse_arglist(is); - int argsize = static_cast(args.size()); + std::vector args = parse_arglist(is); + int argsize = static_cast(args.size()); if (defs[i].min_args > argsize || defs[i].max_args < argsize) set_error(std::string("Invalid number of arguments for operator ") + opname, is); else { - auto it = std::find_if(spec.preprocess_ops.begin(), spec.preprocess_ops.end(), - [&target](QuerySpec::PreprocessSpec& s){ - return s.target == target; - }); + auto it = std::find_if( + spec.preprocess_ops.begin(), + spec.preprocess_ops.end(), + [&target](QuerySpec::PreprocessSpec& s) { return s.target == target; } + ); if (it == spec.preprocess_ops.end()) { pspec.target = target; - pspec.op = QuerySpec::AggregationOp(defs[i], args); + pspec.op = QuerySpec::AggregationOp(defs[i], args); } else { set_error(target + " defined twice", is); return; @@ -607,8 +593,8 @@ struct CalQLParser::CalQLParserImpl parse_clause_from_word(next_keyword, is); } - void - parse_clause(Clause clause, std::istream& is) { + void parse_clause(Clause clause, std::istream& is) + { switch (clause) { case Aggregate: parse_aggregate(is); @@ -639,12 +625,12 @@ struct CalQLParser::CalQLParserImpl } } - void - parse_clause_from_word(std::string w, std::istream& is) { + void parse_clause_from_word(std::string w, std::istream& is) + { Clause clause = get_clause_from_word(w); if (clause == None) { - set_error(std::string("Expected clause keyword, got ")+w, is); + set_error(std::string("Expected clause keyword, got ") + w, is); } else { // special handling for "group" and "sort" if (clause == Group || clause == Sort) { @@ -661,8 +647,8 @@ struct CalQLParser::CalQLParserImpl } } - void - parse(std::istream& is) { + void parse(std::istream& is) + { while (!error && is.good()) { std::string w = util::read_word(is, ",;=<>()\n"); @@ -677,12 +663,11 @@ struct CalQLParser::CalQLParserImpl char c = util::read_char(is); if (is.good() && c != '\0' && c != '\n') - set_error(std::string("Expected clause, got ")+c, is); + set_error(std::string("Expected clause, got ") + c, is); } } - CalQLParserImpl() - : error(false), error_pos(std::istream::pos_type(-1)) + CalQLParserImpl() : error(false), error_pos(std::istream::pos_type(-1)) { spec.aggregate.selection = QuerySpec::AggregationSelection::None; spec.groupby.selection = QuerySpec::AttributeSelection::Default; @@ -693,14 +678,12 @@ struct CalQLParser::CalQLParserImpl } }; -CalQLParser::CalQLParser(std::istream& is) - : mP(new CalQLParserImpl) +CalQLParser::CalQLParser(std::istream& is) : mP(new CalQLParserImpl) { mP->parse(is); } -CalQLParser::CalQLParser(const char* str) - : mP(new CalQLParserImpl) +CalQLParser::CalQLParser(const char* str) : mP(new CalQLParserImpl) { std::istringstream is(str); mP->parse(is); @@ -711,26 +694,22 @@ CalQLParser::~CalQLParser() mP.reset(); } -bool -CalQLParser::error() const +bool CalQLParser::error() const { return mP->error; } -std::string -CalQLParser::error_msg() const +std::string CalQLParser::error_msg() const { return mP->error_msg; } -std::istream::pos_type -CalQLParser::error_pos() +std::istream::pos_type CalQLParser::error_pos() { return mP->error_pos; } -QuerySpec -CalQLParser::spec() const +QuerySpec CalQLParser::spec() const { return mP->spec; } diff --git a/src/reader/CaliReader.cpp b/src/reader/CaliReader.cpp index 9bd02b120..340bc361f 100644 --- a/src/reader/CaliReader.cpp +++ b/src/reader/CaliReader.cpp @@ -20,21 +20,23 @@ using namespace std; namespace { -class fast_istringstream { +class fast_istringstream +{ std::string::iterator it_; std::string::iterator end_; public: - fast_istringstream(std::string::iterator b, std::string::iterator e) - : it_ { b }, end_ { e } - { } + fast_istringstream(std::string::iterator b, std::string::iterator e) : it_ { b }, end_ { e } {} inline bool good() const { return it_ != end_; } - inline char get() { return *it_++; } + + inline char get() { return *it_++; } + inline void unget() { --it_; } - inline bool matches(char c) { + inline bool matches(char c) + { if (it_ != end_) { if (c == *it_) { ++it_; @@ -44,9 +46,10 @@ class fast_istringstream { return false; } - inline bool matches(size_t N, const char* key) { - if (it_+N < end_) { - if (std::equal(it_, it_+N, key)) { + inline bool matches(size_t N, const char* key) + { + if (it_ + N < end_) { + if (std::equal(it_, it_ + N, key)) { it_ += N; return true; } @@ -57,8 +60,7 @@ class fast_istringstream { std::string context() { return std::string { it_, end_ }; } }; -inline uint64_t -read_uint64_element(fast_istringstream& is) +inline uint64_t read_uint64_element(fast_istringstream& is) { uint64_t ret = 0; @@ -76,8 +78,7 @@ read_uint64_element(fast_istringstream& is) return ret; } -inline std::string -read_escaped_word(fast_istringstream& is) +inline std::string read_escaped_word(fast_istringstream& is) { std::string w; w.reserve(32); @@ -104,8 +105,7 @@ read_escaped_word(fast_istringstream& is) return w; } -inline std::vector -read_id_list(fast_istringstream& is) +inline std::vector read_id_list(fast_istringstream& is) { std::vector ret; ret.reserve(8); @@ -117,8 +117,7 @@ read_id_list(fast_istringstream& is) return ret; } -inline std::vector -read_string_list(fast_istringstream& is) +inline std::vector read_string_list(fast_istringstream& is) { std::vector ret; ret.reserve(8); @@ -130,28 +129,26 @@ read_string_list(fast_istringstream& is) return ret; } -} // namespace [anonymous] +} // namespace -struct CaliReader::CaliReaderImpl -{ - bool m_error; - std::string m_error_msg; +struct CaliReader::CaliReaderImpl { + bool m_error; + std::string m_error_msg; unsigned int m_num_read; - CaliReaderImpl() - : m_error { false } - { } + CaliReaderImpl() : m_error { false } {} - void set_error(const std::string& msg) { - m_error = true; + void set_error(const std::string& msg) + { + m_error = true; m_error_msg = msg; } void read_node(fast_istringstream& is, CaliperMetadataDB& db, IdMap& idmap, NodeProcessFn node_proc) { - cali_id_t attr_id = CALI_INV_ID; - cali_id_t node_id = CALI_INV_ID; - cali_id_t prnt_id = CALI_INV_ID; + cali_id_t attr_id = CALI_INV_ID; + cali_id_t node_id = CALI_INV_ID; + cali_id_t prnt_id = CALI_INV_ID; std::string data_str; do { @@ -237,7 +234,13 @@ struct CaliReader::CaliReaderImpl db.merge_global(attr[i], data[i], idmap); } - void read_record(fast_istringstream& is, CaliperMetadataDB& db, IdMap& idmap, NodeProcessFn node_proc, SnapshotProcessFn snap_proc) + void read_record( + fast_istringstream& is, + CaliperMetadataDB& db, + IdMap& idmap, + NodeProcessFn node_proc, + SnapshotProcessFn snap_proc + ) { if (is.matches(11, "__rec=node,")) { read_node(is, db, idmap, node_proc); @@ -250,10 +253,11 @@ struct CaliReader::CaliReaderImpl } } - void read(std::istream& is, CaliperMetadataDB& db, NodeProcessFn node_proc, SnapshotProcessFn snap_proc) { + void read(std::istream& is, CaliperMetadataDB& db, NodeProcessFn node_proc, SnapshotProcessFn snap_proc) + { IdMap idmap; - for (std::string line; std::getline(is, line); ) { + for (std::string line; std::getline(is, line);) { if (line.empty()) continue; fast_istringstream isstream { line.begin(), line.end() }; @@ -262,33 +266,33 @@ struct CaliReader::CaliReaderImpl } }; -CaliReader::CaliReader() - : mP { new CaliReaderImpl() } -{ } +CaliReader::CaliReader() : mP { new CaliReaderImpl() } +{} CaliReader::~CaliReader() -{ } +{} -bool -CaliReader::error() const +bool CaliReader::error() const { return mP->m_error; } -std::string -CaliReader::error_msg() const +std::string CaliReader::error_msg() const { return mP->m_error_msg; } -void -CaliReader::read(std::istream& is, CaliperMetadataDB& db, NodeProcessFn node_proc, SnapshotProcessFn snap_proc) +void CaliReader::read(std::istream& is, CaliperMetadataDB& db, NodeProcessFn node_proc, SnapshotProcessFn snap_proc) { mP->read(is, db, node_proc, snap_proc); } -void -CaliReader::read(const std::string& filename, CaliperMetadataDB& db, NodeProcessFn node_proc, SnapshotProcessFn snap_proc) +void CaliReader::read( + const std::string& filename, + CaliperMetadataDB& db, + NodeProcessFn node_proc, + SnapshotProcessFn snap_proc +) { if (filename.empty()) mP->read(std::cin, db, node_proc, snap_proc); @@ -296,7 +300,7 @@ CaliReader::read(const std::string& filename, CaliperMetadataDB& db, NodeProcess std::ifstream is(filename.c_str()); if (!is) { - mP->m_error = true; + mP->m_error = true; mP->m_error_msg = std::string("Cannot open file ") + filename; return; } diff --git a/src/reader/CaliWriter.cpp b/src/reader/CaliWriter.cpp index d31a3c1f0..6c3335d91 100644 --- a/src/reader/CaliWriter.cpp +++ b/src/reader/CaliWriter.cpp @@ -19,9 +19,7 @@ using namespace cali; namespace { -enum RecordKind { - Snapshot, Globals -}; +enum RecordKind { Snapshot, Globals }; void write_node_content(std::ostream& os, const cali::Node* node) { @@ -36,7 +34,12 @@ void write_node_content(std::ostream& os, const cali::Node* node) os.put('\n'); } -void write_record_content(std::ostream& os, RecordKind kind, const std::vector& ref_entries, const std::vector& imm_entries) +void write_record_content( + std::ostream& os, + RecordKind kind, + const std::vector& ref_entries, + const std::vector& imm_entries +) { if (kind == RecordKind::Snapshot) os.write("__rec=ctx", 9); @@ -64,23 +67,18 @@ void write_record_content(std::ostream& os, RecordKind kind, const std::vector m_written_nodes; - std::mutex m_written_nodes_lock; + std::mutex m_written_nodes_lock; - std::size_t m_num_written; + std::size_t m_num_written; - - CaliWriterImpl(OutputStream& os) - : m_os(os), - m_num_written(0) - { } + CaliWriterImpl(OutputStream& os) : m_os(os), m_num_written(0) {} void recursive_write_node(const CaliperMetadataAccessInterface& db, cali_id_t id) { @@ -88,8 +86,7 @@ struct CaliWriter::CaliWriterImpl return; { - std::lock_guard - g(m_written_nodes_lock); + std::lock_guard g(m_written_nodes_lock); if (m_written_nodes.count(id) > 0) return; @@ -108,8 +105,7 @@ struct CaliWriter::CaliWriterImpl recursive_write_node(db, parent->id()); { - std::lock_guard - g(m_os_lock); + std::lock_guard g(m_os_lock); std::ostream* real_os = m_os.stream(); @@ -118,16 +114,13 @@ struct CaliWriter::CaliWriterImpl } { - std::lock_guard - g(m_written_nodes_lock); + std::lock_guard g(m_written_nodes_lock); m_written_nodes.insert(id); } } - void write_entrylist(const CaliperMetadataAccessInterface& db, - RecordKind kind, - const std::vector& rec) + void write_entrylist(const CaliperMetadataAccessInterface& db, RecordKind kind, const std::vector& rec) { // write node entries @@ -149,8 +142,7 @@ struct CaliWriter::CaliWriterImpl // write the record { - std::lock_guard - g(m_os_lock); + std::lock_guard g(m_os_lock); std::ostream* real_os = m_os.stream(); @@ -160,10 +152,8 @@ struct CaliWriter::CaliWriterImpl } }; - -CaliWriter::CaliWriter(OutputStream& os) - : mP(new CaliWriterImpl(os)) -{ } +CaliWriter::CaliWriter(OutputStream& os) : mP(new CaliWriterImpl(os)) +{} CaliWriter::~CaliWriter() { diff --git a/src/reader/CaliperMetadataDB.cpp b/src/reader/CaliperMetadataDB.cpp index 091cd0d4e..0269c8fac 100644 --- a/src/reader/CaliperMetadataDB.cpp +++ b/src/reader/CaliperMetadataDB.cpp @@ -22,47 +22,46 @@ using namespace std; namespace { -inline cali_id_t -map_id(cali_id_t id, const IdMap& idmap) { +inline cali_id_t map_id(cali_id_t id, const IdMap& idmap) +{ auto it = idmap.find(id); return it == idmap.end() ? id : it->second; } } // namespace -struct CaliperMetadataDB::CaliperMetadataDBImpl -{ - Node m_root; ///< (Artificial) root node - vector m_nodes; ///< Node list - mutable mutex m_node_lock; +struct CaliperMetadataDB::CaliperMetadataDBImpl { + Node m_root; ///< (Artificial) root node + vector m_nodes; ///< Node list + mutable mutex m_node_lock; - Node* m_type_nodes[CALI_MAXTYPE+1] = { 0 }; + Node* m_type_nodes[CALI_MAXTYPE + 1] = { 0 }; - map m_attributes; - mutable mutex m_attribute_lock; + map m_attributes; + mutable mutex m_attribute_lock; - vector m_string_db; - mutable mutex m_string_db_lock; + vector m_string_db; + mutable mutex m_string_db_lock; - vector m_globals; - mutex m_globals_lock; + vector m_globals; + mutex m_globals_lock; std::map m_attr_aliases; std::map m_attr_units; - Attribute m_alias_attr; - Attribute m_unit_attr; + Attribute m_alias_attr; + Attribute m_unit_attr; - inline Node* node(cali_id_t id) const { - std::lock_guard - g(m_node_lock); + inline Node* node(cali_id_t id) const + { + std::lock_guard g(m_node_lock); return id < m_nodes.size() ? m_nodes[id] : nullptr; } - inline Attribute attribute(cali_id_t id) const { - std::lock_guard - g(m_node_lock); + inline Attribute attribute(cali_id_t id) const + { + std::lock_guard g(m_node_lock); if (id >= m_nodes.size()) return Attribute(); @@ -70,7 +69,8 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return Attribute::make_attribute(m_nodes[id]); } - void setup_bootstrap_nodes() { + void setup_bootstrap_nodes() + { // Create initial nodes static const struct NodeInfo { @@ -78,21 +78,19 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl cali_id_t attr_id; Variant data; cali_id_t parent; - } bootstrap_nodes[] = { - { 0, 9, { CALI_TYPE_USR }, CALI_INV_ID }, - { 1, 9, { CALI_TYPE_INT }, CALI_INV_ID }, - { 2, 9, { CALI_TYPE_UINT }, CALI_INV_ID }, - { 3, 9, { CALI_TYPE_STRING }, CALI_INV_ID }, - { 4, 9, { CALI_TYPE_ADDR }, CALI_INV_ID }, - { 5, 9, { CALI_TYPE_DOUBLE }, CALI_INV_ID }, - { 6, 9, { CALI_TYPE_BOOL }, CALI_INV_ID }, - { 7, 9, { CALI_TYPE_TYPE }, CALI_INV_ID }, - { 8, 8, { CALI_TYPE_STRING, "cali.attribute.name", 19 }, 3 }, - { 9, 8, { CALI_TYPE_STRING, "cali.attribute.type", 19 }, 7 }, - { 10, 8, { CALI_TYPE_STRING, "cali.attribute.prop", 19 }, 1 }, - { 11, 9, { CALI_TYPE_PTR }, CALI_INV_ID }, - { CALI_INV_ID, CALI_INV_ID, { }, CALI_INV_ID } - }; + } bootstrap_nodes[] = { { 0, 9, { CALI_TYPE_USR }, CALI_INV_ID }, + { 1, 9, { CALI_TYPE_INT }, CALI_INV_ID }, + { 2, 9, { CALI_TYPE_UINT }, CALI_INV_ID }, + { 3, 9, { CALI_TYPE_STRING }, CALI_INV_ID }, + { 4, 9, { CALI_TYPE_ADDR }, CALI_INV_ID }, + { 5, 9, { CALI_TYPE_DOUBLE }, CALI_INV_ID }, + { 6, 9, { CALI_TYPE_BOOL }, CALI_INV_ID }, + { 7, 9, { CALI_TYPE_TYPE }, CALI_INV_ID }, + { 8, 8, { CALI_TYPE_STRING, "cali.attribute.name", 19 }, 3 }, + { 9, 8, { CALI_TYPE_STRING, "cali.attribute.type", 19 }, 7 }, + { 10, 8, { CALI_TYPE_STRING, "cali.attribute.prop", 19 }, 1 }, + { 11, 9, { CALI_TYPE_PTR }, CALI_INV_ID }, + { CALI_INV_ID, CALI_INV_ID, {}, CALI_INV_ID } }; // Create nodes @@ -115,7 +113,8 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl } } - Node* create_node(cali_id_t attr_id, const Variant& data, Node* parent) { + Node* create_node(cali_id_t attr_id, const Variant& data, Node* parent) + { // NOTE: We assume that m_node_lock is locked! Node* node = new Node(m_nodes.size(), attr_id, data); @@ -129,17 +128,16 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl } /// \brief Make string variant from string database - Variant make_string_variant(const char* str, size_t len) { - if (len > 0 && str[len-1] == '\0') + Variant make_string_variant(const char* str, size_t len) + { + if (len > 0 && str[len - 1] == '\0') --len; - std::lock_guard - g(m_string_db_lock); + std::lock_guard g(m_string_db_lock); - auto it = std::lower_bound(m_string_db.begin(), m_string_db.end(), str, - [len](const char* a, const char* b) { - return strncmp(a, b, len) < 0; - }); + auto it = std::lower_bound(m_string_db.begin(), m_string_db.end(), str, [len](const char* a, const char* b) { + return strncmp(a, b, len) < 0; + }); if (it != m_string_db.end() && strncmp(str, *it, len) == 0 && strlen(*it) == len) return Variant(CALI_TYPE_STRING, *it, len); @@ -153,7 +151,8 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return Variant(CALI_TYPE_STRING, ptr, len); } - Variant make_variant(cali_attr_type type, const std::string& str) { + Variant make_variant(cali_attr_type type, const std::string& str) + { Variant ret; switch (type) { @@ -175,33 +174,27 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl /// Merge node given by un-mapped node info from stream with given \a idmap into DB /// If \a v_data is a string, it must already be in the string database! - Node* merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const Variant& v_data) { + Node* merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const Variant& v_data) + { if (!attribute(attr_id)) attr_id = CALI_INV_ID; if (node_id == CALI_INV_ID || attr_id == CALI_INV_ID || v_data.empty()) { Log(0).stream() << "CaliperMetadataDB::merge_node(): Invalid node info: " - << "id=" << node_id - << ", attr=" << attr_id - << ", parent=" << prnt_id - << ", value=" << v_data - << std::endl; + << "id=" << node_id << ", attr=" << attr_id << ", parent=" << prnt_id + << ", value=" << v_data << std::endl; return nullptr; } Node* parent = &m_root; if (prnt_id != CALI_INV_ID) { - std::lock_guard - g(m_node_lock); + std::lock_guard g(m_node_lock); if (prnt_id >= m_nodes.size()) { Log(0).stream() << "CaliperMetadataDB::merge_node(): Invalid parent node " << prnt_id << " for " - << "id=" << node_id - << ", attr=" << attr_id - << ", parent=" << prnt_id - << ", value=" << v_data - << std::endl; + << "id=" << node_id << ", attr=" << attr_id << ", parent=" << prnt_id + << ", value=" << v_data << std::endl; return nullptr; } @@ -212,10 +205,9 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl bool new_node = false; { - std::lock_guard - g(m_node_lock); + std::lock_guard g(m_node_lock); - for ( node = parent->first_child(); node && !node->equals(attr_id, v_data); node = node->next_sibling() ) + for (node = parent->first_child(); node && !node->equals(attr_id, v_data); node = node->next_sibling()) ; if (!node) { @@ -225,8 +217,7 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl } if (new_node && node->attribute() == Attribute::NAME_ATTR_ID) { - std::lock_guard - g(m_attribute_lock); + std::lock_guard g(m_attribute_lock); m_attributes.insert(make_pair(string(node->data().to_string()), node)); } @@ -236,7 +227,8 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl /// Merge node given by un-mapped node info from stream with given \a idmap into DB /// If \a v_data is a string, it must already be in the string database! - Node* merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const Variant& v_data, IdMap& idmap) { + Node* merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const Variant& v_data, IdMap& idmap) + { attr_id = ::map_id(attr_id, idmap); prnt_id = ::map_id(prnt_id, idmap); @@ -248,9 +240,14 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return node; } - EntryList merge_snapshot(size_t n_nodes, const cali_id_t node_ids[], - size_t n_imm, const cali_id_t attr_ids[], const Variant values[], - const IdMap& idmap) const + EntryList merge_snapshot( + size_t n_nodes, + const cali_id_t node_ids[], + size_t n_imm, + const cali_id_t attr_ids[], + const Variant values[], + const IdMap& idmap + ) const { EntryList list; list.reserve(n_nodes + n_imm); @@ -263,16 +260,15 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return list; } - Node* recursive_merge_node(Node* node, const CaliperMetadataAccessInterface& db) { + Node* recursive_merge_node(Node* node, const CaliperMetadataAccessInterface& db) + { if (!node || node->id() == CALI_INV_ID) return nullptr; if (node->id() < 12) return m_nodes[node->id()]; - Node* attr_node = - recursive_merge_node(db.node(node->attribute()), db); - Node* parent = - recursive_merge_node(node->parent(), db); + Node* attr_node = recursive_merge_node(db.node(node->attribute()), db); + Node* parent = recursive_merge_node(node->parent(), db); Variant v_data = node->data(); @@ -282,8 +278,7 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return merge_node(node->id(), attr_node->id(), parent ? parent->id() : CALI_INV_ID, v_data); } - EntryList merge_snapshot(const CaliperMetadataAccessInterface& db, - const EntryList& rec) + EntryList merge_snapshot(const CaliperMetadataAccessInterface& db, const EntryList& rec) { EntryList list; list.reserve(rec.size()); @@ -300,12 +295,12 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return list; } - void merge_global(cali_id_t node_id, const IdMap& idmap) { + void merge_global(cali_id_t node_id, const IdMap& idmap) + { Node* glbl_node = node(::map_id(node_id, idmap)); if (glbl_node) { - std::lock_guard - g(m_globals_lock); + std::lock_guard g(m_globals_lock); Entry e = Entry(glbl_node); @@ -314,17 +309,17 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl } } - Attribute attribute(const std::string& name) const { - std::lock_guard - g(m_attribute_lock); + Attribute attribute(const std::string& name) const + { + std::lock_guard g(m_attribute_lock); auto it = m_attributes.find(name); return it == m_attributes.end() ? Attribute() : Attribute::make_attribute(it->second); } - std::vector get_attributes() const { - std::lock_guard - g(m_attribute_lock); + std::vector get_attributes() const + { + std::lock_guard g(m_attribute_lock); std::vector ret; ret.reserve(m_attributes.size()); @@ -335,7 +330,8 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return ret; } - Node* make_tree_entry(std::size_t n, const Attribute attr[], const Variant data[], Node* parent = 0) { + Node* make_tree_entry(std::size_t n, const Attribute attr[], const Variant data[], Node* parent = 0) + { Node* node = nullptr; if (!parent) @@ -350,8 +346,7 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl if (v_data.type() == CALI_TYPE_STRING) v_data = make_string_variant(static_cast(data[i].data()), data[i].size()); - std::lock_guard - g(m_node_lock); + std::lock_guard g(m_node_lock); for (node = parent->first_child(); node && !node->equals(attr[i].id(), v_data); node = node->next_sibling()) ; @@ -365,17 +360,18 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return node; } - Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent = 0) { + Node* make_tree_entry(std::size_t n, const Node* nodelist[], Node* parent = 0) + { Node* node = nullptr; if (!parent) parent = &m_root; for (size_t i = 0; i < n; ++i) { - std::lock_guard - g(m_node_lock); + std::lock_guard g(m_node_lock); - for (node = parent->first_child(); node && !node->equals(nodelist[i]->attribute(), nodelist[i]->data()); node = node->next_sibling()) + for (node = parent->first_child(); node && !node->equals(nodelist[i]->attribute(), nodelist[i]->data()); + node = node->next_sibling()) ; if (!node) @@ -387,10 +383,16 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return node; } - Attribute create_attribute(const std::string& name, cali_attr_type type, int prop, - int meta, const Attribute* meta_attr, const Variant* meta_data) { - std::lock_guard - g(m_attribute_lock); + Attribute create_attribute( + const std::string& name, + cali_attr_type type, + int prop, + int meta, + const Attribute* meta_attr, + const Variant* meta_data + ) + { + std::lock_guard g(m_attribute_lock); // --- Check if attribute exists @@ -417,10 +419,8 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl parent = make_tree_entry(1, &m_alias_attr, &v_alias, parent); } - Attribute n_attr[2] = { attribute(Attribute::PROP_ATTR_ID), - attribute(Attribute::NAME_ATTR_ID) }; - Variant n_data[2] = { Variant(prop), - make_variant(CALI_TYPE_STRING, name) }; + Attribute n_attr[2] = { attribute(Attribute::PROP_ATTR_ID), attribute(Attribute::NAME_ATTR_ID) }; + Variant n_data[2] = { Variant(prop), make_variant(CALI_TYPE_STRING, name) }; Node* node = make_tree_entry(2, n_attr, n_data, parent); @@ -429,25 +429,24 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return Attribute::make_attribute(node); } - std::vector get_globals() { - std::lock_guard - g(m_globals_lock); + std::vector get_globals() + { + std::lock_guard g(m_globals_lock); return m_globals; } - void set_global(const Attribute& attr, const Variant& value) { + void set_global(const Attribute& attr, const Variant& value) + { if (!attr.is_global()) return; - std::lock_guard - g(m_globals_lock); + std::lock_guard g(m_globals_lock); if (attr.store_as_value()) { - auto it = std::find_if(m_globals.begin(), m_globals.end(), - [attr](const Entry& e) { - return e.attribute() == attr.id(); - } ); + auto it = std::find_if(m_globals.begin(), m_globals.end(), [attr](const Entry& e) { + return e.attribute() == attr.id(); + }); if (it == m_globals.end()) m_globals.push_back(Entry(attr, value)); @@ -455,19 +454,20 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl *it = Entry(attr, value); } else { // check if we already have this value - if (std::find_if(m_globals.begin(), m_globals.end(), - [attr,value](const Entry& e) { - for (const Node* node = e.node(); node; node = node->parent()) - if (node->equals(attr.id(), value)) - return true; - return false; - } ) != m_globals.end()) + if (std::find_if( + m_globals.begin(), + m_globals.end(), + [attr, value](const Entry& e) { + for (const Node* node = e.node(); node; node = node->parent()) + if (node->equals(attr.id(), value)) + return true; + return false; + } + ) + != m_globals.end()) return; - auto it = std::find_if(m_globals.begin(), m_globals.end(), - [](const Entry& e) { - return e.is_reference(); - } ); + auto it = std::find_if(m_globals.begin(), m_globals.end(), [](const Entry& e) { return e.is_reference(); }); if (it == m_globals.end() || !attr.is_autocombineable()) m_globals.push_back(Entry(make_tree_entry(1, &attr, &value))); @@ -476,9 +476,9 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl } } - std::vector import_globals(CaliperMetadataAccessInterface& db, const std::vector& import_globals) { - std::lock_guard - g(m_globals_lock); + std::vector import_globals(CaliperMetadataAccessInterface& db, const std::vector& import_globals) + { + std::lock_guard g(m_globals_lock); m_globals.clear(); @@ -494,20 +494,17 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl return m_globals; } - CaliperMetadataDBImpl() - : m_root { CALI_INV_ID, CALI_INV_ID, { } } - { - setup_bootstrap_nodes(); - - m_alias_attr = - create_attribute("attribute.alias", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS, - 0, nullptr, nullptr); - m_unit_attr = - create_attribute("attribute.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS, - 0, nullptr, nullptr); - } + CaliperMetadataDBImpl() : m_root { CALI_INV_ID, CALI_INV_ID, {} } + { + setup_bootstrap_nodes(); + + m_alias_attr = + create_attribute("attribute.alias", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS, 0, nullptr, nullptr); + m_unit_attr = create_attribute("attribute.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS, 0, nullptr, nullptr); + } - ~CaliperMetadataDBImpl() { + ~CaliperMetadataDBImpl() + { for (const char* str : m_string_db) delete[] str; for (Node* n : m_nodes) @@ -515,23 +512,26 @@ struct CaliperMetadataDB::CaliperMetadataDBImpl } }; // CaliperMetadataDBImpl - // // --- Public API // -CaliperMetadataDB::CaliperMetadataDB() - : mP { new CaliperMetadataDBImpl } -{ } +CaliperMetadataDB::CaliperMetadataDB() : mP { new CaliperMetadataDBImpl } +{} CaliperMetadataDB::~CaliperMetadataDB() { if (Log::verbosity() >= 2) - print_statistics( Log(2).stream() ); + print_statistics(Log(2).stream()); } -Node* -CaliperMetadataDB::merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const Variant& value, IdMap& idmap) +Node* CaliperMetadataDB::merge_node( + cali_id_t node_id, + cali_id_t attr_id, + cali_id_t prnt_id, + const Variant& value, + IdMap& idmap +) { Variant v_data = value; @@ -542,8 +542,13 @@ CaliperMetadataDB::merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t pr return mP->merge_node(node_id, attr_id, prnt_id, v_data, idmap); } -Node* -CaliperMetadataDB::merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t prnt_id, const std::string& data, IdMap& idmap) +Node* CaliperMetadataDB::merge_node( + cali_id_t node_id, + cali_id_t attr_id, + cali_id_t prnt_id, + const std::string& data, + IdMap& idmap +) { Attribute attr = mP->attribute(::map_id(attr_id, idmap)); Variant v_data; @@ -556,135 +561,128 @@ CaliperMetadataDB::merge_node(cali_id_t node_id, cali_id_t attr_id, cali_id_t pr return mP->merge_node(node_id, attr_id, prnt_id, v_data, idmap); } -EntryList -CaliperMetadataDB::merge_snapshot(size_t n_nodes, const cali_id_t node_ids[], - size_t n_imm, const cali_id_t attr_ids[], const Variant values[], - const IdMap& idmap) const +EntryList CaliperMetadataDB::merge_snapshot( + size_t n_nodes, + const cali_id_t node_ids[], + size_t n_imm, + const cali_id_t attr_ids[], + const Variant values[], + const IdMap& idmap +) const { return mP->merge_snapshot(n_nodes, node_ids, n_imm, attr_ids, values, idmap); } -EntryList -CaliperMetadataDB::merge_snapshot(const CaliperMetadataAccessInterface& db, - const std::vector& rec) +EntryList CaliperMetadataDB::merge_snapshot(const CaliperMetadataAccessInterface& db, const std::vector& rec) { return mP->merge_snapshot(db, rec); } -Entry -CaliperMetadataDB::merge_entry(cali_id_t node_id, const IdMap& idmap) +Entry CaliperMetadataDB::merge_entry(cali_id_t node_id, const IdMap& idmap) { Node* node = mP->node(::map_id(node_id, idmap)); return node ? Entry(node) : Entry(); } -Entry -CaliperMetadataDB::merge_entry(cali_id_t attr_id, const std::string& data, const IdMap& idmap) +Entry CaliperMetadataDB::merge_entry(cali_id_t attr_id, const std::string& data, const IdMap& idmap) { Attribute attr = mP->attribute(::map_id(attr_id, idmap)); return attr ? Entry(attr, mP->make_variant(attr.type(), data)) : Entry(); } -void -CaliperMetadataDB::merge_global(cali_id_t node_id, const IdMap& idmap) +void CaliperMetadataDB::merge_global(cali_id_t node_id, const IdMap& idmap) { return mP->merge_global(node_id, idmap); } -void -CaliperMetadataDB::merge_global(cali_id_t attr_id, const std::string& data, const IdMap& idmap) +void CaliperMetadataDB::merge_global(cali_id_t attr_id, const std::string& data, const IdMap& idmap) { Attribute attr = mP->attribute(::map_id(attr_id, idmap)); if (attr) mP->set_global(attr, mP->make_variant(attr.type(), data)); } -Node* -CaliperMetadataDB::node(cali_id_t id) const +Node* CaliperMetadataDB::node(cali_id_t id) const { return mP->node(id); } -Attribute -CaliperMetadataDB::get_attribute(cali_id_t id) const +Attribute CaliperMetadataDB::get_attribute(cali_id_t id) const { return mP->attribute(id); } -Attribute -CaliperMetadataDB::get_attribute(const std::string& name) const +Attribute CaliperMetadataDB::get_attribute(const std::string& name) const { return mP->attribute(name); } -std::vector -CaliperMetadataDB::get_all_attributes() const +std::vector CaliperMetadataDB::get_all_attributes() const { return mP->get_attributes(); } -Node* -CaliperMetadataDB::make_tree_entry(size_t n, const Node* nodelist[], Node* parent) +Node* CaliperMetadataDB::make_tree_entry(size_t n, const Node* nodelist[], Node* parent) { return mP->make_tree_entry(n, nodelist, parent); } -Node* -CaliperMetadataDB::make_tree_entry(size_t n, const Attribute attr[], const Variant data[], Node* parent) +Node* CaliperMetadataDB::make_tree_entry(size_t n, const Attribute attr[], const Variant data[], Node* parent) { return mP->make_tree_entry(n, attr, data, parent); } -Attribute -CaliperMetadataDB::create_attribute(const std::string& name, cali_attr_type type, int prop, - int meta, const Attribute* meta_attr, const Variant* meta_data) +Attribute CaliperMetadataDB::create_attribute( + const std::string& name, + cali_attr_type type, + int prop, + int meta, + const Attribute* meta_attr, + const Variant* meta_data +) { return mP->create_attribute(name, type, prop, meta, meta_attr, meta_data); } -std::vector -CaliperMetadataDB::get_globals() +std::vector CaliperMetadataDB::get_globals() { return mP->get_globals(); } -void -CaliperMetadataDB::set_global(const Attribute& attr, const Variant& value) +void CaliperMetadataDB::set_global(const Attribute& attr, const Variant& value) { mP->set_global(attr, value); } -std::vector -CaliperMetadataDB::import_globals(CaliperMetadataAccessInterface& db) +std::vector CaliperMetadataDB::import_globals(CaliperMetadataAccessInterface& db) { return mP->import_globals(db, db.get_globals()); } -std::vector -CaliperMetadataDB::import_globals(CaliperMetadataAccessInterface& db, const std::vector& globals) +std::vector CaliperMetadataDB::import_globals( + CaliperMetadataAccessInterface& db, + const std::vector& globals +) { return mP->import_globals(db, globals); } -void -CaliperMetadataDB::add_attribute_aliases(const std::map& aliases) +void CaliperMetadataDB::add_attribute_aliases(const std::map& aliases) { - for (const auto &p: aliases) + for (const auto& p : aliases) mP->m_attr_aliases[p.first] = p.second; } -void -CaliperMetadataDB::add_attribute_units(const std::map& units) +void CaliperMetadataDB::add_attribute_units(const std::map& units) { - for (const auto &p: units) + for (const auto& p : units) mP->m_attr_units[p.first] = p.second; } -std::ostream& -CaliperMetadataDB::print_statistics(std::ostream& os) +std::ostream& CaliperMetadataDB::print_statistics(std::ostream& os) { - os << "CaliperMetadataDB: stored " << mP->m_nodes.size() - << " nodes, " << mP->m_string_db.size() << " strings." << std::endl; + os << "CaliperMetadataDB: stored " << mP->m_nodes.size() << " nodes, " << mP->m_string_db.size() << " strings." + << std::endl; return os; } diff --git a/src/reader/Expand.cpp b/src/reader/Expand.cpp index b8dd981b2..bfcec39f0 100644 --- a/src/reader/Expand.cpp +++ b/src/reader/Expand.cpp @@ -24,22 +24,20 @@ using namespace cali; using namespace std; -struct Expand::ExpandImpl -{ - set m_selected; - set m_deselected; +struct Expand::ExpandImpl { + set m_selected; + set m_deselected; std::map m_aliases; OutputStream m_os; - std::mutex m_os_lock; + std::mutex m_os_lock; - ExpandImpl(OutputStream& os) - : m_os(os) - { } + ExpandImpl(OutputStream& os) : m_os(os) {} - void parse(const string& field_string) { + void parse(const string& field_string) + { vector fields; util::split(field_string, ':', back_inserter(fields)); @@ -55,7 +53,8 @@ struct Expand::ExpandImpl } } - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { switch (spec.select.selection) { case QuerySpec::AttributeSelection::Default: case QuerySpec::AttributeSelection::All: @@ -65,19 +64,19 @@ struct Expand::ExpandImpl // doesn't make much sense break; case QuerySpec::AttributeSelection::List: - m_selected = - std::set(spec.select.list.begin(), spec.select.list.end()); + m_selected = std::set(spec.select.list.begin(), spec.select.list.end()); break; } m_aliases = spec.aliases; } - - void print(CaliperMetadataAccessInterface& db, const EntryList& list) { + + void print(CaliperMetadataAccessInterface& db, const EntryList& list) + { int nentry = 0; std::ostringstream os; - + for (const Entry& e : list) { if (e.is_reference()) { vector nodes; @@ -94,8 +93,10 @@ struct Expand::ExpandImpl if (nodes.empty()) continue; - stable_sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) { return a->attribute() < b->attribute(); } ); - + stable_sort(nodes.begin(), nodes.end(), [](const Node* a, const Node* b) { + return a->attribute() < b->attribute(); + }); + cali_id_t prev_attr_id = CALI_INV_ID; for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) { @@ -107,7 +108,7 @@ struct Expand::ExpandImpl if (it != m_aliases.end()) name = it->second; } - + os << (nentry++ ? "," : "") << name << '='; prev_attr_id = (*it)->attribute(); } else { @@ -130,27 +131,23 @@ struct Expand::ExpandImpl os << (nentry++ ? "," : "") << name << '=' << e.value(); } } - + if (nentry > 0) { - std::lock_guard - g(m_os_lock); + std::lock_guard g(m_os_lock); std::ostream* real_os = m_os.stream(); - + *real_os << os.str() << endl; } } }; - -Expand::Expand(OutputStream& os, const string& field_string) - : mP { new ExpandImpl(os) } +Expand::Expand(OutputStream& os, const string& field_string) : mP { new ExpandImpl(os) } { mP->parse(field_string); } -Expand::Expand(OutputStream& os, const QuerySpec& spec) - : mP { new ExpandImpl(os) } +Expand::Expand(OutputStream& os, const QuerySpec& spec) : mP { new ExpandImpl(os) } { mP->configure(spec); } @@ -160,14 +157,12 @@ Expand::~Expand() mP.reset(); } -void -Expand::operator()(CaliperMetadataAccessInterface& db, const EntryList& list) const +void Expand::operator() (CaliperMetadataAccessInterface& db, const EntryList& list) const { mP->print(db, list); } -void -Expand::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) +void Expand::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->print(db, list); } diff --git a/src/reader/Expand.h b/src/reader/Expand.h index 02a1ffb83..23539a228 100644 --- a/src/reader/Expand.h +++ b/src/reader/Expand.h @@ -20,7 +20,7 @@ struct QuerySpec; /// \brief Prints expanded snapshot records in CSV form /// \ingroup ReaderAPI - + class Expand : public Formatter { struct ExpandImpl; @@ -33,7 +33,7 @@ class Expand : public Formatter ~Expand(); - void operator()(CaliperMetadataAccessInterface&, const EntryList&) const; + void operator() (CaliperMetadataAccessInterface&, const EntryList&) const; void process_record(CaliperMetadataAccessInterface&, const EntryList&); }; diff --git a/src/reader/FlatExclusiveRegionProfile.cpp b/src/reader/FlatExclusiveRegionProfile.cpp index 982eb3911..58aa98478 100644 --- a/src/reader/FlatExclusiveRegionProfile.cpp +++ b/src/reader/FlatExclusiveRegionProfile.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #include "caliper/reader/FlatExclusiveRegionProfile.h" @@ -10,21 +10,21 @@ using namespace cali; -struct FlatExclusiveRegionProfile::FlatExclusiveRegionProfileImpl -{ - double total = 0.0; - double total_reg = 0.0; +struct FlatExclusiveRegionProfile::FlatExclusiveRegionProfileImpl { + double total = 0.0; + double total_reg = 0.0; std::map reg_profile; - + Attribute metric_attr; Attribute region_attr; - void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) { + void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) + { cali_id_t metric_attr_id = metric_attr.id(); - auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e){ - return e.attribute() == metric_attr_id; - }); + auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e) { + return e.attribute() == metric_attr_id; + }); if (metric_entry_it == rec.end()) return; @@ -35,27 +35,27 @@ struct FlatExclusiveRegionProfile::FlatExclusiveRegionProfileImpl auto region_entry_it = rec.end(); if (!region_attr) - region_entry_it = std::find_if(rec.begin(), rec.end(), [&db](const Entry& e){ - return db.get_attribute(e.attribute()).is_nested(); - }); + region_entry_it = std::find_if(rec.begin(), rec.end(), [&db](const Entry& e) { + return db.get_attribute(e.attribute()).is_nested(); + }); else { cali_id_t attr_id = region_attr.id(); - region_entry_it = std::find_if(rec.begin(), rec.end(), [attr_id](const Entry& e){ - return e.attribute() == attr_id; - }); + region_entry_it = + std::find_if(rec.begin(), rec.end(), [attr_id](const Entry& e) { return e.attribute() == attr_id; }); } - - if (region_entry_it != rec.end()) { + + if (region_entry_it != rec.end()) { total_reg += val; reg_profile[region_entry_it->value().to_string()] += val; } } }; - -FlatExclusiveRegionProfile::FlatExclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name) +FlatExclusiveRegionProfile::FlatExclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name +) : mP { new FlatExclusiveRegionProfileImpl } { mP->metric_attr = db.get_attribute(metric_attr_name); @@ -64,14 +64,12 @@ FlatExclusiveRegionProfile::FlatExclusiveRegionProfile(CaliperMetadataAccessInte mP->region_attr = db.get_attribute(region_attr_name); } -void -FlatExclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) +void FlatExclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) { mP->process_record(db, rec); } -std::tuple< std::map, double, double > -FlatExclusiveRegionProfile::result() const +std::tuple, double, double> FlatExclusiveRegionProfile::result() const { return std::make_tuple(mP->reg_profile, mP->total_reg, mP->total); } diff --git a/src/reader/FlatInclusiveRegionProfile.cpp b/src/reader/FlatInclusiveRegionProfile.cpp index 02f08402b..e51e3af67 100644 --- a/src/reader/FlatInclusiveRegionProfile.cpp +++ b/src/reader/FlatInclusiveRegionProfile.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #include "caliper/reader/FlatInclusiveRegionProfile.h" @@ -10,22 +10,21 @@ using namespace cali; - -struct FlatInclusiveRegionProfile::FlatInclusiveRegionProfileImpl -{ - double total = 0.0; - double total_reg = 0.0; +struct FlatInclusiveRegionProfile::FlatInclusiveRegionProfileImpl { + double total = 0.0; + double total_reg = 0.0; std::map reg_profile; - + Attribute metric_attr; Attribute region_attr; - void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) { + void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) + { cali_id_t metric_attr_id = metric_attr.id(); - auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e){ - return e.attribute() == metric_attr_id; - }); + auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e) { + return e.attribute() == metric_attr_id; + }); if (metric_entry_it == rec.end()) return; @@ -33,17 +32,16 @@ struct FlatInclusiveRegionProfile::FlatInclusiveRegionProfileImpl double val = metric_entry_it->value().to_double(); total += val; - cali_id_t r_a_id = region_attr.id(); - bool have_reg_entry = false; - + cali_id_t r_a_id = region_attr.id(); + bool have_reg_entry = false; + for (const Entry& e : rec) { if (!e.is_reference()) continue; for (const Node* node = e.node(); node && node->attribute() != CALI_INV_ID; node = node->parent()) { - cali_id_t n_a_id = node->attribute(); - bool is_target_reg = - (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); + cali_id_t n_a_id = node->attribute(); + bool is_target_reg = (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); if (is_target_reg) { have_reg_entry = true; @@ -57,10 +55,11 @@ struct FlatInclusiveRegionProfile::FlatInclusiveRegionProfileImpl } }; - -FlatInclusiveRegionProfile::FlatInclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name) +FlatInclusiveRegionProfile::FlatInclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name +) : mP { new FlatInclusiveRegionProfileImpl } { mP->metric_attr = db.get_attribute(metric_attr_name); @@ -69,14 +68,12 @@ FlatInclusiveRegionProfile::FlatInclusiveRegionProfile(CaliperMetadataAccessInte mP->region_attr = db.get_attribute(region_attr_name); } -void -FlatInclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) +void FlatInclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) { mP->process_record(db, rec); } -std::tuple< std::map, double, double > -FlatInclusiveRegionProfile::result() const +std::tuple, double, double> FlatInclusiveRegionProfile::result() const { return std::make_tuple(mP->reg_profile, mP->total_reg, mP->total); } diff --git a/src/reader/FormatProcessor.cpp b/src/reader/FormatProcessor.cpp index 9415b587c..ec751ff37 100644 --- a/src/reader/FormatProcessor.cpp +++ b/src/reader/FormatProcessor.cpp @@ -25,27 +25,17 @@ const char* tree_kernel_args[] = { "path-attributes", "column-width", "print-g const char* table_kernel_args[] = { "column-width", "print-globals" }; const char* json_kernel_args[] = { "object", "pretty", "quote-all", "separate-nested", "records", "split" }; -enum FormatterID { - Cali = 0, - Json = 1, - Expand = 2, - Format = 3, - Table = 4, - Tree = 5, - JsonSplit = 6 -}; +enum FormatterID { Cali = 0, Json = 1, Expand = 2, Format = 3, Table = 4, Tree = 5, JsonSplit = 6 }; -const QuerySpec::FunctionSignature formatters[] = { - { FormatterID::Cali, "cali", 0, 0, nullptr }, - { FormatterID::Json, "json", 0, 6, json_kernel_args }, - { FormatterID::Expand, "expand", 0, 0, nullptr }, - { FormatterID::Format, "format", 1, 2, format_kernel_args }, - { FormatterID::Table, "table", 0, 2, table_kernel_args }, - { FormatterID::Tree, "tree", 0, 3, tree_kernel_args }, - { FormatterID::JsonSplit, "json-split", 0, 0, nullptr }, +const QuerySpec::FunctionSignature formatters[] = { { FormatterID::Cali, "cali", 0, 0, nullptr }, + { FormatterID::Json, "json", 0, 6, json_kernel_args }, + { FormatterID::Expand, "expand", 0, 0, nullptr }, + { FormatterID::Format, "format", 1, 2, format_kernel_args }, + { FormatterID::Table, "table", 0, 2, table_kernel_args }, + { FormatterID::Tree, "tree", 0, 3, tree_kernel_args }, + { FormatterID::JsonSplit, "json-split", 0, 0, nullptr }, - QuerySpec::FunctionSignatureTerminator -}; + QuerySpec::FunctionSignatureTerminator }; class CaliFormatter : public Formatter { @@ -53,27 +43,24 @@ class CaliFormatter : public Formatter public: - CaliFormatter(OutputStream& os) - : m_writer(CaliWriter(os)) - { } + CaliFormatter(OutputStream& os) : m_writer(CaliWriter(os)) {} - void process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { + void process_record(CaliperMetadataAccessInterface& db, const EntryList& list) + { m_writer.write_snapshot(db, list); } - void flush(CaliperMetadataAccessInterface& db, std::ostream&) { - m_writer.write_globals(db, db.get_globals()); - } + void flush(CaliperMetadataAccessInterface& db, std::ostream&) { m_writer.write_globals(db, db.get_globals()); } }; -} +} // namespace -struct FormatProcessor::FormatProcessorImpl -{ +struct FormatProcessor::FormatProcessorImpl { Formatter* m_formatter; OutputStream m_stream; - void create_formatter(const QuerySpec& spec) { + void create_formatter(const QuerySpec& spec) + { if (spec.format.opt == QuerySpec::FormatSpec::Default) { m_formatter = new CaliFormatter(m_stream); } else { @@ -103,43 +90,35 @@ struct FormatProcessor::FormatProcessorImpl } } - FormatProcessorImpl(OutputStream& stream, const QuerySpec& spec) - : m_formatter(nullptr), m_stream(stream) + FormatProcessorImpl(OutputStream& stream, const QuerySpec& spec) : m_formatter(nullptr), m_stream(stream) { create_formatter(spec); } - ~FormatProcessorImpl() - { - delete m_formatter; - } + ~FormatProcessorImpl() { delete m_formatter; } }; - FormatProcessor::FormatProcessor(const QuerySpec& spec, OutputStream& stream) : mP(new FormatProcessorImpl(stream, spec)) -{ } +{} FormatProcessor::~FormatProcessor() { mP.reset(); } -const QuerySpec::FunctionSignature* -FormatProcessor::formatter_defs() +const QuerySpec::FunctionSignature* FormatProcessor::formatter_defs() { return ::formatters; } -void -FormatProcessor::process_record(CaliperMetadataAccessInterface& db, const EntryList& rec) +void FormatProcessor::process_record(CaliperMetadataAccessInterface& db, const EntryList& rec) { if (mP->m_formatter) mP->m_formatter->process_record(db, rec); } -void -FormatProcessor::flush(CaliperMetadataAccessInterface& db) +void FormatProcessor::flush(CaliperMetadataAccessInterface& db) { if (mP->m_formatter) { std::ostream* os = mP->m_stream.stream(); diff --git a/src/reader/Formatter.h b/src/reader/Formatter.h index e0408d797..060496df4 100644 --- a/src/reader/Formatter.h +++ b/src/reader/Formatter.h @@ -16,19 +16,18 @@ class CaliperMetadataAccessInterface; /// \class Formatter /// \brief Abstract base class for output formatters. /// \ingroup ReaderAPI - + class Formatter { public: - virtual ~Formatter() { } + virtual ~Formatter() {} /// Process a snapshot record. virtual void process_record(CaliperMetadataAccessInterface&, const EntryList&) = 0; /// Flush processed contents to stream. Need not be implemented for stream formatters. - virtual void flush(CaliperMetadataAccessInterface&, std::ostream&) - { } + virtual void flush(CaliperMetadataAccessInterface&, std::ostream&) {} }; } // namespace cali diff --git a/src/reader/JsonFormatter.cpp b/src/reader/JsonFormatter.cpp index 358b8b113..a246808d6 100644 --- a/src/reader/JsonFormatter.cpp +++ b/src/reader/JsonFormatter.cpp @@ -26,37 +26,31 @@ using namespace cali; using namespace std; -struct JsonFormatter::JsonFormatterImpl -{ - set m_selected; - set m_deselected; +struct JsonFormatter::JsonFormatterImpl { + set m_selected; + set m_deselected; OutputStream m_os; - std::mutex m_os_lock; + std::mutex m_os_lock; - unsigned m_num_recs = 0; + unsigned m_num_recs = 0; - enum Layout { - Records, - Split, - Object - }; + enum Layout { Records, Split, Object }; - Layout m_layout = Records; + Layout m_layout = Records; - bool m_opt_split = false; - bool m_opt_pretty = false; - bool m_opt_quote_all = false; - bool m_opt_sep_nested = false; + bool m_opt_split = false; + bool m_opt_pretty = false; + bool m_opt_quote_all = false; + bool m_opt_sep_nested = false; - std::map m_aliases; + std::map m_aliases; - JsonFormatterImpl(OutputStream &os) - : m_os(os) - { } + JsonFormatterImpl(OutputStream& os) : m_os(os) {} - void parse(const string& field_string) { + void parse(const string& field_string) + { vector fields; util::split(field_string, ':', back_inserter(fields)); @@ -72,8 +66,8 @@ struct JsonFormatter::JsonFormatterImpl } } - - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { for (auto p : spec.format.kwargs) { if (p.first == "pretty") m_opt_pretty = true; @@ -98,19 +92,18 @@ struct JsonFormatter::JsonFormatterImpl // doesn't make much sense break; case QuerySpec::AttributeSelection::List: - m_selected = - std::set(spec.select.list.begin(), spec.select.list.end()); + m_selected = std::set(spec.select.list.begin(), spec.select.list.end()); break; } m_aliases = spec.aliases; } - inline std::string get_key(const Attribute& attr) { + inline std::string get_key(const Attribute& attr) + { std::string name = attr.name(); - bool selected = - m_selected.count(name) > 0 && !(m_deselected.count(name) > 0); + bool selected = m_selected.count(name) > 0 && !(m_deselected.count(name) > 0); if (!selected && (!m_selected.empty() || attr.is_hidden() || attr.is_global())) return ""; @@ -123,19 +116,22 @@ struct JsonFormatter::JsonFormatterImpl return (it == m_aliases.end() ? name : it->second); } - void begin_records_section(std::ostream& os) { + void begin_records_section(std::ostream& os) + { if (m_layout == Records) os << "[\n"; else if (m_layout == Object) os << "{\n\"records\": [\n"; } - void end_records_section(std::ostream& os) { + void end_records_section(std::ostream& os) + { if (m_layout == Records || m_layout == Object) os << "\n]"; } - void print(CaliperMetadataAccessInterface& db, const EntryList& list) { + void print(CaliperMetadataAccessInterface& db, const EntryList& list) + { std::map noquote_kvs; std::map quote_kvs; @@ -159,8 +155,8 @@ struct JsonFormatter::JsonFormatterImpl it->second = node->data().to_string().append("/").append(it->second); } } else if (e.is_immediate()) { - Attribute attr = db.get_attribute(e.attribute()); - std::string key = get_key(attr); + Attribute attr = db.get_attribute(e.attribute()); + std::string key = get_key(attr); if (key.empty()) continue; @@ -178,8 +174,7 @@ struct JsonFormatter::JsonFormatterImpl // write the key-value pairs // - std::lock_guard - g(m_os_lock); + std::lock_guard g(m_os_lock); std::ostream* real_os = m_os.stream(); @@ -190,15 +185,13 @@ struct JsonFormatter::JsonFormatterImpl *real_os << (m_num_recs > 0 ? ",\n" : "") << "{"; int count = 0; - for (auto &p : quote_kvs) { - *real_os << (count++ > 0 ? "," : "") - << (m_opt_pretty ? "\n\t" : ""); + for (auto& p : quote_kvs) { + *real_os << (count++ > 0 ? "," : "") << (m_opt_pretty ? "\n\t" : ""); util::write_json_esc_string(*real_os << "\"", p.first) << "\":"; util::write_json_esc_string(*real_os << "\"", p.second) << "\""; } - for (auto &p : noquote_kvs) { - *real_os << (count++ > 0 ? "," : "") - << (m_opt_pretty ? "\n\t" : ""); + for (auto& p : noquote_kvs) { + *real_os << (count++ > 0 ? "," : "") << (m_opt_pretty ? "\n\t" : ""); util::write_json_esc_string(*real_os << "\"", p.first) << "\":"; util::write_json_esc_string(*real_os, p.second); } @@ -209,7 +202,8 @@ struct JsonFormatter::JsonFormatterImpl } } - std::ostream& write_attributes(CaliperMetadataAccessInterface& db, std::ostream& os) { + std::ostream& write_attributes(CaliperMetadataAccessInterface& db, std::ostream& os) + { std::vector attrs; if (m_selected.empty()) @@ -220,23 +214,24 @@ struct JsonFormatter::JsonFormatterImpl // remove nested and hidden attributes if (!m_opt_sep_nested) - attrs.erase(std::remove_if(attrs.begin(), attrs.end(), - [](const Attribute& a){ return a.is_nested(); }), - attrs.end()); + attrs.erase( + std::remove_if(attrs.begin(), attrs.end(), [](const Attribute& a) { return a.is_nested(); }), + attrs.end() + ); - attrs.erase(std::remove_if(attrs.begin(), attrs.end(), - [](const Attribute& a){ return a.is_hidden(); }), - attrs.end()); + attrs.erase( + std::remove_if(attrs.begin(), attrs.end(), [](const Attribute& a) { return a.is_hidden(); }), + attrs.end() + ); // write "attr1": { "prop": "value", ... } , "attr2" : ... int count = 0; for (const Attribute& a : attrs) { - os << (count++ > 0 ? ",\n" : "\n") << (m_opt_pretty ? "\t" : "") - << '\"' << a.name() << "\": {"; + os << (count++ > 0 ? ",\n" : "\n") << (m_opt_pretty ? "\t" : "") << '\"' << a.name() << "\": {"; // encode some properties - os << "\"is_global\": " << (a.is_global() ? "true" : "false") + os << "\"is_global\": " << (a.is_global() ? "true" : "false") << ",\"is_nested\": " << (a.is_nested() ? "true" : "false"); // print meta-info @@ -251,7 +246,8 @@ struct JsonFormatter::JsonFormatterImpl return os; } - std::ostream& write_globals(CaliperMetadataAccessInterface& db, std::ostream& os) { + std::ostream& write_globals(CaliperMetadataAccessInterface& db, std::ostream& os) + { std::vector globals = db.get_globals(); std::map global_vals; @@ -269,7 +265,7 @@ struct JsonFormatter::JsonFormatterImpl global_vals[e.attribute()] = e.value().to_string(); int count = 0; - for (auto &p : global_vals) { + for (auto& p : global_vals) { if (count++ > 0) os << ",\n"; if (m_opt_pretty) @@ -282,7 +278,8 @@ struct JsonFormatter::JsonFormatterImpl return os; } - void flush(CaliperMetadataAccessInterface& db) { + void flush(CaliperMetadataAccessInterface& db) + { std::ostream* real_os = m_os.stream(); // close records section @@ -301,8 +298,7 @@ struct JsonFormatter::JsonFormatterImpl } }; -JsonFormatter::JsonFormatter(OutputStream &os, const QuerySpec& spec) - : mP { new JsonFormatterImpl(os) } +JsonFormatter::JsonFormatter(OutputStream& os, const QuerySpec& spec) : mP { new JsonFormatterImpl(os) } { mP->configure(spec); } @@ -312,8 +308,7 @@ JsonFormatter::~JsonFormatter() mP.reset(); } -void -JsonFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) +void JsonFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->print(db, list); } diff --git a/src/reader/JsonSplitFormatter.cpp b/src/reader/JsonSplitFormatter.cpp index 7f04d088b..a4abfdae7 100644 --- a/src/reader/JsonSplitFormatter.cpp +++ b/src/reader/JsonSplitFormatter.cpp @@ -35,23 +35,24 @@ class Hierarchy class HierarchyNode : public util::LockfreeIntrusiveTree { util::LockfreeIntrusiveTree::Node m_treenode; - cali_id_t m_id; - std::string m_label; - std::string m_column; + cali_id_t m_id; + std::string m_label; + std::string m_column; public: HierarchyNode(cali_id_t id, const std::string& label, const std::string& column) : util::LockfreeIntrusiveTree(this, &HierarchyNode::m_treenode), - m_id(id), - m_label(label), - m_column(column) - { } + m_id(id), + m_label(label), + m_column(column) + {} const std::string& label() const { return m_label; } - std::ostream& write_json(std::ostream& os) const { - util::write_json_esc_string(os << "{ \"label\": \"", m_label ) << "\""; + std::ostream& write_json(std::ostream& os) const + { + util::write_json_esc_string(os << "{ \"label\": \"", m_label) << "\""; util::write_json_esc_string(os << ", \"column\": \"", m_column) << "\""; if (parent() && parent()->id() != CALI_INV_ID) @@ -65,12 +66,13 @@ class Hierarchy cali_id_t id() const { return m_id; } }; - HierarchyNode* m_root; + HierarchyNode* m_root; std::mutex m_nodes_lock; std::vector m_nodes; - void recursive_delete(HierarchyNode* node) { + void recursive_delete(HierarchyNode* node) + { HierarchyNode* child = node->first_child(); while (child) { @@ -84,18 +86,18 @@ class Hierarchy public: - Hierarchy() - : m_root(new HierarchyNode(CALI_INV_ID, "", "")) - { } + Hierarchy() : m_root(new HierarchyNode(CALI_INV_ID, "", "")) {} - ~Hierarchy() { + ~Hierarchy() + { // delete all nodes recursive_delete(m_root); m_root = nullptr; } /// \brief Return Node ID for the given path - cali_id_t get_id(const std::vector& vec, const std::string& column) { + cali_id_t get_id(const std::vector& vec, const std::string& column) + { HierarchyNode* node = m_root; for (const Entry& e : vec) { @@ -106,8 +108,7 @@ class Hierarchy ; if (!node) { - std::lock_guard - g(m_nodes_lock); + std::lock_guard g(m_nodes_lock); node = new HierarchyNode(m_nodes.size(), label, column); m_nodes.push_back(node); @@ -119,9 +120,9 @@ class Hierarchy return node->id(); } - std::ostream& write_nodes(std::ostream& os) { - std::lock_guard - g(m_nodes_lock); + std::ostream& write_nodes(std::ostream& os) + { + std::lock_guard g(m_nodes_lock); os << "\"nodes\": ["; @@ -135,11 +136,9 @@ class Hierarchy } }; -} - +} // namespace -struct JsonSplitFormatter::JsonSplitFormatterImpl -{ +struct JsonSplitFormatter::JsonSplitFormatterImpl { bool m_select_all; bool m_select_path; std::vector m_attr_names; @@ -152,25 +151,25 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl std::vector attributes; bool is_hierarchy; - static Column make_column(const std::string& title, const Attribute& a) { + static Column make_column(const std::string& title, const Attribute& a) + { Column c; - c.title = title; + c.title = title; c.attributes.push_back(a); c.is_hierarchy = !(a.store_as_value()); return c; } }; - Hierarchy m_hierarchy; + Hierarchy m_hierarchy; - std::vector m_records; - std::mutex m_records_lock; + std::vector m_records; + std::mutex m_records_lock; - JsonSplitFormatterImpl() - : m_select_all(false) - { } + JsonSplitFormatterImpl() : m_select_all(false) {} - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { m_select_all = false; m_select_path = spec.select.use_path; m_attr_names.clear(); @@ -180,8 +179,7 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl case QuerySpec::AttributeSelection::All: // Explicitly use aggregation key and ops if there is a GROUPBY if (spec.groupby.selection == QuerySpec::AttributeSelection::List) { - m_attr_names.insert(m_attr_names.end(), - spec.groupby.list.begin(), spec.groupby.list.end()); + m_attr_names.insert(m_attr_names.end(), spec.groupby.list.begin(), spec.groupby.list.end()); for (auto op : spec.aggregate.list) m_attr_names.push_back(Aggregator::get_aggregation_attribute_name(op)); @@ -194,37 +192,32 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl case QuerySpec::AttributeSelection::None: break; case QuerySpec::AttributeSelection::List: - m_attr_names.insert(m_attr_names.end(), - spec.select.list.begin(), - spec.select.list.end()); + m_attr_names.insert(m_attr_names.end(), spec.select.list.begin(), spec.select.list.end()); break; } m_aliases = spec.aliases; } - std::vector init_columns(const CaliperMetadataAccessInterface& db) const { + std::vector init_columns(const CaliperMetadataAccessInterface& db) const + { std::vector columns; - auto attrs = db.get_all_attributes(); + auto attrs = db.get_all_attributes(); auto attrs_rem = attrs.end(); if (m_select_all) { // filter out hidden and global attributes - attrs_rem = - std::remove_if(attrs.begin(), attrs.end(), [](const Attribute& a) { - return (a.is_hidden() || a.is_global()); - }); + attrs_rem = std::remove_if(attrs.begin(), attrs.end(), [](const Attribute& a) { + return (a.is_hidden() || a.is_global()); + }); } else { // only include selected attributes - attrs_rem = - std::remove_if(attrs.begin(), attrs.end(), [this](const Attribute& a) { - bool select = - (m_select_path && a.is_nested()) || - std::find(m_attr_names.begin(), m_attr_names.end(), - a.name()) != m_attr_names.end(); - return !select; - }); + attrs_rem = std::remove_if(attrs.begin(), attrs.end(), [this](const Attribute& a) { + bool select = (m_select_path && a.is_nested()) + || std::find(m_attr_names.begin(), m_attr_names.end(), a.name()) != m_attr_names.end(); + return !select; + }); } attrs.erase(attrs_rem, attrs.end()); @@ -254,7 +247,13 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl return columns; } - void write_hierarchy_entry(std::ostream& os, const EntryList& list, const std::vector& path_attrs, const std::string& column) { + void write_hierarchy_entry( + std::ostream& os, + const EntryList& list, + const std::vector& path_attrs, + const std::string& column + ) + { std::vector path; for (const Entry& e : list) @@ -274,9 +273,10 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl os << "null"; } - void write_immediate_entry(std::ostream& os, const EntryList& list, const Attribute& attr) { - cali_attr_type type = attr.type(); - bool quote = !(type == CALI_TYPE_INT || type == CALI_TYPE_UINT || type == CALI_TYPE_DOUBLE); + void write_immediate_entry(std::ostream& os, const EntryList& list, const Attribute& attr) + { + cali_attr_type type = attr.type(); + bool quote = !(type == CALI_TYPE_INT || type == CALI_TYPE_UINT || type == CALI_TYPE_DOUBLE); for (const Entry& e : list) if (e.attribute() == attr.id()) { @@ -291,15 +291,16 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl os << "null"; } - void process_record(const CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_records_lock); + void process_record(const CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_records_lock); m_records.push_back(list); } - std::ostream& write_globals(std::ostream& os, CaliperMetadataAccessInterface& db) { - std::vector globals = db.get_globals(); + std::ostream& write_globals(std::ostream& os, CaliperMetadataAccessInterface& db) + { + std::vector globals = db.get_globals(); std::map global_vals; for (const Entry& e : globals) @@ -315,7 +316,7 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl else global_vals[e.attribute()] = e.value().to_string(); - for (auto &p : global_vals) { + for (auto& p : global_vals) { util::write_json_esc_string(os << ",\n \"", db.get_attribute(p.first).name()) << "\": "; util::write_json_esc_string(os << '"', p.second) << '\"'; } @@ -323,7 +324,8 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl return os; } - std::ostream& write_column_metadata(std::ostream& os, const Column& column, CaliperMetadataAccessInterface& db) { + std::ostream& write_column_metadata(std::ostream& os, const Column& column, CaliperMetadataAccessInterface& db) + { os << "\"is_value\": " << (column.is_hierarchy ? "false" : "true"); // for single-attribute columns (i.e. not "path"), write metadata @@ -333,22 +335,27 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl if (node) node = node->parent(); - for ( ; node && node->id() != CALI_INV_ID; node = node->parent()) { + for (; node && node->id() != CALI_INV_ID; node = node->parent()) { Attribute attr = db.get_attribute(node->attribute()); // skip bootstrap info and hidden attributes if (attr.id() < 12 || attr.is_hidden()) continue; - util::write_json_esc_string(os << ", \"", attr.name_c_str()) << "\": "; - util::write_json_esc_string(os << "\"", node->data().to_string()) << "\""; + util::write_json_esc_string(os << ", \"", attr.name_c_str()) << "\": "; + util::write_json_esc_string(os << "\"", node->data().to_string()) << "\""; } } return os; } - std::ostream& write_metadata(CaliperMetadataAccessInterface& db, const std::vector& columns, std::ostream& os) { + std::ostream& write_metadata( + CaliperMetadataAccessInterface& db, + const std::vector& columns, + std::ostream& os + ) + { // start "columns" os << ",\n \"columns\": ["; @@ -372,12 +379,13 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl } // close "column_metadata", write "nodes" - m_hierarchy.write_nodes( os << " ],\n " ); + m_hierarchy.write_nodes(os << " ],\n "); return write_globals(os, db); } - void flush(CaliperMetadataAccessInterface& db, std::ostream& os) { + void flush(CaliperMetadataAccessInterface& db, std::ostream& os) + { auto columns = init_columns(db); os << "{\n \"data\": ["; @@ -410,9 +418,7 @@ struct JsonSplitFormatter::JsonSplitFormatterImpl } }; - -JsonSplitFormatter::JsonSplitFormatter(const QuerySpec& spec) - : mP { new JsonSplitFormatterImpl } +JsonSplitFormatter::JsonSplitFormatter(const QuerySpec& spec) : mP { new JsonSplitFormatterImpl } { mP->configure(spec); } @@ -422,8 +428,7 @@ JsonSplitFormatter::~JsonSplitFormatter() mP.reset(); } -void -JsonSplitFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) +void JsonSplitFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->process_record(db, list); } diff --git a/src/reader/NestedExclusiveRegionProfile.cpp b/src/reader/NestedExclusiveRegionProfile.cpp index 89e8ad6b6..daa83fd04 100644 --- a/src/reader/NestedExclusiveRegionProfile.cpp +++ b/src/reader/NestedExclusiveRegionProfile.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #include "caliper/reader/NestedExclusiveRegionProfile.h" @@ -16,15 +16,14 @@ namespace std::string get_path(CaliperMetadataAccessInterface& db, const Node* node, cali_id_t r_a_id) { std::string path; - + if (!node || node->attribute() == CALI_INV_ID) return path; - + path = get_path(db, node->parent(), r_a_id); - cali_id_t n_a_id = node->attribute(); - bool is_target_reg = - (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); + cali_id_t n_a_id = node->attribute(); + bool is_target_reg = (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); if (is_target_reg) path.append(path.empty() ? "" : "/").append(node->data().to_string()); @@ -32,24 +31,23 @@ std::string get_path(CaliperMetadataAccessInterface& db, const Node* node, cali_ return path; } -} // namespace [anonymous] +} // namespace - -struct NestedExclusiveRegionProfile::NestedExclusiveRegionProfileImpl -{ - double total = 0.0; - double total_reg = 0.0; +struct NestedExclusiveRegionProfile::NestedExclusiveRegionProfileImpl { + double total = 0.0; + double total_reg = 0.0; std::map reg_profile; - + Attribute metric_attr; Attribute region_attr; - void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) { + void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) + { cali_id_t metric_attr_id = metric_attr.id(); - auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e){ - return e.attribute() == metric_attr_id; - }); + auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e) { + return e.attribute() == metric_attr_id; + }); if (metric_entry_it == rec.end()) return; @@ -58,14 +56,13 @@ struct NestedExclusiveRegionProfile::NestedExclusiveRegionProfileImpl total += val; cali_id_t r_a_id = region_attr.id(); - + for (const Entry& e : rec) { if (!e.is_reference()) continue; - - cali_id_t n_a_id = e.attribute(); - bool is_target_reg = - (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); + + cali_id_t n_a_id = e.attribute(); + bool is_target_reg = (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); if (is_target_reg) { total_reg += val; @@ -76,10 +73,11 @@ struct NestedExclusiveRegionProfile::NestedExclusiveRegionProfileImpl } }; - -NestedExclusiveRegionProfile::NestedExclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name) +NestedExclusiveRegionProfile::NestedExclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name +) : mP { new NestedExclusiveRegionProfileImpl } { mP->metric_attr = db.get_attribute(metric_attr_name); @@ -88,14 +86,12 @@ NestedExclusiveRegionProfile::NestedExclusiveRegionProfile(CaliperMetadataAccess mP->region_attr = db.get_attribute(region_attr_name); } -void -NestedExclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) +void NestedExclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) { mP->process_record(db, rec); } -std::tuple< std::map, double, double > -NestedExclusiveRegionProfile::result() const +std::tuple, double, double> NestedExclusiveRegionProfile::result() const { return std::make_tuple(mP->reg_profile, mP->total_reg, mP->total); } diff --git a/src/reader/NestedInclusiveRegionProfile.cpp b/src/reader/NestedInclusiveRegionProfile.cpp index 8440bb64e..a5352dcc2 100644 --- a/src/reader/NestedInclusiveRegionProfile.cpp +++ b/src/reader/NestedInclusiveRegionProfile.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #include "caliper/reader/NestedInclusiveRegionProfile.h" @@ -13,22 +13,23 @@ using namespace cali; namespace { -std::string recursive_update(CaliperMetadataAccessInterface& db, - std::map& reg_profile, - const Node* node, - double val, - cali_id_t r_a_id) +std::string recursive_update( + CaliperMetadataAccessInterface& db, + std::map& reg_profile, + const Node* node, + double val, + cali_id_t r_a_id +) { std::string path; - + if (!node || node->attribute() == CALI_INV_ID) return path; - + path = recursive_update(db, reg_profile, node->parent(), val, r_a_id); - cali_id_t n_a_id = node->attribute(); - bool is_target_reg = - (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); + cali_id_t n_a_id = node->attribute(); + bool is_target_reg = (r_a_id == CALI_INV_ID ? db.get_attribute(n_a_id).is_nested() : r_a_id == n_a_id); if (is_target_reg) { path.append(path.empty() ? "" : "/").append(node->data().to_string()); @@ -38,24 +39,23 @@ std::string recursive_update(CaliperMetadataAccessInterface& db, return path; } -} // namespace [anonymous] - +} // namespace -struct NestedInclusiveRegionProfile::NestedInclusiveRegionProfileImpl -{ - double total = 0.0; - double total_reg = 0.0; +struct NestedInclusiveRegionProfile::NestedInclusiveRegionProfileImpl { + double total = 0.0; + double total_reg = 0.0; std::map reg_profile; - + Attribute metric_attr; Attribute region_attr; - void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) { + void process_record(CaliperMetadataAccessInterface& db, const std::vector& rec) + { cali_id_t metric_attr_id = metric_attr.id(); - auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e){ - return e.attribute() == metric_attr_id; - }); + auto metric_entry_it = std::find_if(rec.begin(), rec.end(), [metric_attr_id](const Entry& e) { + return e.attribute() == metric_attr_id; + }); if (metric_entry_it == rec.end()) return; @@ -63,9 +63,9 @@ struct NestedInclusiveRegionProfile::NestedInclusiveRegionProfileImpl double val = metric_entry_it->value().to_double(); total += val; - cali_id_t r_a_id = region_attr.id(); + cali_id_t r_a_id = region_attr.id(); std::string pathstr; - + for (const Entry& e : rec) { if (!e.is_reference()) continue; @@ -81,10 +81,11 @@ struct NestedInclusiveRegionProfile::NestedInclusiveRegionProfileImpl } }; - -NestedInclusiveRegionProfile::NestedInclusiveRegionProfile(CaliperMetadataAccessInterface& db, - const char* metric_attr_name, - const char* region_attr_name) +NestedInclusiveRegionProfile::NestedInclusiveRegionProfile( + CaliperMetadataAccessInterface& db, + const char* metric_attr_name, + const char* region_attr_name +) : mP { new NestedInclusiveRegionProfileImpl } { mP->metric_attr = db.get_attribute(metric_attr_name); @@ -93,14 +94,12 @@ NestedInclusiveRegionProfile::NestedInclusiveRegionProfile(CaliperMetadataAccess mP->region_attr = db.get_attribute(region_attr_name); } -void -NestedInclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) +void NestedInclusiveRegionProfile::operator() (CaliperMetadataAccessInterface& db, const std::vector& rec) { mP->process_record(db, rec); } -std::tuple< std::map, double, double > -NestedInclusiveRegionProfile::result() const +std::tuple, double, double> NestedInclusiveRegionProfile::result() const { return std::make_tuple(mP->reg_profile, mP->total_reg, mP->total); } diff --git a/src/reader/Preprocessor.cpp b/src/reader/Preprocessor.cpp index d2a5bd681..0d69c39af 100644 --- a/src/reader/Preprocessor.cpp +++ b/src/reader/Preprocessor.cpp @@ -24,7 +24,12 @@ using namespace cali; namespace { -Variant get_value(const CaliperMetadataAccessInterface& db, const std::string& attr_name, Attribute& attr, const EntryList& rec) +Variant get_value( + const CaliperMetadataAccessInterface& db, + const std::string& attr_name, + Attribute& attr, + const EntryList& rec +) { if (!attr) attr = db.get_attribute(attr_name); @@ -40,7 +45,8 @@ Variant get_value(const CaliperMetadataAccessInterface& db, const std::string& a return Variant(); } -class Kernel { +class Kernel +{ public: virtual void process(CaliperMetadataAccessInterface& db, EntryList& rec) = 0; @@ -54,25 +60,23 @@ class ScaledRatioKernel : public Kernel std::string m_nom_attr_name; std::string m_dnm_attr_name; - Attribute m_res_attr; - Attribute m_nom_attr; - Attribute m_dnm_attr; + Attribute m_res_attr; + Attribute m_nom_attr; + Attribute m_dnm_attr; - double m_scale; + double m_scale; ScaledRatioKernel(const std::string& def, const std::vector& args) - : m_res_attr_name(def), - m_nom_attr_name(args[0]), - m_dnm_attr_name(args[1]), - m_scale(1.0) - { - if (args.size() > 2) - m_scale = std::stod(args[2]); - } + : m_res_attr_name(def), m_nom_attr_name(args[0]), m_dnm_attr_name(args[1]), m_scale(1.0) + { + if (args.size() > 2) + m_scale = std::stod(args[2]); + } public: - void process(CaliperMetadataAccessInterface& db, EntryList& rec) { + void process(CaliperMetadataAccessInterface& db, EntryList& rec) + { Variant v_nom = get_value(db, m_nom_attr_name, m_nom_attr, rec); Variant v_dnm = get_value(db, m_dnm_attr_name, m_dnm_attr, rec); @@ -87,81 +91,77 @@ class ScaledRatioKernel : public Kernel double val = m_scale * (v_nom.to_double() / dnm); if (!m_res_attr) - m_res_attr = db.create_attribute(m_res_attr_name, CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + m_res_attr = + db.create_attribute(m_res_attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); rec.push_back(Entry(m_res_attr, Variant(val))); } - static Kernel* create(const std::string& def, const std::vector& args) { + static Kernel* create(const std::string& def, const std::vector& args) + { return new ScaledRatioKernel(def, args); } }; - class ScaleKernel : public Kernel { std::string m_res_attr_name; std::string m_tgt_attr_name; - Attribute m_res_attr; - Attribute m_tgt_attr; + Attribute m_res_attr; + Attribute m_tgt_attr; - double m_scale; + double m_scale; ScaleKernel(const std::string& def, const std::vector& args) - : m_res_attr_name(def), - m_tgt_attr_name(args[0]), - m_scale(1.0) - { - m_scale = std::stod(args[1]); - } + : m_res_attr_name(def), m_tgt_attr_name(args[0]), m_scale(1.0) + { + m_scale = std::stod(args[1]); + } public: - void process(CaliperMetadataAccessInterface& db, EntryList& rec) { + void process(CaliperMetadataAccessInterface& db, EntryList& rec) + { Variant v_tgt = get_value(db, m_tgt_attr_name, m_tgt_attr, rec); if (v_tgt.empty()) return; if (!m_res_attr) - m_res_attr = db.create_attribute(m_res_attr_name, CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + m_res_attr = + db.create_attribute(m_res_attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); rec.push_back(Entry(m_res_attr, Variant(m_scale * v_tgt.to_double()))); } - static Kernel* create(const std::string& def, const std::vector& args) { + static Kernel* create(const std::string& def, const std::vector& args) + { return new ScaleKernel(def, args); } }; - class TruncateKernel : public Kernel { std::string m_res_attr_name; std::string m_tgt_attr_name; - Attribute m_res_attr; - Attribute m_tgt_attr; + Attribute m_res_attr; + Attribute m_tgt_attr; - double m_factor; + double m_factor; TruncateKernel(const std::string& def, const std::vector& args) - : m_res_attr_name(def), - m_tgt_attr_name(args[0]), - m_factor(1.0) - { - if (args.size() > 1) - m_factor = std::stod(args[1]); - } + : m_res_attr_name(def), m_tgt_attr_name(args[0]), m_factor(1.0) + { + if (args.size() > 1) + m_factor = std::stod(args[1]); + } public: - void process(CaliperMetadataAccessInterface& db, EntryList& rec) { + void process(CaliperMetadataAccessInterface& db, EntryList& rec) + { Variant v_tgt = get_value(db, m_tgt_attr_name, m_tgt_attr, rec); if (v_tgt.empty()) @@ -173,9 +173,7 @@ class TruncateKernel : public Kernel type = CALI_TYPE_DOUBLE; if (!m_res_attr) - m_res_attr = db.create_attribute(m_res_attr_name, type, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + m_res_attr = db.create_attribute(m_res_attr_name, type, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); double val = v_tgt.to_double(); double res = val - fmod(val, m_factor); @@ -190,7 +188,8 @@ class TruncateKernel : public Kernel rec.push_back(Entry(m_res_attr, Variant(v_res))); } - static Kernel* create(const std::string& def, const std::vector& args) { + static Kernel* create(const std::string& def, const std::vector& args) + { return new TruncateKernel(def, args); } }; @@ -206,13 +205,13 @@ class FirstKernel : public Kernel public: FirstKernel(const std::string& def, const std::vector& args) - : m_res_attr_name(def), - m_tgt_attr_names(args) - { - m_tgt_attrs.assign(args.size(), Attribute()); - } + : m_res_attr_name(def), m_tgt_attr_names(args) + { + m_tgt_attrs.assign(args.size(), Attribute()); + } - void process(CaliperMetadataAccessInterface& db, EntryList& rec) { + void process(CaliperMetadataAccessInterface& db, EntryList& rec) + { for (size_t i = 0; i < m_tgt_attrs.size(); ++i) { Variant v_tgt = get_value(db, m_tgt_attr_names[i], m_tgt_attrs[i], rec); @@ -222,9 +221,7 @@ class FirstKernel : public Kernel cali_attr_type type = m_tgt_attrs[i].type(); if (!m_res_attr) - m_res_attr = db.create_attribute(m_res_attr_name, type, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + m_res_attr = db.create_attribute(m_res_attr_name, type, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); rec.push_back(Entry(m_res_attr, v_tgt)); @@ -232,10 +229,10 @@ class FirstKernel : public Kernel } } - static Kernel* create(const std::string& def, const std::vector& args) { + static Kernel* create(const std::string& def, const std::vector& args) + { return new FirstKernel(def, args); } - }; class SumKernel : public Kernel @@ -249,13 +246,13 @@ class SumKernel : public Kernel public: SumKernel(const std::string& def, const std::vector& args) - : m_res_attr_name(def), - m_tgt_attr_names(args) - { - m_tgt_attrs.assign(args.size(), Attribute()); - } + : m_res_attr_name(def), m_tgt_attr_names(args) + { + m_tgt_attrs.assign(args.size(), Attribute()); + } - void process(CaliperMetadataAccessInterface& db, EntryList& rec) { + void process(CaliperMetadataAccessInterface& db, EntryList& rec) + { Variant v_sum; for (size_t i = 0; i < m_tgt_attrs.size(); ++i) { @@ -269,18 +266,17 @@ class SumKernel : public Kernel if (!v_sum.empty()) { if (!m_res_attr) - m_res_attr = db.create_attribute(m_res_attr_name, v_sum.type(), - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + m_res_attr = + db.create_attribute(m_res_attr_name, v_sum.type(), CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); rec.push_back(Entry(m_res_attr, v_sum)); } } - static Kernel* create(const std::string& def, const std::vector& args) { + static Kernel* create(const std::string& def, const std::vector& args) + { return new SumKernel(def, args); } - }; class LeafKernel : public Kernel @@ -293,27 +289,23 @@ class LeafKernel : public Kernel public: - LeafKernel(const std::string& def) - : m_use_path(true), - m_res_attr_name(def) - { } + LeafKernel(const std::string& def) : m_use_path(true), m_res_attr_name(def) {} LeafKernel(const std::string& def, const std::string& tgt) - : m_use_path(false), - m_res_attr_name(def), - m_tgt_attr_name(tgt) - { } + : m_use_path(false), m_res_attr_name(def), m_tgt_attr_name(tgt) + {} - void process(CaliperMetadataAccessInterface& db, EntryList& rec) { + void process(CaliperMetadataAccessInterface& db, EntryList& rec) + { if (!m_res_attr) { cali_attr_type type = CALI_TYPE_STRING; - int prop = CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE; + int prop = CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE; if (!m_use_path) { m_tgt_attr = db.get_attribute(m_tgt_attr_name); if (!m_tgt_attr) return; - type = m_tgt_attr.type(); + type = m_tgt_attr.type(); prop |= m_tgt_attr.properties(); prop &= ~CALI_ATTR_NESTED; } @@ -330,7 +322,8 @@ class LeafKernel : public Kernel } } - static Kernel* create(const std::string& def, const std::vector& args) { + static Kernel* create(const std::string& def, const std::vector& args) + { if (args.empty()) return new LeafKernel(def); @@ -338,104 +331,80 @@ class LeafKernel : public Kernel } }; -enum KernelID { - ScaledRatio, - Scale, - Truncate, - First, - Sum, - Leaf -}; +enum KernelID { ScaledRatio, Scale, Truncate, First, Sum, Leaf }; -const char* sratio_args[] = { "numerator", "denominator", "scale" }; -const char* scale_args[] = { "attribute", "scale" }; -const char* first_args[] = { - "attribute0", "attribute1", "attribute2", - "attribute3", "attribute4", "attribute5", - "attribute6", "attribute7", "attribute8" -}; +const char* sratio_args[] = { "numerator", "denominator", "scale" }; +const char* scale_args[] = { "attribute", "scale" }; +const char* first_args[] = { "attribute0", "attribute1", "attribute2", "attribute3", "attribute4", + "attribute5", "attribute6", "attribute7", "attribute8" }; -const QuerySpec::FunctionSignature kernel_signatures[] = { - { KernelID::ScaledRatio, "ratio", 2, 3, sratio_args }, - { KernelID::Scale, "scale", 2, 2, scale_args }, - { KernelID::Truncate, "truncate", 1, 2, scale_args }, - { KernelID::First, "first", 1, 8, first_args }, - { KernelID::Sum, "sum", 1, 8, first_args }, - { KernelID::Leaf, "leaf", 0, 1, scale_args }, +const QuerySpec::FunctionSignature kernel_signatures[] = { { KernelID::ScaledRatio, "ratio", 2, 3, sratio_args }, + { KernelID::Scale, "scale", 2, 2, scale_args }, + { KernelID::Truncate, "truncate", 1, 2, scale_args }, + { KernelID::First, "first", 1, 8, first_args }, + { KernelID::Sum, "sum", 1, 8, first_args }, + { KernelID::Leaf, "leaf", 0, 1, scale_args }, - QuerySpec::FunctionSignatureTerminator -}; + QuerySpec::FunctionSignatureTerminator }; typedef Kernel* (*KernelCreateFn)(const std::string& def, const std::vector& args); -const KernelCreateFn kernel_create_fn[] = { - ScaledRatioKernel::create, - ScaleKernel::create, - TruncateKernel::create, - FirstKernel::create, - SumKernel::create, - LeafKernel::create -}; +const KernelCreateFn kernel_create_fn[] = { ScaledRatioKernel::create, ScaleKernel::create, TruncateKernel::create, + FirstKernel::create, SumKernel::create, LeafKernel::create }; constexpr int MAX_KERNEL_ID = 5; -} +} // namespace -struct Preprocessor::PreprocessorImpl -{ - std::vector< std::pair > kernels; +struct Preprocessor::PreprocessorImpl { + std::vector> kernels; - void configure(const QuerySpec& spec) { - for (const auto &pspec : spec.preprocess_ops) { + void configure(const QuerySpec& spec) + { + for (const auto& pspec : spec.preprocess_ops) { int index = pspec.op.op.id; if (index >= 0 && index <= MAX_KERNEL_ID) { - kernels.push_back( - std::make_pair( - RecordSelector(pspec.cond), - (*::kernel_create_fn[index])(pspec.target, pspec.op.args) - ) - ); + kernels.push_back(std::make_pair( + RecordSelector(pspec.cond), + (*::kernel_create_fn[index])(pspec.target, pspec.op.args) + )); } } } - EntryList process(CaliperMetadataAccessInterface& db, const EntryList& rec) { + EntryList process(CaliperMetadataAccessInterface& db, const EntryList& rec) + { EntryList ret = rec; - for (auto &k : kernels) + for (auto& k : kernels) if (k.first.pass(db, ret)) k.second->process(db, ret); return ret; } - PreprocessorImpl(const QuerySpec& spec) { - configure(spec); - } + PreprocessorImpl(const QuerySpec& spec) { configure(spec); } - ~PreprocessorImpl() { - for (auto &k : kernels) + ~PreprocessorImpl() + { + for (auto& k : kernels) delete k.second; } }; - -Preprocessor::Preprocessor(const QuerySpec& spec) - : mP( new PreprocessorImpl(spec) ) -{ } +Preprocessor::Preprocessor(const QuerySpec& spec) : mP(new PreprocessorImpl(spec)) +{} Preprocessor::~Preprocessor() -{ } +{} -EntryList -Preprocessor::process(CaliperMetadataAccessInterface& db, const EntryList& rec) +EntryList Preprocessor::process(CaliperMetadataAccessInterface& db, const EntryList& rec) { return mP->process(db, rec); } -const QuerySpec::FunctionSignature* -Preprocessor::preprocess_defs() +const QuerySpec::FunctionSignature* Preprocessor::preprocess_defs() { return ::kernel_signatures; } diff --git a/src/reader/QueryProcessor.cpp b/src/reader/QueryProcessor.cpp index 6ce37094d..c02347083 100644 --- a/src/reader/QueryProcessor.cpp +++ b/src/reader/QueryProcessor.cpp @@ -12,17 +12,16 @@ using namespace cali; -struct QueryProcessor::QueryProcessorImpl -{ - Aggregator aggregator; - Preprocessor preprocessor; - RecordSelector filter; - FormatProcessor formatter; +struct QueryProcessor::QueryProcessorImpl { + Aggregator aggregator; + Preprocessor preprocessor; + RecordSelector filter; + FormatProcessor formatter; - bool do_aggregate; + bool do_aggregate; - void - process_record(CaliperMetadataAccessInterface& db, const EntryList& in_rec) { + void process_record(CaliperMetadataAccessInterface& db, const EntryList& in_rec) + { auto rec = preprocessor.process(db, in_rec); if (filter.pass(db, rec)) { @@ -33,41 +32,34 @@ struct QueryProcessor::QueryProcessorImpl } } - void - flush(CaliperMetadataAccessInterface& db) { + void flush(CaliperMetadataAccessInterface& db) + { aggregator.flush(db, formatter); formatter.flush(db); } QueryProcessorImpl(const QuerySpec& spec, OutputStream& stream) - : aggregator(spec), - preprocessor(spec), - filter(spec), - formatter(spec, stream) + : aggregator(spec), preprocessor(spec), filter(spec), formatter(spec, stream) { do_aggregate = (spec.aggregate.selection != QuerySpec::AggregationSelection::None); } }; - -QueryProcessor::QueryProcessor(const QuerySpec& spec, OutputStream& stream) - : mP(new QueryProcessorImpl(spec, stream)) -{ } +QueryProcessor::QueryProcessor(const QuerySpec& spec, OutputStream& stream) : mP(new QueryProcessorImpl(spec, stream)) +{} QueryProcessor::~QueryProcessor() { mP.reset(); } -void -QueryProcessor::process_record(CaliperMetadataAccessInterface& db, const EntryList& rec) +void QueryProcessor::process_record(CaliperMetadataAccessInterface& db, const EntryList& rec) { mP->process_record(db, rec); } -void -QueryProcessor::flush(CaliperMetadataAccessInterface& db) +void QueryProcessor::flush(CaliperMetadataAccessInterface& db) { mP->flush(db); } diff --git a/src/reader/RecordSelector.cpp b/src/reader/RecordSelector.cpp index 5460c928c..fe8f52b3c 100644 --- a/src/reader/RecordSelector.cpp +++ b/src/reader/RecordSelector.cpp @@ -21,8 +21,7 @@ using namespace cali; namespace { -QuerySpec::Condition -parse_clause(const std::string& str) +QuerySpec::Condition parse_clause(const std::string& str) { QuerySpec::Condition clause; @@ -31,8 +30,8 @@ parse_clause(const std::string& str) if (str.empty()) return clause; - std::string::size_type spos = 0; - bool negate = false; + std::string::size_type spos = 0; + bool negate = false; if (str[spos] == '-') { ++spos; @@ -41,15 +40,17 @@ parse_clause(const std::string& str) std::string::size_type opos = str.find_first_of("<>=", spos); - clause.attr_name.assign(str, spos, opos < std::string::npos ? opos-spos : opos); + clause.attr_name.assign(str, spos, opos < std::string::npos ? opos - spos : opos); - if (opos < str.size()-1) { - clause.value.assign(str, opos+1, std::string::npos); + if (opos < str.size() - 1) { + clause.value.assign(str, opos + 1, std::string::npos); - struct ops_t { char c; QuerySpec::Condition::Op op; } const ops[] = { - // { '<', Op::Less }, { '>', Op::Greater }, - { '=', QuerySpec::Condition::Op::Equal }, - { 0, QuerySpec::Condition::Op::Exist } + struct ops_t { + char c; + QuerySpec::Condition::Op op; + } const ops[] = { // { '<', Op::Less }, { '>', Op::Greater }, + { '=', QuerySpec::Condition::Op::Equal }, + { 0, QuerySpec::Condition::Op::Exist } }; int i; @@ -81,35 +82,37 @@ parse_clause(const std::string& str) return clause; } -} +} // namespace using namespace cali; -struct RecordSelector::RecordSelectorImpl -{ +struct RecordSelector::RecordSelectorImpl { std::vector m_filters; struct Clause { QuerySpec::Condition::Op op; - Attribute attr; - Variant value; + Attribute attr; + Variant value; }; - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { m_filters.clear(); if (spec.filter.selection == QuerySpec::FilterSelection::List) m_filters = spec.filter.list; } - void configure(const QuerySpec::Condition& cond) { + void configure(const QuerySpec::Condition& cond) + { m_filters.clear(); if (cond.op != QuerySpec::Condition::None) m_filters.push_back(cond); } - Clause make_clause(const CaliperMetadataAccessInterface& db, const QuerySpec::Condition& f) { + Clause make_clause(const CaliperMetadataAccessInterface& db, const QuerySpec::Condition& f) + { Clause clause { f.op, db.get_attribute(f.attr_name), Variant() }; if (clause.attr) @@ -118,20 +121,22 @@ struct RecordSelector::RecordSelectorImpl return clause; } - template - bool have_match(const Entry& entry, Op match) { + template + bool have_match(const Entry& entry, Op match) + { if (entry.is_reference()) { - for (const Node* node = entry.node() ; node && node->id() != CALI_INV_ID; node = node->parent()) + for (const Node* node = entry.node(); node && node->id() != CALI_INV_ID; node = node->parent()) if (match(node->attribute(), node->data())) return true; - } else if (match(entry.attribute(), entry.value())) + } else if (match(entry.attribute(), entry.value())) return true; return false; } - bool pass(const CaliperMetadataAccessInterface& db, const EntryList& list) { - for (auto &f : m_filters) { + bool pass(const CaliperMetadataAccessInterface& db, const EntryList& list) + { + for (auto& f : m_filters) { Clause clause = make_clause(db, f); switch (clause.op) { @@ -140,9 +145,9 @@ struct RecordSelector::RecordSelectorImpl bool m = false; for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant&){ - return attr_id == clause.attr.id(); - })) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant&) { + return attr_id == clause.attr.id(); + })) m = true; if (!m) @@ -151,9 +156,9 @@ struct RecordSelector::RecordSelectorImpl break; case QuerySpec::Condition::Op::NotExist: for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant&){ - return attr_id == clause.attr.id(); - })) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant&) { + return attr_id == clause.attr.id(); + })) return false; break; case QuerySpec::Condition::Op::Equal: @@ -161,9 +166,9 @@ struct RecordSelector::RecordSelectorImpl bool m = false; for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val){ - return attr_id == clause.attr.id() && val == clause.value; - })) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val) { + return attr_id == clause.attr.id() && val == clause.value; + })) m = true; if (!m) @@ -172,67 +177,67 @@ struct RecordSelector::RecordSelectorImpl break; case QuerySpec::Condition::Op::NotEqual: for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val){ - return attr_id == clause.attr.id() && val == clause.value; - })) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val) { + return attr_id == clause.attr.id() && val == clause.value; + })) return false; break; case QuerySpec::Condition::Op::LessThan: - { - bool m = false; + { + bool m = false; - for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val){ + for (const Entry& e : list) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val) { return attr_id == clause.attr.id() && val < clause.value; })) - m = true; + m = true; - if (!m) - return false; - } - break; + if (!m) + return false; + } + break; case QuerySpec::Condition::Op::GreaterThan: - { - bool m = false; + { + bool m = false; - for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val){ + for (const Entry& e : list) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val) { return attr_id == clause.attr.id() && val > clause.value; })) - m = true; + m = true; - if (!m) - return false; - } - break; + if (!m) + return false; + } + break; case QuerySpec::Condition::Op::LessOrEqual: - { - bool m = false; + { + bool m = false; - for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val){ + for (const Entry& e : list) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val) { return attr_id == clause.attr.id() && (val < clause.value || val == clause.value); })) - m = true; + m = true; - if (!m) - return false; - } - break; + if (!m) + return false; + } + break; case QuerySpec::Condition::Op::GreaterOrEqual: - { - bool m = false; + { + bool m = false; - for (const Entry& e : list) - if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val){ + for (const Entry& e : list) + if (have_match(e, [&clause](cali_id_t attr_id, const Variant& val) { return attr_id == clause.attr.id() && (val > clause.value || val == clause.value); })) - m = true; + m = true; - if (!m) - return false; - } - break; + if (!m) + return false; + } + break; case QuerySpec::Condition::Op::None: break; } @@ -242,21 +247,17 @@ struct RecordSelector::RecordSelectorImpl } }; // RecordSelectorImpl - -RecordSelector::RecordSelector(const std::string& filter_string) - : mP { new RecordSelectorImpl } +RecordSelector::RecordSelector(const std::string& filter_string) : mP { new RecordSelectorImpl } { mP->m_filters = parse(filter_string); } -RecordSelector::RecordSelector(const QuerySpec& spec) - : mP { new RecordSelectorImpl } +RecordSelector::RecordSelector(const QuerySpec& spec) : mP { new RecordSelectorImpl } { mP->configure(spec); } -RecordSelector::RecordSelector(const QuerySpec::Condition& cond) - : mP { new RecordSelectorImpl } +RecordSelector::RecordSelector(const QuerySpec::Condition& cond) : mP { new RecordSelectorImpl } { mP->configure(cond); } @@ -266,24 +267,22 @@ RecordSelector::~RecordSelector() mP.reset(); } -bool -RecordSelector::pass(const CaliperMetadataAccessInterface& db, const EntryList& list) +bool RecordSelector::pass(const CaliperMetadataAccessInterface& db, const EntryList& list) { return mP->pass(db, list); } -void -RecordSelector::operator()(CaliperMetadataAccessInterface& db, const EntryList& list, SnapshotProcessFn push) const +void RecordSelector::operator() (CaliperMetadataAccessInterface& db, const EntryList& list, SnapshotProcessFn push) + const { if (mP->pass(db, list)) push(db, list); } -std::vector -RecordSelector::parse(const std::string& str) +std::vector RecordSelector::parse(const std::string& str) { std::vector clauses; - std::vector clause_strings; + std::vector clause_strings; util::split(str, ',', std::back_inserter(clause_strings)); diff --git a/src/reader/SnapshotTableFormatter.cpp b/src/reader/SnapshotTableFormatter.cpp index 425d101eb..e7f326798 100644 --- a/src/reader/SnapshotTableFormatter.cpp +++ b/src/reader/SnapshotTableFormatter.cpp @@ -12,24 +12,23 @@ using namespace cali; namespace { -struct EntryInfo { +struct EntryInfo { std::string key; std::string val; - bool align_right; + bool align_right; }; struct RecordInfo { std::vector entries; - size_t max_key_len; - size_t max_right_len; + size_t max_key_len; + size_t max_right_len; - void add(const CaliperMetadataAccessInterface& db, cali_id_t attr_id, const Variant& data) { + void add(const CaliperMetadataAccessInterface& db, cali_id_t attr_id, const Variant& data) + { Attribute attr = db.get_attribute(attr_id); bool align_right = - attr.type() == CALI_TYPE_DOUBLE || - attr.type() == CALI_TYPE_INT || - attr.type() == CALI_TYPE_UINT; + attr.type() == CALI_TYPE_DOUBLE || attr.type() == CALI_TYPE_INT || attr.type() == CALI_TYPE_UINT; EntryInfo info { attr.name(), data.to_string(), align_right }; @@ -41,9 +40,7 @@ struct RecordInfo { entries.push_back(info); } - RecordInfo() - : max_key_len { 0 }, max_right_len { 0 } - { } + RecordInfo() : max_key_len { 0 }, max_right_len { 0 } {} }; RecordInfo unpack_record(CaliperMetadataAccessInterface& db, const std::vector& rec) @@ -87,14 +84,18 @@ std::ostream& write_record_data(const RecordInfo& info, std::ostream& os) return os << "\n"; } -} // namespace anonymous +} // namespace namespace cali { -std::ostream& format_record_as_table(CaliperMetadataAccessInterface& db, const std::vector& rec, std::ostream& os) +std::ostream& format_record_as_table( + CaliperMetadataAccessInterface& db, + const std::vector& rec, + std::ostream& os +) { return ::write_record_data(::unpack_record(db, rec), os); } -} +} // namespace cali diff --git a/src/reader/SnapshotTableFormatter.h b/src/reader/SnapshotTableFormatter.h index 3544778e9..6d5782ced 100644 --- a/src/reader/SnapshotTableFormatter.h +++ b/src/reader/SnapshotTableFormatter.h @@ -16,8 +16,7 @@ namespace cali class CaliperMetadataAccessInterface; -std::ostream& -format_record_as_table(CaliperMetadataAccessInterface& db, const std::vector&, std::ostream& os); +std::ostream& format_record_as_table(CaliperMetadataAccessInterface& db, const std::vector&, std::ostream& os); } // namespace cali diff --git a/src/reader/SnapshotTree.cpp b/src/reader/SnapshotTree.cpp index f32fdbbcf..f82330e7e 100644 --- a/src/reader/SnapshotTree.cpp +++ b/src/reader/SnapshotTree.cpp @@ -14,8 +14,7 @@ using namespace cali; -Variant -SnapshotTreeNode::min_val(const Attribute& key) +Variant SnapshotTreeNode::min_val(const Attribute& key) { { auto it = m_v_min.find(key); @@ -26,9 +25,9 @@ SnapshotTreeNode::min_val(const Attribute& key) Variant val; for (const auto& rec : m_records) { - auto it = std::find_if(rec.begin(), rec.end(), [key](const std::pair& p){ - return p.first == key; - }); + auto it = std::find_if(rec.begin(), rec.end(), [key](const std::pair& p) { + return p.first == key; + }); if (it != rec.end()) val = val ? std::min(val, (*it).second) : (*it).second; @@ -41,8 +40,7 @@ SnapshotTreeNode::min_val(const Attribute& key) return val; } -Variant -SnapshotTreeNode::max_val(const Attribute& key) +Variant SnapshotTreeNode::max_val(const Attribute& key) { { auto it = m_v_max.find(key); @@ -53,9 +51,9 @@ SnapshotTreeNode::max_val(const Attribute& key) Variant val; for (const auto& rec : m_records) { - auto it = std::find_if(rec.begin(), rec.end(), [key](const std::pair& p){ - return p.first == key; - }); + auto it = std::find_if(rec.begin(), rec.end(), [key](const std::pair& p) { + return p.first == key; + }); if (it != rec.end()) val = val ? std::max(val, (*it).second) : (*it).second; @@ -68,31 +66,30 @@ SnapshotTreeNode::max_val(const Attribute& key) return val; } -void -SnapshotTreeNode::sort(const Attribute& key, bool ascending) +void SnapshotTreeNode::sort(const Attribute& key, bool ascending) { - std::stable_sort(m_records.begin(), m_records.end(), [key,ascending](const Record& lhs, const Record& rhs){ - auto find_key = [key](const std::pair& p){ - return p.first == key; - }; + std::stable_sort(m_records.begin(), m_records.end(), [key, ascending](const Record& lhs, const Record& rhs) { + auto find_key = [key](const std::pair& p) { + return p.first == key; + }; - auto l_it = std::find_if(lhs.begin(), lhs.end(), find_key); - auto r_it = std::find_if(rhs.begin(), rhs.end(), find_key); + auto l_it = std::find_if(lhs.begin(), lhs.end(), find_key); + auto r_it = std::find_if(rhs.begin(), rhs.end(), find_key); - if (r_it == rhs.end()) - return true; - if (l_it == lhs.end()) - return false; + if (r_it == rhs.end()) + return true; + if (l_it == lhs.end()) + return false; - return ascending ? l_it->second < r_it->second : l_it->second > r_it->second; - }); + return ascending ? l_it->second < r_it->second : l_it->second > r_it->second; + }); } -struct SnapshotTree::SnapshotTreeImpl -{ +struct SnapshotTree::SnapshotTreeImpl { SnapshotTreeNode* m_root; - void recursive_delete(SnapshotTreeNode* node) { + void recursive_delete(SnapshotTreeNode* node) + { if (node) { recursive_delete(node->first_child()); recursive_delete(node->next_sibling()); @@ -101,18 +98,19 @@ struct SnapshotTree::SnapshotTreeImpl } } - const SnapshotTreeNode* - add_snapshot(const CaliperMetadataAccessInterface& db, - const EntryList& list, - IsPathPredicateFn is_path) + const SnapshotTreeNode* add_snapshot( + const CaliperMetadataAccessInterface& db, + const EntryList& list, + IsPathPredicateFn is_path + ) { - std::vector< std::pair > path; - std::vector< std::pair > data; + std::vector> path; + std::vector> data; // // helper function to distinguish path and attribute entries // - auto push_entry = [&](cali_id_t attr_id, const Variant& val){ + auto push_entry = [&](cali_id_t attr_id, const Variant& val) { Attribute attr = db.get_attribute(attr_id); if (!attr) @@ -121,9 +119,9 @@ struct SnapshotTree::SnapshotTreeImpl if (is_path(attr, val)) path.push_back(std::make_pair(attr, val)); else { - auto it = std::find_if(data.begin(), data.end(), [&attr](const std::pair& p){ - return attr == p.first; - }); + auto it = std::find_if(data.begin(), data.end(), [&attr](const std::pair& p) { + return attr == p.first; + }); if (it == data.end()) data.push_back(std::make_pair(attr, val)); @@ -154,7 +152,7 @@ struct SnapshotTree::SnapshotTreeImpl for (auto it = path.rbegin(); it != path.rend(); ++it) { SnapshotTreeNode* child = node->first_child(); - for ( ; child && !child->label_equals((*it).first, (*it).second); child = child->next_sibling()) + for (; child && !child->label_equals((*it).first, (*it).second); child = child->next_sibling()) ; if (!child) { @@ -176,40 +174,33 @@ struct SnapshotTree::SnapshotTreeImpl return node; } - SnapshotTreeImpl(const Attribute& attr, const Variant& value) - : m_root(new SnapshotTreeNode(attr, value)) - { } + SnapshotTreeImpl(const Attribute& attr, const Variant& value) : m_root(new SnapshotTreeNode(attr, value)) {} - ~SnapshotTreeImpl() { - recursive_delete(m_root); - } + ~SnapshotTreeImpl() { recursive_delete(m_root); } }; // SnapshotTreeImpl +SnapshotTree::SnapshotTree() : mP(new SnapshotTreeImpl(Attribute(), Variant())) +{} -SnapshotTree::SnapshotTree() - : mP(new SnapshotTreeImpl(Attribute(), Variant())) -{ } - -SnapshotTree::SnapshotTree(const Attribute& attr, const Variant& value) - : mP(new SnapshotTreeImpl(attr, value)) -{ } +SnapshotTree::SnapshotTree(const Attribute& attr, const Variant& value) : mP(new SnapshotTreeImpl(attr, value)) +{} SnapshotTree::~SnapshotTree() { mP.reset(); } -const SnapshotTreeNode* -SnapshotTree::add_snapshot(const CaliperMetadataAccessInterface& db, - const EntryList& list, - IsPathPredicateFn is_path) +const SnapshotTreeNode* SnapshotTree::add_snapshot( + const CaliperMetadataAccessInterface& db, + const EntryList& list, + IsPathPredicateFn is_path +) { return mP->add_snapshot(db, list, is_path); } -SnapshotTreeNode* -SnapshotTree::root() const +SnapshotTreeNode* SnapshotTree::root() const { return mP->m_root; } diff --git a/src/reader/SnapshotTree.h b/src/reader/SnapshotTree.h index e9aba1f3e..3c860accd 100644 --- a/src/reader/SnapshotTree.h +++ b/src/reader/SnapshotTree.h @@ -19,7 +19,6 @@ namespace cali class CaliperMetadataAccessInterface; - // // --- SnapshotTree class // @@ -60,57 +59,54 @@ class SnapshotTreeNode : public util::LockfreeIntrusiveTree Attribute m_label_key; Variant m_label_value; - using Record = std::vector< std::pair >; + using Record = std::vector>; std::vector m_records; std::map m_v_min; std::map m_v_max; - void add_record(const Record& rec) { - m_records.push_back(rec); - } + void add_record(const Record& rec) { m_records.push_back(rec); } SnapshotTreeNode(const Attribute& label_key, const Variant& label_val) : util::LockfreeIntrusiveTree(this, &SnapshotTreeNode::m_treenode), m_label_key(label_key), m_label_value(label_val) - { } + {} public: /// \brief Return the label attribute key. - Attribute label_key() const { return m_label_key; } + Attribute label_key() const { return m_label_key; } + /// \brief Return the label value. - Variant label_value() const { return m_label_value; } + Variant label_value() const { return m_label_value; } /// \brief Return `false` if the node represents a snapshot record, /// otherwise (i.e., if the node is empty) return `true`. - bool is_empty() const { return m_records.empty(); } + bool is_empty() const { return m_records.empty(); } /// \brief Return `true` if the label equals the given (\a key,\a value) pair. - bool label_equals(const Attribute& key, const Variant& value) const { + bool label_equals(const Attribute& key, const Variant& value) const + { return m_label_key == key && m_label_value == value; } /// \brief Access the non-path attributes of the snapshot records associated /// with this node. - const decltype(m_records)& records() const { - return m_records; - } + const decltype(m_records)& records() const { return m_records; } /// \brief Recursively find the minimum value for \a key under this node - Variant min_val(const Attribute& key); + Variant min_val(const Attribute& key); /// \brief Recursively find the maximum value for \a key under this node - Variant max_val(const Attribute& key); + Variant max_val(const Attribute& key); /// \brief sort records by \a key - void sort(const Attribute& key, bool ascending); + void sort(const Attribute& key, bool ascending); friend class SnapshotTree; }; // SnapshotTreeNode - // // --- SnapshotTree // @@ -150,7 +146,7 @@ class SnapshotTree /// A predicate to determine if a given _(attribute,value)_ pair /// in a snapshot record belongs to the tree path or not. - typedef std::function IsPathPredicateFn; + typedef std::function IsPathPredicateFn; /// \brief Add given snapshot record to the tree. /// @@ -168,14 +164,14 @@ class SnapshotTree /// (attribute,value) pair is on the path or not. /// \return Pointer to the newly created tree node. Null pointer /// if no path entry was found. - const SnapshotTreeNode* - add_snapshot(const CaliperMetadataAccessInterface& db, - const EntryList& list, - IsPathPredicateFn is_path); + const SnapshotTreeNode* add_snapshot( + const CaliperMetadataAccessInterface& db, + const EntryList& list, + IsPathPredicateFn is_path + ); /// \brief Return the snapshot tree's root node. - SnapshotTreeNode* - root() const; + SnapshotTreeNode* root() const; }; } // namespace cali diff --git a/src/reader/TableFormatter.cpp b/src/reader/TableFormatter.cpp index 076c9e6cf..f63563b76 100644 --- a/src/reader/TableFormatter.cpp +++ b/src/reader/TableFormatter.cpp @@ -23,44 +23,51 @@ using namespace cali; -struct TableFormatter::TableImpl -{ +struct TableFormatter::TableImpl { struct Column { std::string name; std::string display_name; std::size_t width; - Attribute attr; + Attribute attr; - bool print; // used for hidden sort columns + bool print; // used for hidden sort columns QuerySpec::SortSpec::Order sort_order; - Column(const std::string& n, const std::string alias, std::size_t w, const Attribute& a, bool p, - QuerySpec::SortSpec::Order o = QuerySpec::SortSpec::Order::None) + Column( + const std::string& n, + const std::string alias, + std::size_t w, + const Attribute& a, + bool p, + QuerySpec::SortSpec::Order o = QuerySpec::SortSpec::Order::None + ) : name(n), display_name(alias), width(w), attr(a), print(p), sort_order(o) - { } + {} }; - std::vector m_cols; - std::vector< std::vector > m_rows; + std::vector m_cols; + std::vector> m_rows; - std::mutex m_col_lock; - std::mutex m_row_lock; + std::mutex m_col_lock; + std::mutex m_row_lock; - std::map m_aliases; + std::map m_aliases; - bool m_auto_column; + bool m_auto_column; - int m_max_column_width; + int m_max_column_width; - bool m_print_globals; + bool m_print_globals; - int column_width(int base) const { + int column_width(int base) const + { return std::max(m_max_column_width > 0 ? std::min(base, m_max_column_width) : base, 4); } - void parse(const std::string& field_string, const std::string& sort_string) { + void parse(const std::string& field_string, const std::string& sort_string) + { std::vector fields; // fill sort columns @@ -88,7 +95,8 @@ struct TableFormatter::TableImpl m_cols.emplace_back(s, s, s.size(), Attribute(), true); } - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { m_cols.clear(); m_rows.clear(); @@ -153,11 +161,11 @@ struct TableFormatter::TableImpl } } - void update_column_attribute(CaliperMetadataAccessInterface& db, cali_id_t attr_id) { - auto it = std::find_if(m_cols.begin(), m_cols.end(), - [attr_id](const Column& c) { - return c.sort_order == QuerySpec::SortSpec::None && c.attr.id() == attr_id; - }); + void update_column_attribute(CaliperMetadataAccessInterface& db, cali_id_t attr_id) + { + auto it = std::find_if(m_cols.begin(), m_cols.end(), [attr_id](const Column& c) { + return c.sort_order == QuerySpec::SortSpec::None && c.attr.id() == attr_id; + }); if (it != m_cols.end()) return; @@ -185,9 +193,9 @@ struct TableFormatter::TableImpl m_cols.emplace_back(name, alias, alias.size(), attr, true); } - std::vector update_columns(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::lock_guard - g(m_col_lock); + std::vector update_columns(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::lock_guard g(m_col_lock); // Auto-generate columns from attributes in the snapshots. Used if no // field list was given. Skips some internal attributes. @@ -210,11 +218,12 @@ struct TableFormatter::TableImpl return m_cols; } - void add(CaliperMetadataAccessInterface& db, const EntryList& list) { - std::vector cols = update_columns(db, list); + void add(CaliperMetadataAccessInterface& db, const EntryList& list) + { + std::vector cols = update_columns(db, list); std::vector row(cols.size()); - bool active = false; + bool active = false; bool update_max_width = false; for (std::vector::size_type c = 0; c < cols.size(); ++c) { @@ -255,15 +264,13 @@ struct TableFormatter::TableImpl } if (active) { - std::lock_guard - g(m_row_lock); + std::lock_guard g(m_row_lock); m_rows.push_back(std::move(row)); } if (update_max_width) { - std::lock_guard - g(m_col_lock); + std::lock_guard g(m_col_lock); for (std::vector::size_type c = 0; c < cols.size(); ++c) if (cols[c].width > m_cols[c].width) @@ -271,7 +278,8 @@ struct TableFormatter::TableImpl } } - void flush(std::ostream& os) { + void flush(std::ostream& os) + { // NOTE: No locking, assume flush() runs serially // sort rows @@ -279,21 +287,27 @@ struct TableFormatter::TableImpl for (std::vector::size_type c = 0; c < m_cols.size(); ++c) if (m_cols[c].sort_order == QuerySpec::SortSpec::Order::Ascending) - std::stable_sort(m_rows.begin(), m_rows.end(), - [c,this](const std::vector& lhs, const std::vector& rhs){ - if (c >= lhs.size() || c >= rhs.size()) - return lhs.size() < rhs.size(); - cali_attr_type type = this->m_cols[c].attr.type(); - return Variant::from_string(type, lhs[c].c_str()) < Variant::from_string(type, rhs[c].c_str()); - }); + std::stable_sort( + m_rows.begin(), + m_rows.end(), + [c, this](const std::vector& lhs, const std::vector& rhs) { + if (c >= lhs.size() || c >= rhs.size()) + return lhs.size() < rhs.size(); + cali_attr_type type = this->m_cols[c].attr.type(); + return Variant::from_string(type, lhs[c].c_str()) < Variant::from_string(type, rhs[c].c_str()); + } + ); else if (m_cols[c].sort_order == QuerySpec::SortSpec::Order::Descending) - std::stable_sort(m_rows.begin(), m_rows.end(), - [c,this](const std::vector& lhs, const std::vector& rhs){ - if (c >= lhs.size() || c >= rhs.size()) - return lhs.size() > rhs.size(); - cali_attr_type type = this->m_cols[c].attr.type(); - return Variant::from_string(type, lhs[c].c_str()) > Variant::from_string(type, rhs[c].c_str()); - }); + std::stable_sort( + m_rows.begin(), + m_rows.end(), + [c, this](const std::vector& lhs, const std::vector& rhs) { + if (c >= lhs.size() || c >= rhs.size()) + return lhs.size() > rhs.size(); + cali_attr_type type = this->m_cols[c].attr.type(); + return Variant::from_string(type, lhs[c].c_str()) > Variant::from_string(type, rhs[c].c_str()); + } + ); // print header @@ -312,15 +326,13 @@ struct TableFormatter::TableImpl if (!m_cols[c].print) continue; - int width = column_width(m_cols[c].width); - std::string str = util::clamp_string(row[c], width); - cali_attr_type t = m_cols[c].attr.type(); - bool align_right = (t == CALI_TYPE_INT || - t == CALI_TYPE_UINT || - t == CALI_TYPE_DOUBLE); + int width = column_width(m_cols[c].width); + std::string str = util::clamp_string(row[c], width); + cali_attr_type t = m_cols[c].attr.type(); + bool align_right = (t == CALI_TYPE_INT || t == CALI_TYPE_UINT || t == CALI_TYPE_DOUBLE); if (align_right) - util::pad_left (os, str, width); + util::pad_left(os, str, width); else util::pad_right(os, str, width); } @@ -329,19 +341,15 @@ struct TableFormatter::TableImpl } } - TableImpl() - : m_max_column_width(60), m_print_globals(false) - { } + TableImpl() : m_max_column_width(60), m_print_globals(false) {} }; -TableFormatter::TableFormatter(const std::string& fields, const std::string& sort_fields) - : mP { new TableImpl } +TableFormatter::TableFormatter(const std::string& fields, const std::string& sort_fields) : mP { new TableImpl } { mP->parse(fields, sort_fields); } -TableFormatter::TableFormatter(const QuerySpec& spec) - : mP { new TableImpl } +TableFormatter::TableFormatter(const QuerySpec& spec) : mP { new TableImpl } { mP->configure(spec); } @@ -351,14 +359,12 @@ TableFormatter::~TableFormatter() mP.reset(); } -void -TableFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) +void TableFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->add(db, list); } -void -TableFormatter::flush(CaliperMetadataAccessInterface& db, std::ostream& os) +void TableFormatter::flush(CaliperMetadataAccessInterface& db, std::ostream& os) { if (mP->m_print_globals) format_record_as_table(db, db.get_globals(), os); diff --git a/src/reader/TableFormatter.h b/src/reader/TableFormatter.h index 8dce49089..6d58ad58b 100644 --- a/src/reader/TableFormatter.h +++ b/src/reader/TableFormatter.h @@ -20,7 +20,7 @@ struct QuerySpec; /// \brief Print a set of snapshot records in a human-readable table /// \ingroup ReaderAPI - + class TableFormatter : public Formatter { struct TableImpl; diff --git a/src/reader/TreeFormatter.cpp b/src/reader/TreeFormatter.cpp index bc67e1e8e..a62096497 100644 --- a/src/reader/TreeFormatter.cpp +++ b/src/reader/TreeFormatter.cpp @@ -28,45 +28,44 @@ using namespace cali; - -struct TreeFormatter::TreeFormatterImpl -{ - SnapshotTree m_tree; - QuerySpec m_spec; +struct TreeFormatter::TreeFormatterImpl { + SnapshotTree m_tree; + QuerySpec m_spec; struct ColumnInfo { std::string display_name; - int width; + int width; }; struct SortInfo { - Attribute attribute; + Attribute attribute; QuerySpec::SortSpec::Order order; }; std::map m_column_info; - int m_path_column_width; - int m_max_column_width; + int m_path_column_width; + int m_max_column_width; std::map m_aliases; - bool m_use_nested; - bool m_print_globals; + bool m_use_nested; + bool m_print_globals; std::vector m_path_key_names; std::vector m_path_keys; - std::vector m_sort_info; - - std::mutex m_path_key_lock; + std::vector m_sort_info; + std::mutex m_path_key_lock; - int column_width(int base) const { + int column_width(int base) const + { return std::max(m_max_column_width > 0 ? std::min(base, m_max_column_width) : base, 4); } - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { // set path keys (first argument in spec.format.args) { @@ -84,8 +83,7 @@ struct TreeFormatter::TreeFormatterImpl m_max_column_width = StringConverter(it->second).to_int(&ok); if (!ok) { - Log(0).stream() << "TreeFormatter: invalid column width argument \"" - << it->second << "\"" + Log(0).stream() << "TreeFormatter: invalid column width argument \"" << it->second << "\"" << std::endl; m_max_column_width = -1; @@ -101,8 +99,7 @@ struct TreeFormatter::TreeFormatterImpl { for (const auto path_str : { "path", "prop:nested" }) { - auto it = std::find(m_path_key_names.begin(), m_path_key_names.end(), - path_str); + auto it = std::find(m_path_key_names.begin(), m_path_key_names.end(), path_str); if (it != m_path_key_names.end()) { m_use_nested = true; @@ -117,12 +114,12 @@ struct TreeFormatter::TreeFormatterImpl m_path_keys.assign(m_path_key_names.size(), Attribute()); } - std::vector get_path_keys(const CaliperMetadataAccessInterface& db) { + std::vector get_path_keys(const CaliperMetadataAccessInterface& db) + { std::vector path_keys; { - std::lock_guard - g(m_path_key_lock); + std::lock_guard g(m_path_key_lock); path_keys = m_path_keys; } @@ -132,9 +129,8 @@ struct TreeFormatter::TreeFormatterImpl Attribute attr = db.get_attribute(m_path_key_names[i]); if (attr) { - path_keys[i] = attr; - std::lock_guard - g(m_path_key_lock); + path_keys[i] = attr; + std::lock_guard g(m_path_key_lock); m_path_keys[i] = attr; } } @@ -142,20 +138,19 @@ struct TreeFormatter::TreeFormatterImpl return path_keys; } - void add(const CaliperMetadataAccessInterface& db, const EntryList& list) { + void add(const CaliperMetadataAccessInterface& db, const EntryList& list) + { const SnapshotTreeNode* node = nullptr; if (m_use_nested) { - node = m_tree.add_snapshot(db, list, [](const Attribute& attr,const Variant&){ - return attr.is_nested(); - }); + node = + m_tree.add_snapshot(db, list, [](const Attribute& attr, const Variant&) { return attr.is_nested(); }); } else { auto path_keys = get_path_keys(db); - node = m_tree.add_snapshot(db, list, [&path_keys](const Attribute& attr, const Variant&){ - return (std::find(std::begin(path_keys), std::end(path_keys), - attr) != std::end(path_keys)); - }); + node = m_tree.add_snapshot(db, list, [&path_keys](const Attribute& attr, const Variant&) { + return (std::find(std::begin(path_keys), std::end(path_keys), attr) != std::end(path_keys)); + }); } if (!node) @@ -163,10 +158,10 @@ struct TreeFormatter::TreeFormatterImpl // update column widths - for (auto &data : node->records()) { - for (auto &entry : data) { - int len = entry.second.to_string().size(); - auto it = m_column_info.find(entry.first); + for (auto& data : node->records()) { + for (auto& entry : data) { + int len = entry.second.to_string().size(); + auto it = m_column_info.find(entry.first); if (it == m_column_info.end()) { std::string name = entry.first.name(); @@ -190,19 +185,19 @@ struct TreeFormatter::TreeFormatterImpl } } - void - init_sort_info(const CaliperMetadataAccessInterface& db) { + void init_sort_info(const CaliperMetadataAccessInterface& db) + { m_sort_info.clear(); - for (const auto &s : m_spec.sort.list) { + for (const auto& s : m_spec.sort.list) { Attribute attr = db.get_attribute(s.attribute); if (attr) m_sort_info.push_back({ attr, s.order }); } } - std::vector - get_sorted_child_nodes(SnapshotTreeNode* parent) { + std::vector get_sorted_child_nodes(SnapshotTreeNode* parent) + { std::vector nodes; if (!parent) @@ -211,28 +206,30 @@ struct TreeFormatter::TreeFormatterImpl for (SnapshotTreeNode* node = parent->first_child(); node; node = node->next_sibling()) nodes.push_back(node); - for (const auto &si : m_sort_info) { + for (const auto& si : m_sort_info) { for (SnapshotTreeNode* node : nodes) node->sort(si.attribute, si.order == QuerySpec::SortSpec::Order::Ascending); if (si.order == QuerySpec::SortSpec::Order::Ascending) { - std::stable_sort(nodes.begin(), nodes.end(), [si](SnapshotTreeNode* lhs, SnapshotTreeNode* rhs){ - return lhs->min_val(si.attribute) < rhs->min_val(si.attribute); - }); + std::stable_sort(nodes.begin(), nodes.end(), [si](SnapshotTreeNode* lhs, SnapshotTreeNode* rhs) { + return lhs->min_val(si.attribute) < rhs->min_val(si.attribute); + }); } else if (si.order == QuerySpec::SortSpec::Order::Descending) { - std::stable_sort(nodes.begin(), nodes.end(), [si](SnapshotTreeNode* lhs, SnapshotTreeNode* rhs){ - return lhs->max_val(si.attribute) > rhs->max_val(si.attribute); - }); + std::stable_sort(nodes.begin(), nodes.end(), [si](SnapshotTreeNode* lhs, SnapshotTreeNode* rhs) { + return lhs->max_val(si.attribute) > rhs->max_val(si.attribute); + }); } } return nodes; } - void recursive_print_nodes(SnapshotTreeNode* node, - int level, - const std::vector& attributes, - std::ostream& os) + void recursive_print_nodes( + SnapshotTreeNode* node, + int level, + const std::vector& attributes, + std::ostream& os + ) { // // print this node @@ -243,12 +240,12 @@ struct TreeFormatter::TreeFormatterImpl int width = column_width(m_path_column_width); std::string path_str; - path_str.append(std::min(2*level, width-2), ' '); + path_str.append(std::min(2 * level, width - 2), ' '); - if (2*level >= width) + if (2 * level >= width) path_str.append(".."); else - path_str.append(util::clamp_string(node->label_value().to_string(), width-2*level)); + path_str.append(util::clamp_string(node->label_value().to_string(), width - 2 * level)); util::pad_right(os, path_str, width); } @@ -261,9 +258,9 @@ struct TreeFormatter::TreeFormatterImpl int width = column_width(m_path_column_width); std::string path_str; - path_str.append(std::min(2*level, width-2), ' '); + path_str.append(std::min(2 * level, width - 2), ' '); - if (2*level >= width) + if (2 * level >= width) path_str.append(".."); else path_str.append(" |-"); @@ -278,21 +275,19 @@ struct TreeFormatter::TreeFormatterImpl { auto it = std::find_if(rec.begin(), rec.end(), [&a](const std::pair& p) { - return a == p.first; - }); + return a == p.first; + }); if (it != rec.end()) str = util::clamp_string(it->second.to_string(), width); } cali_attr_type t = a.type(); - bool align_right = (t == CALI_TYPE_INT || - t == CALI_TYPE_UINT || - t == CALI_TYPE_DOUBLE || - t == CALI_TYPE_ADDR); + bool align_right = + (t == CALI_TYPE_INT || t == CALI_TYPE_UINT || t == CALI_TYPE_DOUBLE || t == CALI_TYPE_ADDR); if (align_right) - util::pad_left (os, str, width); + util::pad_left(os, str, width); else util::pad_right(os, str, width); } @@ -305,10 +300,11 @@ struct TreeFormatter::TreeFormatterImpl // for (auto child : get_sorted_child_nodes(node)) - recursive_print_nodes(child, level+1, attributes, os); + recursive_print_nodes(child, level + 1, attributes, os); } - int recursive_max_path_label_width(const SnapshotTreeNode* node, int level) { + int recursive_max_path_label_width(const SnapshotTreeNode* node, int level) + { int len = 2 * level + node->label_value().to_string().length(); for (node = node->first_child(); node; node = node->next_sibling()) @@ -317,7 +313,8 @@ struct TreeFormatter::TreeFormatterImpl return len; } - void flush(const CaliperMetadataAccessInterface& db, std::ostream& os) { + void flush(const CaliperMetadataAccessInterface& db, std::ostream& os) + { // // establish order of attribute columns // @@ -329,7 +326,7 @@ struct TreeFormatter::TreeFormatterImpl switch (m_spec.select.selection) { case QuerySpec::AttributeSelection::Default: // auto-attributes: skip hidden and global attributes - for (auto &p : m_column_info) { + for (auto& p : m_column_info) { if (p.first.is_hidden() || p.first.is_global()) continue; @@ -337,7 +334,7 @@ struct TreeFormatter::TreeFormatterImpl } break; case QuerySpec::AttributeSelection::All: - for (auto &p : m_column_info) + for (auto& p : m_column_info) attributes.push_back(p.first); break; case QuerySpec::AttributeSelection::List: @@ -345,8 +342,7 @@ struct TreeFormatter::TreeFormatterImpl Attribute attr = db.get_attribute(s); if (!attr) - std::cerr << "cali-query: TreeFormatter: Attribute \"" << s << "\" not found." - << std::endl; + std::cerr << "cali-query: TreeFormatter: Attribute \"" << s << "\" not found." << std::endl; else attributes.push_back(attr); } @@ -395,34 +391,26 @@ struct TreeFormatter::TreeFormatterImpl } TreeFormatterImpl(const QuerySpec& spec) - : m_spec(spec), - m_path_column_width(0), - m_max_column_width(48), - m_use_nested(true), - m_print_globals(false) + : m_spec(spec), m_path_column_width(0), m_max_column_width(48), m_use_nested(true), m_print_globals(false) { configure(spec); } }; - -TreeFormatter::TreeFormatter(const QuerySpec& spec) - : mP { new TreeFormatterImpl(spec) } -{ } +TreeFormatter::TreeFormatter(const QuerySpec& spec) : mP { new TreeFormatterImpl(spec) } +{} TreeFormatter::~TreeFormatter() { mP.reset(); } -void -TreeFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) +void TreeFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->add(db, list); } -void -TreeFormatter::flush(CaliperMetadataAccessInterface& db, std::ostream& os) +void TreeFormatter::flush(CaliperMetadataAccessInterface& db, std::ostream& os) { if (mP->m_print_globals) format_record_as_table(db, db.get_globals(), os); diff --git a/src/reader/TreeFormatter.h b/src/reader/TreeFormatter.h index 25c2ac20c..3de6effdd 100644 --- a/src/reader/TreeFormatter.h +++ b/src/reader/TreeFormatter.h @@ -17,9 +17,9 @@ namespace cali class CaliperMetadataAccessInterface; struct QuerySpec; -/// \brief Print a set of snapshot records in a tree +/// \brief Print a set of snapshot records in a tree /// \ingroup ReaderAPI - + class TreeFormatter : public Formatter { struct TreeFormatterImpl; @@ -37,4 +37,3 @@ class TreeFormatter : public Formatter }; } // namespace cali - diff --git a/src/reader/UserFormatter.cpp b/src/reader/UserFormatter.cpp index f34d69ea2..3859c183d 100644 --- a/src/reader/UserFormatter.cpp +++ b/src/reader/UserFormatter.cpp @@ -25,32 +25,30 @@ using namespace cali; using namespace std; -struct UserFormatter::FormatImpl -{ +struct UserFormatter::FormatImpl { struct Field { - string prefix; + string prefix; string attr_name; Attribute attr; - size_t width; - char align; // 'l', 'r', 'c' - }; // for the frmtparse function + size_t width; + char align; // 'l', 'r', 'c' + }; // for the frmtparse function vector m_fields; // for the format strings string m_title; - std::mutex m_fields_lock; + std::mutex m_fields_lock; - OutputStream m_os; - std::mutex m_os_lock; + OutputStream m_os; + std::mutex m_os_lock; - FormatImpl(OutputStream& os) - : m_os(os) - { } + FormatImpl(OutputStream& os) : m_os(os) {} // based on SnapshotTextFormatter::parse - void parse(const string& formatstring) { + void parse(const string& formatstring) + { // parse format: "(prefix string)%[]attr_name%..." vector split_string; @@ -77,19 +75,19 @@ struct UserFormatter::FormatImpl int nfields = field_strings.size(); for (int i = 0; i < nfields; ++i) - if(field_strings[i] == "[") + if (field_strings[i] == "[") wfbegin = i; else if (field_strings[i] == "]") wfend = i; - if (wfbegin >= 0 && wfend > wfbegin+1) { + if (wfbegin >= 0 && wfend > wfbegin + 1) { // width field specified - field.width = stoi(field_strings[wfbegin+1]); + field.width = stoi(field_strings[wfbegin + 1]); if (wfbegin > 0) apos = 0; - else if (wfend+1 < nfields) - apos = wfend+1; + else if (wfend + 1 < nfields) + apos = wfend + 1; } else if (nfields > 0) apos = 0; @@ -103,21 +101,22 @@ struct UserFormatter::FormatImpl } } - void configure(const QuerySpec& spec) { + void configure(const QuerySpec& spec) + { auto it = spec.format.kwargs.find("format"); if (it != spec.format.kwargs.end()) parse(it->second); } - void print(CaliperMetadataAccessInterface& db, const EntryList& list) { + void print(CaliperMetadataAccessInterface& db, const EntryList& list) + { std::ostringstream os; for (Field f : m_fields) { Attribute attr; { - std::lock_guard - g(m_fields_lock); + std::lock_guard g(m_fields_lock); if (!f.attr) f.attr = db.get_attribute(f.attr_name); @@ -128,7 +127,7 @@ struct UserFormatter::FormatImpl string str; if (attr) - for (const Entry& e: list) { + for (const Entry& e : list) { if (e.is_reference()) { for (const Node* node = e.node(); node; node = node->parent()) if (node->attribute() == attr.id()) @@ -140,30 +139,27 @@ struct UserFormatter::FormatImpl break; } - const char whitespace[80+1] = + const char whitespace[80 + 1] = " " " "; size_t len = str.size(); size_t w = len < f.width ? std::min(f.width - len, 80) : 0; - os << f.prefix << str << (w > 0 ? whitespace+(80-w) : ""); + os << f.prefix << str << (w > 0 ? whitespace + (80 - w) : ""); } { - std::lock_guard - g(m_os_lock); + std::lock_guard g(m_os_lock); std::ostream* real_os = m_os.stream(); - *real_os << os.str() <configure(spec); @@ -181,8 +177,7 @@ UserFormatter::~UserFormatter() mP.reset(); } -void -UserFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) +void UserFormatter::process_record(CaliperMetadataAccessInterface& db, const EntryList& list) { mP->print(db, list); } diff --git a/src/reader/UserFormatter.h b/src/reader/UserFormatter.h index 970e68117..21daa1562 100644 --- a/src/reader/UserFormatter.h +++ b/src/reader/UserFormatter.h @@ -20,7 +20,7 @@ struct QuerySpec; /// \brief Prints snapshot records using a user-defined format string /// \ingroup ReaderAPI - + class UserFormatter : public Formatter { struct FormatImpl; diff --git a/src/reader/test/test_aggregator.cpp b/src/reader/test/test_aggregator.cpp index 630999dd7..65f14657e 100644 --- a/src/reader/test/test_aggregator.cpp +++ b/src/reader/test/test_aggregator.cpp @@ -11,13 +11,17 @@ using namespace cali; namespace { -QuerySpec::AggregationOp -make_op(const char* name, const char* arg1 = nullptr, const char* arg2 = nullptr, const char* arg3 = nullptr) +QuerySpec::AggregationOp make_op( + const char* name, + const char* arg1 = nullptr, + const char* arg2 = nullptr, + const char* arg3 = nullptr +) { - QuerySpec::AggregationOp op; + QuerySpec::AggregationOp op; const QuerySpec::FunctionSignature* p = Aggregator::aggregation_defs(); - for ( ; p && p->name && (0 != strcmp(p->name, name)); ++p ) + for (; p && p->name && (0 != strcmp(p->name, name)); ++p) ; if (p->name) { @@ -34,12 +38,11 @@ make_op(const char* name, const char* arg1 = nullptr, const char* arg2 = nullptr return op; } -std::map -make_dict_from_entrylist(const EntryList& list) +std::map make_dict_from_entrylist(const EntryList& list) { std::map dict; - for ( const Entry& e : list ) + for (const Entry& e : list) dict[e.attribute()] = e; return dict; @@ -47,8 +50,8 @@ make_dict_from_entrylist(const EntryList& list) } // namespace - -TEST(AggregatorTest, DefaultKeyCountOpSpec) { +TEST(AggregatorTest, DefaultKeyCountOpSpec) +{ // // --- setup // @@ -58,12 +61,9 @@ TEST(AggregatorTest, DefaultKeyCountOpSpec) { // create some context attributes - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -72,13 +72,11 @@ TEST(AggregatorTest, DefaultKeyCountOpSpec) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -98,25 +96,23 @@ TEST(AggregatorTest, DefaultKeyCountOpSpec) { // add some entries - Variant v_val(47); + Variant v_val(47); cali_id_t node_ctx1 = 102; cali_id_t node_ctx2 = 101; cali_id_t val_id = val_attr.id(); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); Attribute count_attr = db.get_attribute("count"); @@ -128,28 +124,29 @@ TEST(AggregatorTest, DefaultKeyCountOpSpec) { int rescount = 0; - std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list){ - auto dict = make_dict_from_entrylist(list); - int aggr = dict[count_attr.id()].value().to_int(); - - if (dict[ctx1.id()].value() == Variant("inner")) { - EXPECT_EQ(aggr, 3); - EXPECT_EQ(static_cast(list.size()), 2); - ++rescount; - } else if (dict[ctx2.id()].value() == Variant(42)) { - EXPECT_EQ(aggr, 2); - EXPECT_EQ(static_cast(list.size()), 2); - ++rescount; - } else { - EXPECT_EQ(aggr, 2); - ++rescount; - } - }); + std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list) { + auto dict = make_dict_from_entrylist(list); + int aggr = dict[count_attr.id()].value().to_int(); + + if (dict[ctx1.id()].value() == Variant("inner")) { + EXPECT_EQ(aggr, 3); + EXPECT_EQ(static_cast(list.size()), 2); + ++rescount; + } else if (dict[ctx2.id()].value() == Variant(42)) { + EXPECT_EQ(aggr, 2); + EXPECT_EQ(static_cast(list.size()), 2); + ++rescount; + } else { + EXPECT_EQ(aggr, 2); + ++rescount; + } + }); EXPECT_EQ(rescount, 3); } -TEST(AggregatorTest, DefaultKeySumOpSpec) { +TEST(AggregatorTest, DefaultKeySumOpSpec) +{ // // --- setup // @@ -159,12 +156,9 @@ TEST(AggregatorTest, DefaultKeySumOpSpec) { // create some context attributes - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -173,13 +167,11 @@ TEST(AggregatorTest, DefaultKeySumOpSpec) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -205,25 +197,23 @@ TEST(AggregatorTest, DefaultKeySumOpSpec) { // add some entries - Variant v_val(7); + Variant v_val(7); cali_id_t node_ctx1 = 102; cali_id_t node_ctx2 = 101; cali_id_t val_id = val_attr.id(); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); Attribute count_attr = db.get_attribute("count"); Attribute sum_attr = db.get_attribute("sum#val"); @@ -237,33 +227,34 @@ TEST(AggregatorTest, DefaultKeySumOpSpec) { int rescount = 0; - std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list){ - auto dict = make_dict_from_entrylist(list); - - int aggr = dict[count_attr.id()].value().to_int(); - int val = dict[sum_attr.id() ].value().to_int(); - - if (dict[ctx1.id()].value() == Variant("inner")) { - EXPECT_EQ(aggr, 3); - EXPECT_EQ(val, 14); - EXPECT_EQ(static_cast(list.size()), 3); - ++rescount; - } else if (dict[ctx2.id()].value() == Variant(42)) { - EXPECT_EQ(aggr, 2); - EXPECT_EQ(val, 7); - EXPECT_EQ(static_cast(list.size()), 3); - ++rescount; - } else { - EXPECT_EQ(aggr, 2); - EXPECT_EQ(val, 7); - ++rescount; - } - }); + std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list) { + auto dict = make_dict_from_entrylist(list); + + int aggr = dict[count_attr.id()].value().to_int(); + int val = dict[sum_attr.id()].value().to_int(); + + if (dict[ctx1.id()].value() == Variant("inner")) { + EXPECT_EQ(aggr, 3); + EXPECT_EQ(val, 14); + EXPECT_EQ(static_cast(list.size()), 3); + ++rescount; + } else if (dict[ctx2.id()].value() == Variant(42)) { + EXPECT_EQ(aggr, 2); + EXPECT_EQ(val, 7); + EXPECT_EQ(static_cast(list.size()), 3); + ++rescount; + } else { + EXPECT_EQ(aggr, 2); + EXPECT_EQ(val, 7); + ++rescount; + } + }); EXPECT_EQ(rescount, 3); } -TEST(AggregatorTest, SingleKeySumOpSpec) { +TEST(AggregatorTest, SingleKeySumOpSpec) +{ // // --- setup // @@ -273,12 +264,9 @@ TEST(AggregatorTest, SingleKeySumOpSpec) { // create some context attributes - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -287,13 +275,11 @@ TEST(AggregatorTest, SingleKeySumOpSpec) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -318,28 +304,26 @@ TEST(AggregatorTest, SingleKeySumOpSpec) { // add some entries - Variant v_val(7); + Variant v_val(7); cali_id_t node_ctx1 = 102; cali_id_t node_ctx2 = 101; cali_id_t node_ctx3 = 100; cali_id_t val_id = val_attr.id(); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx3, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx3, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx3, 0, nullptr, nullptr, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); Attribute count_attr = db.get_attribute("count"); Attribute sum_attr = db.get_attribute("sum#val"); @@ -352,28 +336,29 @@ TEST(AggregatorTest, SingleKeySumOpSpec) { int rescount = 0; - std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list){ - auto dict = make_dict_from_entrylist(list); + std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list) { + auto dict = make_dict_from_entrylist(list); - int count = dict[count_attr.id()].value().to_int(); - int val = dict[sum_attr.id() ].value().to_int(); + int count = dict[count_attr.id()].value().to_int(); + int val = dict[sum_attr.id()].value().to_int(); - if (dict[ctx2.id()].value() == Variant(42)) { - EXPECT_EQ(count, 5); - EXPECT_EQ(val, 21); - EXPECT_EQ(static_cast(list.size()), 3); - ++rescount; - } else { - EXPECT_EQ(count, 4); - EXPECT_EQ(val, 14); - ++rescount; - } - }); + if (dict[ctx2.id()].value() == Variant(42)) { + EXPECT_EQ(count, 5); + EXPECT_EQ(val, 21); + EXPECT_EQ(static_cast(list.size()), 3); + ++rescount; + } else { + EXPECT_EQ(count, 4); + EXPECT_EQ(val, 14); + ++rescount; + } + }); EXPECT_EQ(rescount, 2); } -TEST(AggregatorTest, InclusiveSumOp) { +TEST(AggregatorTest, InclusiveSumOp) +{ // // --- setup // @@ -383,12 +368,9 @@ TEST(AggregatorTest, InclusiveSumOp) { // create some context attributes - Attribute ctx1 = - db.create_attribute("ictx.1", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute ctx2 = - db.create_attribute("ictx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx1 = db.create_attribute("ictx.1", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute ctx2 = db.create_attribute("ictx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -397,12 +379,9 @@ TEST(AggregatorTest, InclusiveSumOp) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx1.id(), 100, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, { 101, ctx1.id(), 100, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -430,25 +409,23 @@ TEST(AggregatorTest, InclusiveSumOp) { // add some entries - Variant v_val(7); + Variant v_val(7); cali_id_t node_ctx1 = 101; cali_id_t node_ctx2 = 100; cali_id_t val_id = val_attr.id(); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); Attribute count_attr = db.get_attribute("count"); Attribute sum_attr = db.get_attribute("sum#val"); @@ -466,33 +443,33 @@ TEST(AggregatorTest, InclusiveSumOp) { int rescount = 0; - std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list){ - auto dict = make_dict_from_entrylist(list); - - int count = dict[count_attr.id() ].value().to_int(); - int val = dict[sum_attr.id() ].value().to_int(); - int ival = dict[isum_attr.id() ].value().to_int(); - int iscval = dict[iscale_attr.id()].value().to_int(); - - if (dict[ctx1.id()].value() == Variant("inner")) { - EXPECT_EQ(val, 14); - EXPECT_EQ(ival, 14); - EXPECT_EQ(iscval, 28); - EXPECT_EQ(static_cast(list.size()), 6); - ++rescount; - } else if (dict[ctx1.id()].value() == Variant("outer")) { - EXPECT_EQ(val, 7); - EXPECT_EQ(ival, 21); - EXPECT_EQ(iscval, 42); - ++rescount; - } - }); + std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list) { + auto dict = make_dict_from_entrylist(list); + + int count = dict[count_attr.id()].value().to_int(); + int val = dict[sum_attr.id()].value().to_int(); + int ival = dict[isum_attr.id()].value().to_int(); + int iscval = dict[iscale_attr.id()].value().to_int(); + + if (dict[ctx1.id()].value() == Variant("inner")) { + EXPECT_EQ(val, 14); + EXPECT_EQ(ival, 14); + EXPECT_EQ(iscval, 28); + EXPECT_EQ(static_cast(list.size()), 6); + ++rescount; + } else if (dict[ctx1.id()].value() == Variant("outer")) { + EXPECT_EQ(val, 7); + EXPECT_EQ(ival, 21); + EXPECT_EQ(iscval, 42); + ++rescount; + } + }); EXPECT_EQ(rescount, 2); } - -TEST(AggregatorTest, InclusiveRatio) { +TEST(AggregatorTest, InclusiveRatio) +{ // // --- setup // @@ -502,12 +479,9 @@ TEST(AggregatorTest, InclusiveRatio) { // create some context attributes - Attribute ctx = - db.create_attribute("ctx", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute num_attr = - db.create_attribute("num", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute den_attr = - db.create_attribute("den", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx = db.create_attribute("ctx", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute num_attr = db.create_attribute("num", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute den_attr = db.create_attribute("den", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -516,12 +490,9 @@ TEST(AggregatorTest, InclusiveRatio) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx.id(), 100, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx.id(), CALI_INV_ID, Variant("outer") }, { 101, ctx.id(), 100, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -542,25 +513,23 @@ TEST(AggregatorTest, InclusiveRatio) { // add some entries - cali_id_t node_inner = 101; - cali_id_t node_outer = 100; - cali_id_t attr[2] = { num_attr.id(), den_attr.id() }; + cali_id_t node_inner = 101; + cali_id_t node_outer = 100; + cali_id_t attr[2] = { num_attr.id(), den_attr.id() }; Variant data_inner[2] = { Variant(10), Variant(10) }; - Variant data_outer[2] = { Variant(10), Variant(5) }; + Variant data_outer[2] = { Variant(10), Variant(5) }; - a.add(db, db.merge_snapshot(1, &node_outer, 2, attr, data_outer, idmap)); + a.add(db, db.merge_snapshot(1, &node_outer, 2, attr, data_outer, idmap)); a.add(db, db.merge_snapshot(1, &node_outer, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_outer, 2, attr, data_outer, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 2, attr, data_outer, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_inner, 2, attr, data_inner, idmap)); + a.add(db, db.merge_snapshot(1, &node_outer, 2, attr, data_outer, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 2, attr, data_outer, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + a.add(db, db.merge_snapshot(1, &node_inner, 2, attr, data_inner, idmap)); a.add(db, db.merge_snapshot(1, &node_inner, 0, nullptr, nullptr, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); Attribute iratio_attr = db.get_attribute("iratio#num/den"); @@ -572,24 +541,25 @@ TEST(AggregatorTest, InclusiveRatio) { int rescount = 0; - std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list){ - auto dict = make_dict_from_entrylist(list); + std::for_each(resdb.begin(), resdb.end(), [&](const EntryList& list) { + auto dict = make_dict_from_entrylist(list); - double iratio = dict[iratio_attr.id()].value().to_double(); + double iratio = dict[iratio_attr.id()].value().to_double(); - if (dict[ctx.id()].value() == Variant("inner")) { - EXPECT_DOUBLE_EQ(iratio, 1.0); - ++rescount; - } else if (dict[ctx.id()].value() == Variant("outer")) { - EXPECT_DOUBLE_EQ(iratio, 1.5); - ++rescount; - } - }); + if (dict[ctx.id()].value() == Variant("inner")) { + EXPECT_DOUBLE_EQ(iratio, 1.0); + ++rescount; + } else if (dict[ctx.id()].value() == Variant("outer")) { + EXPECT_DOUBLE_EQ(iratio, 1.5); + ++rescount; + } + }); EXPECT_EQ(rescount, 2); } -TEST(AggregatorTest, NoneKeySumOpSpec) { +TEST(AggregatorTest, NoneKeySumOpSpec) +{ // // --- setup // @@ -599,12 +569,9 @@ TEST(AggregatorTest, NoneKeySumOpSpec) { // create some context attributes - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -613,13 +580,11 @@ TEST(AggregatorTest, NoneKeySumOpSpec) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant("outer") }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -643,28 +608,26 @@ TEST(AggregatorTest, NoneKeySumOpSpec) { // add some entries - Variant v_val(7); + Variant v_val(7); cali_id_t node_ctx1 = 102; cali_id_t node_ctx2 = 101; cali_id_t node_ctx3 = 100; cali_id_t val_id = val_attr.id(); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx1, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx2, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - a.add(db, db.merge_snapshot(1, &node_ctx3, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &val_id, &v_val, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + a.add(db, db.merge_snapshot(1, &node_ctx3, 1, &val_id, &v_val, idmap)); a.add(db, db.merge_snapshot(1, &node_ctx3, 0, nullptr, nullptr, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); Attribute count_attr = db.get_attribute("count"); Attribute sum_attr = db.get_attribute("sum#val"); @@ -676,25 +639,24 @@ TEST(AggregatorTest, NoneKeySumOpSpec) { EXPECT_EQ(resdb.size(), 1); // one for (empty key) - auto dict = make_dict_from_entrylist(resdb.front()); + auto dict = make_dict_from_entrylist(resdb.front()); - int count = dict[count_attr.id()].value().to_int(); - int val = dict[sum_attr.id() ].value().to_int(); + int count = dict[count_attr.id()].value().to_int(); + int val = dict[sum_attr.id()].value().to_int(); - EXPECT_EQ(count, 9); - EXPECT_EQ(val, 35); + EXPECT_EQ(count, 9); + EXPECT_EQ(val, 35); } -TEST(AggregatorTest, StatisticsKernels) { +TEST(AggregatorTest, StatisticsKernels) +{ CaliperMetadataDB db; IdMap idmap; - // create some context attributes + // create some context attributes - Attribute ctx = - db.create_attribute("ctx", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx = db.create_attribute("ctx", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -703,12 +665,9 @@ TEST(AggregatorTest, StatisticsKernels) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx.id(), CALI_INV_ID, Variant("outer") }, - { 101, ctx.id(), 100, Variant("inner") } - }; + } test_nodes[] = { { 100, ctx.id(), CALI_INV_ID, Variant("outer") }, { 101, ctx.id(), 100, Variant("inner") } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -729,9 +688,9 @@ TEST(AggregatorTest, StatisticsKernels) { Aggregator a(spec), b(spec); - Variant v_ints[4] = { Variant(-4), Variant(9), Variant(25), Variant(36) }; - cali_id_t node_id = 101; - cali_id_t val_id = val_attr.id(); + Variant v_ints[4] = { Variant(-4), Variant(9), Variant(25), Variant(36) }; + cali_id_t node_id = 101; + cali_id_t val_id = val_attr.id(); a.add(db, db.merge_snapshot(1, &node_id, 1, &val_id, &v_ints[0], idmap)); a.add(db, db.merge_snapshot(1, &node_id, 1, &val_id, &v_ints[1], idmap)); @@ -754,30 +713,27 @@ TEST(AggregatorTest, StatisticsKernels) { std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); // check results EXPECT_EQ(resdb.size(), 1); // one entry - auto dict = make_dict_from_entrylist(resdb.front()); + auto dict = make_dict_from_entrylist(resdb.front()); EXPECT_EQ(dict[attr_min.id()].value().to_int(), -4); EXPECT_EQ(dict[attr_max.id()].value().to_int(), 36); EXPECT_DOUBLE_EQ(dict[attr_avg.id()].value().to_double(), 16.5); - EXPECT_DOUBLE_EQ(dict[attr_var.id()].value().to_double(), 2018.0/4.0 - (16.5*16.5)); + EXPECT_DOUBLE_EQ(dict[attr_var.id()].value().to_double(), 2018.0 / 4.0 - (16.5 * 16.5)); } -TEST(AggregatorTest, ScaledRatioKernel) { +TEST(AggregatorTest, ScaledRatioKernel) +{ CaliperMetadataDB db; IdMap idmap; - Attribute x_attr = - db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute y_attr = - db.create_attribute("y", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute x_attr = db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute y_attr = db.create_attribute("y", CALI_TYPE_INT, CALI_ATTR_ASVALUE); QuerySpec spec; @@ -788,17 +744,15 @@ TEST(AggregatorTest, ScaledRatioKernel) { Aggregator a(spec); - Variant v_ints[4] = { Variant(10), Variant(20), Variant(74), Variant(22) }; - cali_id_t attrs[2] = { x_attr.id(), y_attr.id() }; + Variant v_ints[4] = { Variant(10), Variant(20), Variant(74), Variant(22) }; + cali_id_t attrs[2] = { x_attr.id(), y_attr.id() }; - a.add(db, db.merge_snapshot(0, nullptr, 2, attrs, v_ints+0, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 2, attrs, v_ints+2, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 2, attrs, v_ints + 0, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 2, attrs, v_ints + 2, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); ASSERT_EQ(resdb.size(), 1); @@ -811,12 +765,12 @@ TEST(AggregatorTest, ScaledRatioKernel) { EXPECT_DOUBLE_EQ(dict[attr_ratio.id()].value().to_double(), 20.0); } -TEST(AggregatorTest, ScaledSumKernel) { +TEST(AggregatorTest, ScaledSumKernel) +{ CaliperMetadataDB db; IdMap idmap; - Attribute x_attr = - db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute x_attr = db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); QuerySpec spec; @@ -827,17 +781,15 @@ TEST(AggregatorTest, ScaledSumKernel) { Aggregator a(spec); - Variant v_ints[2] = { Variant(10), Variant(20) }; - cali_id_t attr_id = x_attr.id(); + Variant v_ints[2] = { Variant(10), Variant(20) }; + cali_id_t attr_id = x_attr.id(); - a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints+0, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints+1, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints + 0, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints + 1, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); ASSERT_EQ(resdb.size(), 1); @@ -850,13 +802,12 @@ TEST(AggregatorTest, ScaledSumKernel) { EXPECT_DOUBLE_EQ(dict[attr_scale.id()].value().to_double(), 15.0); } - -TEST(AggregatorTest, ScaledCountKernel) { +TEST(AggregatorTest, ScaledCountKernel) +{ CaliperMetadataDB db; IdMap idmap; - Attribute x_attr = - db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute x_attr = db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); QuerySpec spec; @@ -867,17 +818,15 @@ TEST(AggregatorTest, ScaledCountKernel) { Aggregator a(spec); - Variant v_ints[2] = { Variant(10), Variant(20) }; - cali_id_t attr_id = x_attr.id(); + Variant v_ints[2] = { Variant(10), Variant(20) }; + cali_id_t attr_id = x_attr.id(); - a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints+0, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints+1, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints + 0, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints + 1, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); ASSERT_EQ(resdb.size(), 1); @@ -890,12 +839,12 @@ TEST(AggregatorTest, ScaledCountKernel) { EXPECT_DOUBLE_EQ(dict[attr_scale.id()].value().to_double(), 5.0); } -TEST(AggregatorTest, AnyKernel) { +TEST(AggregatorTest, AnyKernel) +{ CaliperMetadataDB db; IdMap idmap; - Attribute x_attr = - db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute x_attr = db.create_attribute("x", CALI_TYPE_INT, CALI_ATTR_ASVALUE); QuerySpec spec; @@ -906,17 +855,15 @@ TEST(AggregatorTest, AnyKernel) { Aggregator a(spec); - Variant v_ints[2] = { Variant(42), Variant(42) }; - cali_id_t attr_id = x_attr.id(); + Variant v_ints[2] = { Variant(42), Variant(42) }; + cali_id_t attr_id = x_attr.id(); - a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints+0, idmap)); - a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints+1, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints + 0, idmap)); + a.add(db, db.merge_snapshot(0, nullptr, 1, &attr_id, v_ints + 1, idmap)); std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); ASSERT_EQ(resdb.size(), 1); @@ -929,16 +876,15 @@ TEST(AggregatorTest, AnyKernel) { EXPECT_DOUBLE_EQ(dict[attr_scale.id()].value().to_double(), 42.0); } -TEST(AggregatorTest, PercentTotalKernel) { +TEST(AggregatorTest, PercentTotalKernel) +{ CaliperMetadataDB db; IdMap idmap; - // create some context attributes + // create some context attributes - Attribute ctx = - db.create_attribute("ctx", CALI_TYPE_INT, CALI_ATTR_NESTED); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx = db.create_attribute("ctx", CALI_TYPE_INT, CALI_ATTR_NESTED); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes @@ -947,13 +893,11 @@ TEST(AggregatorTest, PercentTotalKernel) { cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx.id(), CALI_INV_ID, Variant(-1) }, - { 101, ctx.id(), 100, Variant(42) }, - { 102, ctx.id(), 101, Variant(24) } - }; + } test_nodes[] = { { 100, ctx.id(), CALI_INV_ID, Variant(-1) }, + { 101, ctx.id(), 100, Variant(42) }, + { 102, ctx.id(), 101, Variant(24) } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); // @@ -972,10 +916,10 @@ TEST(AggregatorTest, PercentTotalKernel) { Aggregator a(spec), b(spec); - Variant v_ints[4] = { Variant(4), Variant(24), Variant(16), Variant(36) }; - cali_id_t nodea_id = 101; - cali_id_t nodeb_id = 102; - cali_id_t val_id = val_attr.id(); + Variant v_ints[4] = { Variant(4), Variant(24), Variant(16), Variant(36) }; + cali_id_t nodea_id = 101; + cali_id_t nodeb_id = 102; + cali_id_t val_id = val_attr.id(); a.add(db, db.merge_snapshot(1, &nodea_id, 1, &val_id, &v_ints[0], idmap)); a.add(db, db.merge_snapshot(1, &nodeb_id, 1, &val_id, &v_ints[1], idmap)); @@ -994,52 +938,50 @@ TEST(AggregatorTest, PercentTotalKernel) { std::vector resdb; - a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { - resdb.push_back(list); - }); + a.flush(db, [&resdb](CaliperMetadataAccessInterface&, const EntryList& list) { resdb.push_back(list); }); // check results EXPECT_EQ(resdb.size(), 3); // three entries - auto it = std::find_if(resdb.begin(), resdb.end(), [ctx](const EntryList& list){ - for (const Entry& e : list) - if (e.value(ctx).to_int() == 42) - return true; - return false; - }); + auto it = std::find_if(resdb.begin(), resdb.end(), [ctx](const EntryList& list) { + for (const Entry& e : list) + if (e.value(ctx).to_int() == 42) + return true; + return false; + }); ASSERT_NE(it, resdb.end()); - auto dict = make_dict_from_entrylist(*it); + auto dict = make_dict_from_entrylist(*it); EXPECT_DOUBLE_EQ(dict[attr_pct.id()].value().to_double(), 25.0); EXPECT_DOUBLE_EQ(dict[attr_ipct.id()].value().to_double(), 100.0); - it = std::find_if(resdb.begin(), resdb.end(), [ctx](const EntryList& list){ - for (const Entry& e : list) - if (e.value(ctx).to_int() == 24) - return true; - return false; - }); + it = std::find_if(resdb.begin(), resdb.end(), [ctx](const EntryList& list) { + for (const Entry& e : list) + if (e.value(ctx).to_int() == 24) + return true; + return false; + }); ASSERT_NE(it, resdb.end()); - dict = make_dict_from_entrylist(*it); + dict = make_dict_from_entrylist(*it); EXPECT_DOUBLE_EQ(dict[attr_pct.id()].value().to_double(), 75.0); EXPECT_DOUBLE_EQ(dict[attr_ipct.id()].value().to_double(), 75.0); - it = std::find_if(resdb.begin(), resdb.end(), [ctx](const EntryList& list){ - for (const Entry& e : list) - if (e.value(ctx).to_int() == -1) - return true; - return false; - }); + it = std::find_if(resdb.begin(), resdb.end(), [ctx](const EntryList& list) { + for (const Entry& e : list) + if (e.value(ctx).to_int() == -1) + return true; + return false; + }); ASSERT_NE(it, resdb.end()); - dict = make_dict_from_entrylist(*it); + dict = make_dict_from_entrylist(*it); EXPECT_DOUBLE_EQ(dict[attr_pct.id()].value().to_double(), 0.0); EXPECT_DOUBLE_EQ(dict[attr_ipct.id()].value().to_double(), 100.0); diff --git a/src/reader/test/test_calireader.cpp b/src/reader/test/test_calireader.cpp index 7c5e5e9e0..a8c84a473 100644 --- a/src/reader/test/test_calireader.cpp +++ b/src/reader/test/test_calireader.cpp @@ -49,21 +49,19 @@ __rec=globals,ref=22=54 TEST(CaliReader, BasicRead) { CaliperMetadataDB db; - CaliReader reader; + CaliReader reader; std::istringstream is(cali_txt); unsigned node_count = 0; - unsigned rec_count = 0; + unsigned rec_count = 0; - NodeProcessFn node_proc = - [&node_count](CaliperMetadataAccessInterface&,const Node*) { - ++node_count; - }; - SnapshotProcessFn snap_proc = - [&rec_count](CaliperMetadataAccessInterface&,const EntryList&){ - ++rec_count; - }; + NodeProcessFn node_proc = [&node_count](CaliperMetadataAccessInterface&, const Node*) { + ++node_count; + }; + SnapshotProcessFn snap_proc = [&rec_count](CaliperMetadataAccessInterface&, const EntryList&) { + ++rec_count; + }; reader.read(is, db, node_proc, snap_proc); diff --git a/src/reader/test/test_calqlparser.cpp b/src/reader/test/test_calqlparser.cpp index 790221efc..8e3e6137b 100644 --- a/src/reader/test/test_calqlparser.cpp +++ b/src/reader/test/test_calqlparser.cpp @@ -6,16 +6,17 @@ using namespace cali; -TEST(CalQLParserTest, SelectClause) { +TEST(CalQLParserTest, SelectClause) +{ std::istringstream is("select a,a.a, b , c "); - CalQLParser p1(is); + CalQLParser p1(is); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); QuerySpec q1 = p1.spec(); EXPECT_EQ(q1.select.selection, QuerySpec::AttributeSelection::List); - EXPECT_EQ(q1.aggregate.selection, QuerySpec::AggregationSelection::None); + EXPECT_EQ(q1.aggregate.selection, QuerySpec::AggregationSelection::None); ASSERT_EQ(q1.select.list.size(), 4); @@ -32,7 +33,7 @@ TEST(CalQLParserTest, SelectClause) { QuerySpec q2 = p2.spec(); EXPECT_EQ(q2.select.selection, QuerySpec::AttributeSelection::List); - EXPECT_EQ(q2.aggregate.selection, QuerySpec::AggregationSelection::None); + EXPECT_EQ(q2.aggregate.selection, QuerySpec::AggregationSelection::None); ASSERT_EQ(q2.select.list.size(), 1); EXPECT_EQ(q2.select.list[0], "aa"); @@ -54,7 +55,8 @@ TEST(CalQLParserTest, SelectClause) { EXPECT_EQ(q5.aggregate.selection, QuerySpec::AggregationSelection::None); } -TEST(CalQLParserTest, SelectClauseWithAggregation) { +TEST(CalQLParserTest, SelectClauseWithAggregation) +{ CalQLParser p1("select aa,count(),sum(bb)"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -67,7 +69,7 @@ TEST(CalQLParserTest, SelectClauseWithAggregation) { EXPECT_EQ(q1.select.list[1], "count"); EXPECT_EQ(q1.select.list[2], "sum#bb"); - EXPECT_EQ(q1.aggregate.selection, QuerySpec::AggregationSelection::List); + EXPECT_EQ(q1.aggregate.selection, QuerySpec::AggregationSelection::List); ASSERT_EQ(q1.aggregate.list.size(), 2); EXPECT_STREQ(q1.aggregate.list[0].op.name, "count"); @@ -87,7 +89,7 @@ TEST(CalQLParserTest, SelectClauseWithAggregation) { EXPECT_EQ(q2.select.list[1], "b"); EXPECT_EQ(q2.select.list[2], "ccc"); - EXPECT_EQ(q2.aggregate.selection, QuerySpec::AggregationSelection::List); + EXPECT_EQ(q2.aggregate.selection, QuerySpec::AggregationSelection::List); ASSERT_EQ(q2.aggregate.list.size(), 1); EXPECT_STREQ(q2.aggregate.list[0].op.name, "count"); @@ -108,12 +110,13 @@ TEST(CalQLParserTest, SelectClauseWithAggregation) { QuerySpec q6 = p6.spec(); EXPECT_EQ(q6.select.selection, QuerySpec::AttributeSelection::All); - EXPECT_EQ(q6.aggregate.selection, QuerySpec::AggregationSelection::List); + EXPECT_EQ(q6.aggregate.selection, QuerySpec::AggregationSelection::List); ASSERT_EQ(q6.aggregate.list.size(), 1); EXPECT_STREQ(q6.aggregate.list[0].op.name, "count"); } -TEST(CalQLParserTest, WhereClause) { +TEST(CalQLParserTest, WhereClause) +{ CalQLParser p1("where a,bbb<17, NOT cc , dd = 5, not eee = foo, ff>42"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -147,7 +150,8 @@ TEST(CalQLParserTest, WhereClause) { EXPECT_TRUE(p3.error()); } -TEST(CalQLParserTest, GroupByClause) { +TEST(CalQLParserTest, GroupByClause) +{ CalQLParser p1("GROUP By aa,b, ccc "); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -173,7 +177,8 @@ TEST(CalQLParserTest, GroupByClause) { EXPECT_EQ(q2.groupby.use_path, true); } -TEST(CalQLParserTest, OrderByClause1) { +TEST(CalQLParserTest, OrderByClause1) +{ CalQLParser p1("Order By aa, b desc , c asc, ddd "); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -184,16 +189,17 @@ TEST(CalQLParserTest, OrderByClause1) { ASSERT_EQ(q1.sort.list.size(), 4); EXPECT_EQ(q1.sort.list[0].attribute, "aa"); - EXPECT_EQ(q1.sort.list[0].order, QuerySpec::SortSpec::Ascending); + EXPECT_EQ(q1.sort.list[0].order, QuerySpec::SortSpec::Ascending); EXPECT_EQ(q1.sort.list[1].attribute, "b"); - EXPECT_EQ(q1.sort.list[1].order, QuerySpec::SortSpec::Descending); + EXPECT_EQ(q1.sort.list[1].order, QuerySpec::SortSpec::Descending); EXPECT_EQ(q1.sort.list[2].attribute, "c"); - EXPECT_EQ(q1.sort.list[2].order, QuerySpec::SortSpec::Ascending); + EXPECT_EQ(q1.sort.list[2].order, QuerySpec::SortSpec::Ascending); EXPECT_EQ(q1.sort.list[3].attribute, "ddd"); - EXPECT_EQ(q1.sort.list[3].order, QuerySpec::SortSpec::Ascending); + EXPECT_EQ(q1.sort.list[3].order, QuerySpec::SortSpec::Ascending); } -TEST(CalQLParserTest, OrderByClause2) { +TEST(CalQLParserTest, OrderByClause2) +{ CalQLParser p("Order By aa,\"b with space\" format table "); EXPECT_FALSE(p.error()) << "Unexpected parse error: " << p.error_msg(); @@ -204,15 +210,16 @@ TEST(CalQLParserTest, OrderByClause2) { ASSERT_EQ(q.sort.list.size(), 2); EXPECT_EQ(q.sort.list[0].attribute, "aa"); - EXPECT_EQ(q.sort.list[0].order, QuerySpec::SortSpec::Ascending); + EXPECT_EQ(q.sort.list[0].order, QuerySpec::SortSpec::Ascending); EXPECT_EQ(q.sort.list[1].attribute, "b with space"); - EXPECT_EQ(q.sort.list[1].order, QuerySpec::SortSpec::Ascending); + EXPECT_EQ(q.sort.list[1].order, QuerySpec::SortSpec::Ascending); EXPECT_EQ(q.format.opt, QuerySpec::FormatSpec::User); EXPECT_STREQ(q.format.formatter.name, "table"); } -TEST(CalQLParserTest, OrderByClause3) { +TEST(CalQLParserTest, OrderByClause3) +{ CalQLParser p("Order By aa DESC format table "); EXPECT_FALSE(p.error()) << "Unexpected parse error: " << p.error_msg(); @@ -223,13 +230,14 @@ TEST(CalQLParserTest, OrderByClause3) { ASSERT_EQ(q.sort.list.size(), 1); EXPECT_EQ(q.sort.list[0].attribute, "aa"); - EXPECT_EQ(q.sort.list[0].order, QuerySpec::SortSpec::Descending); + EXPECT_EQ(q.sort.list[0].order, QuerySpec::SortSpec::Descending); EXPECT_EQ(q.format.opt, QuerySpec::FormatSpec::User); EXPECT_STREQ(q.format.formatter.name, "table"); } -TEST(CalQLParserTest, FormatSpec) { +TEST(CalQLParserTest, FormatSpec) +{ { CalQLParser p1("FORMAT tree(\"a,bb,ccc\")"); @@ -275,7 +283,7 @@ TEST(CalQLParserTest, FormatSpec) { { CalQLParser p6("FORMAT json(object)"); - QuerySpec q6 = p6.spec(); + QuerySpec q6 = p6.spec(); EXPECT_FALSE(p6.error()) << "Unexpected parse error: " << p6.error_msg(); EXPECT_STREQ(q6.format.formatter.name, "json"); EXPECT_EQ(q6.format.kwargs.size(), 1); @@ -283,7 +291,8 @@ TEST(CalQLParserTest, FormatSpec) { } } -TEST(CalQLParserTest, AggregateClause) { +TEST(CalQLParserTest, AggregateClause) +{ CalQLParser p1(" Aggregate SUM ( aaaa ), Count( ) "); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -321,7 +330,8 @@ TEST(CalQLParserTest, AggregateClause) { EXPECT_TRUE(p4.error()); } -TEST(CalQLParserTest, AliasAttribute) { +TEST(CalQLParserTest, AliasAttribute) +{ CalQLParser p1("select a as \"my alias (for a)\", b"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -338,7 +348,8 @@ TEST(CalQLParserTest, AliasAttribute) { EXPECT_STREQ(q1.aliases["a"].c_str(), "my alias (for a)"); } -TEST(CalQLParserTest, AliasAggregate) { +TEST(CalQLParserTest, AliasAggregate) +{ CalQLParser p1("select x,percent_total(a) as \"my alias (for percent_total#a)\" format table"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -362,7 +373,8 @@ TEST(CalQLParserTest, AliasAggregate) { EXPECT_STREQ(q1.aliases["percent_total#a"].c_str(), "my alias (for percent_total#a)"); } -TEST(CalQLParserTest, AttributeUnit) { +TEST(CalQLParserTest, AttributeUnit) +{ CalQLParser p1("select x,scale(a,1e-6) unit \"sec\" format table"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -387,7 +399,8 @@ TEST(CalQLParserTest, AttributeUnit) { EXPECT_STREQ(q1.units["scale#a"].c_str(), "sec"); } -TEST(CalQLParserTest, AttributeAliasAndUnit) { +TEST(CalQLParserTest, AttributeAliasAndUnit) +{ CalQLParser p1("select x,ratio(a,b,1e-6) As \"Read BW\" uNiT \"MB/s\" format table"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -415,14 +428,16 @@ TEST(CalQLParserTest, AttributeAliasAndUnit) { EXPECT_STREQ(q1.aliases["ratio#a/b"].c_str(), "Read BW"); } -TEST(CalQLParserTest, AttributeDoubleAliasParseError) { +TEST(CalQLParserTest, AttributeDoubleAliasParseError) +{ CalQLParser p1("select x,ratio(a,b,1e-6) As \"Read BW\" AS again format table"); EXPECT_TRUE(p1.error()); EXPECT_STREQ(p1.error_msg().c_str(), "Expected clause keyword, got as"); } -TEST(CalQLParserTest, FullStatement) { +TEST(CalQLParserTest, FullStatement) +{ const char* s1 = "SELECT a,bb, cc, count() where bb< 42, NOT d=\"foo,\"\\ bar, c GROUP BY a, bb,d\n" "FORMAT json "; @@ -464,8 +479,7 @@ TEST(CalQLParserTest, FullStatement) { EXPECT_EQ(q1.format.opt, QuerySpec::FormatSpec::User); EXPECT_STREQ(q1.format.formatter.name, "json"); - const char* s2 = - " SELECT count(), *, SUM(x\\\\y) GROUP BY a.b.c where group "; + const char* s2 = " SELECT count(), *, SUM(x\\\\y) GROUP BY a.b.c where group "; CalQLParser p2(s2); @@ -506,7 +520,8 @@ TEST(CalQLParserTest, FullStatement) { EXPECT_EQ(q4.format.opt, QuerySpec::FormatSpec::User); } -TEST(CalQLParserTest, GarbageAtEnd) { +TEST(CalQLParserTest, GarbageAtEnd) +{ CalQLParser p1(" select a,b,c format tree = where b"); EXPECT_TRUE(p1.error()); @@ -514,8 +529,8 @@ TEST(CalQLParserTest, GarbageAtEnd) { EXPECT_TRUE(p2.error()); } - -TEST(CalQLParserTest, LetClause) { +TEST(CalQLParserTest, LetClause) +{ CalQLParser p1("let x= ratio( a, \"b\" ) , y=scale(c,42) let z=truncate ( yy )"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -545,7 +560,8 @@ TEST(CalQLParserTest, LetClause) { EXPECT_EQ(q1.preprocess_ops[2].cond.op, QuerySpec::Condition::None); } -TEST(CalQLParserTest, LetIfClause) { +TEST(CalQLParserTest, LetIfClause) +{ CalQLParser p1("let x= ratio( a, \"b\" ) if not X, y=scale(c,42) if Y = foo let z=truncate ( yy ) if not Z>1"); EXPECT_FALSE(p1.error()) << "Unexpected parse error: " << p1.error_msg(); @@ -572,7 +588,8 @@ TEST(CalQLParserTest, LetIfClause) { EXPECT_STREQ(q1.preprocess_ops[2].cond.value.c_str(), "1"); } -TEST(CalQLParserTest, LetClauseErrors) { +TEST(CalQLParserTest, LetClauseErrors) +{ CalQLParser p1("let blagarbl"); EXPECT_TRUE(p1.error()); diff --git a/src/reader/test/test_filter.cpp b/src/reader/test/test_filter.cpp index 67ee561aa..ff4382b96 100644 --- a/src/reader/test/test_filter.cpp +++ b/src/reader/test/test_filter.cpp @@ -12,8 +12,7 @@ using namespace cali; namespace { -bool -has_attribute(const EntryList& list, const Attribute& attr) +bool has_attribute(const EntryList& list, const Attribute& attr) { for (const Entry& e : list) if (attr.store_as_value()) { @@ -24,12 +23,11 @@ has_attribute(const EntryList& list, const Attribute& attr) if (node->attribute() == attr.id()) return true; } - + return false; } -bool -has_entry(const EntryList& list, const Attribute& attr, const Variant& val) +bool has_entry(const EntryList& list, const Attribute& attr, const Variant& val) { for (const Entry& e : list) if (attr.store_as_value()) { @@ -40,41 +38,36 @@ has_entry(const EntryList& list, const Attribute& attr, const Variant& val) if (node->attribute() == attr.id() && node->data() == val) return true; } - + return false; } -} +} // namespace -TEST(RecordFilterTest, TestExist) { +TEST(RecordFilterTest, TestExist) +{ CaliperMetadataDB db; IdMap idmap; // create some context attributes - - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute ctx3 = - db.create_attribute("ctx.3", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute ctx3 = db.create_attribute("ctx.3", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes - + const struct NodeInfo { cali_id_t node_id; cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 6) }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 6) } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 6) }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 6) } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); Variant v_val47(47); @@ -86,19 +79,19 @@ TEST(RecordFilterTest, TestExist) { std::vector in; in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx2, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - - QuerySpec::Condition ex_1 { QuerySpec::Condition::Op::Exist, "ctx.1", "" }; - QuerySpec::Condition ex_2 { QuerySpec::Condition::Op::Exist, "ctx.2", "" }; - QuerySpec::Condition ex_3 { QuerySpec::Condition::Op::Exist, "ctx.3", "" }; - QuerySpec::Condition ex_v { QuerySpec::Condition::Op::Exist, "val", "" }; + in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val47, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + + QuerySpec::Condition ex_1 { QuerySpec::Condition::Op::Exist, "ctx.1", "" }; + QuerySpec::Condition ex_2 { QuerySpec::Condition::Op::Exist, "ctx.2", "" }; + QuerySpec::Condition ex_3 { QuerySpec::Condition::Op::Exist, "ctx.3", "" }; + QuerySpec::Condition ex_v { QuerySpec::Condition::Op::Exist, "val", "" }; QuerySpec::Condition nex_1 { QuerySpec::Condition::Op::NotExist, "ctx.1", "" }; - QuerySpec::Condition nex_v { QuerySpec::Condition::Op::NotExist, "val", "" }; + QuerySpec::Condition nex_v { QuerySpec::Condition::Op::NotExist, "val", "" }; { QuerySpec spec; @@ -106,16 +99,16 @@ TEST(RecordFilterTest, TestExist) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(ex_1); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 5); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(::has_attribute(rec, ctx1)); } @@ -125,16 +118,16 @@ TEST(RecordFilterTest, TestExist) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(ex_3); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 0); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_FALSE(::has_attribute(rec, ctx3)); } @@ -144,16 +137,16 @@ TEST(RecordFilterTest, TestExist) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(ex_2); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 2); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(::has_attribute(rec, ctx2)); } @@ -164,35 +157,35 @@ TEST(RecordFilterTest, TestExist) { spec.filter.list.push_back(ex_1); spec.filter.list.push_back(ex_v); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 3); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(::has_attribute(rec, ctx1) && ::has_attribute(rec, val_attr)); } - + { QuerySpec spec; spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(nex_1); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 2); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(!::has_attribute(rec, ctx1)); } @@ -203,47 +196,43 @@ TEST(RecordFilterTest, TestExist) { spec.filter.list.push_back(ex_1); spec.filter.list.push_back(nex_v); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 2); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(::has_attribute(rec, ctx1) && !::has_attribute(rec, val_attr)); } } -TEST(RecordFilterTest, TestEqual) { +TEST(RecordFilterTest, TestEqual) +{ CaliperMetadataDB db; IdMap idmap; // create some context attributes - - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes - + const struct NodeInfo { cali_id_t node_id; cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 5) }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 5) } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 5) }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 5) } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); Variant v_val47(47); @@ -255,23 +244,23 @@ TEST(RecordFilterTest, TestEqual) { std::vector in; in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx2, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - - QuerySpec::Condition eq_1a { QuerySpec::Condition::Op::Equal, "ctx.1", "outer" }; - QuerySpec::Condition eq_1b { QuerySpec::Condition::Op::Equal, "ctx.1", "inner" }; - QuerySpec::Condition eq_1c { QuerySpec::Condition::Op::Equal, "ctx.1", "inner" }; - QuerySpec::Condition eq_2a { QuerySpec::Condition::Op::Equal, "ctx.2", "42" }; - QuerySpec::Condition eq_2b { QuerySpec::Condition::Op::Equal, "ctx.2", "142" }; - QuerySpec::Condition eq_va { QuerySpec::Condition::Op::Equal, "val", "47" }; - QuerySpec::Condition eq_vb { QuerySpec::Condition::Op::Equal, "val", "147" }; + in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val47, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + + QuerySpec::Condition eq_1a { QuerySpec::Condition::Op::Equal, "ctx.1", "outer" }; + QuerySpec::Condition eq_1b { QuerySpec::Condition::Op::Equal, "ctx.1", "inner" }; + QuerySpec::Condition eq_1c { QuerySpec::Condition::Op::Equal, "ctx.1", "inner" }; + QuerySpec::Condition eq_2a { QuerySpec::Condition::Op::Equal, "ctx.2", "42" }; + QuerySpec::Condition eq_2b { QuerySpec::Condition::Op::Equal, "ctx.2", "142" }; + QuerySpec::Condition eq_va { QuerySpec::Condition::Op::Equal, "val", "47" }; + QuerySpec::Condition eq_vb { QuerySpec::Condition::Op::Equal, "val", "147" }; QuerySpec::Condition neq_1a { QuerySpec::Condition::Op::NotEqual, "ctx.1", "outer" }; - QuerySpec::Condition neq_2a { QuerySpec::Condition::Op::NotEqual, "val", "42" }; - QuerySpec::Condition neq_va { QuerySpec::Condition::Op::NotEqual, "val", "47" }; + QuerySpec::Condition neq_2a { QuerySpec::Condition::Op::NotEqual, "val", "42" }; + QuerySpec::Condition neq_va { QuerySpec::Condition::Op::NotEqual, "val", "47" }; { QuerySpec spec; @@ -279,16 +268,16 @@ TEST(RecordFilterTest, TestEqual) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(eq_1a); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 5); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(::has_entry(rec, ctx1, Variant(CALI_TYPE_STRING, "outer", 5))); } @@ -298,16 +287,16 @@ TEST(RecordFilterTest, TestEqual) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(eq_va); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 4); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_TRUE(::has_entry(rec, val_attr, v_val47)); } @@ -317,10 +306,10 @@ TEST(RecordFilterTest, TestEqual) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(eq_vb); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); @@ -333,16 +322,16 @@ TEST(RecordFilterTest, TestEqual) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(neq_1a); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 2); - for ( const EntryList& rec : result ) + for (const EntryList& rec : result) EXPECT_FALSE(::has_entry(rec, ctx1, Variant(CALI_TYPE_STRING, "outer", 5))); } @@ -353,49 +342,45 @@ TEST(RecordFilterTest, TestEqual) { spec.filter.list.push_back(eq_2a); spec.filter.list.push_back(neq_va); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 1); - for ( const EntryList& rec : result ) { - EXPECT_TRUE (::has_entry(rec, ctx2, Variant(42))); + for (const EntryList& rec : result) { + EXPECT_TRUE(::has_entry(rec, ctx2, Variant(42))); EXPECT_FALSE(::has_entry(rec, val_attr, v_val47)); } } } -TEST(RecordFilterTest, TestLess) { +TEST(RecordFilterTest, TestLess) +{ CaliperMetadataDB db; IdMap idmap; // create some context attributes - - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes - + const struct NodeInfo { cali_id_t node_id; cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 5) }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 5) } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 5) }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 5) } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); Variant v_val47(47); @@ -408,16 +393,16 @@ TEST(RecordFilterTest, TestLess) { std::vector in; in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx2, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val42, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val42, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - QuerySpec::Condition ls_45 { QuerySpec::Condition::Op::LessThan, "val", "45" }; - QuerySpec::Condition ls_50 { QuerySpec::Condition::Op::LessThan, "val", "50" }; - QuerySpec::Condition le_42 { QuerySpec::Condition::Op::LessOrEqual, "val", "42" }; + QuerySpec::Condition ls_45 { QuerySpec::Condition::Op::LessThan, "val", "45" }; + QuerySpec::Condition ls_50 { QuerySpec::Condition::Op::LessThan, "val", "50" }; + QuerySpec::Condition le_42 { QuerySpec::Condition::Op::LessOrEqual, "val", "42" }; { QuerySpec spec; @@ -425,17 +410,17 @@ TEST(RecordFilterTest, TestLess) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(ls_45); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 1); - for ( const EntryList& rec : result ) { - EXPECT_TRUE (::has_entry(rec, val_attr, v_val42)); + for (const EntryList& rec : result) { + EXPECT_TRUE(::has_entry(rec, val_attr, v_val42)); EXPECT_FALSE(::has_entry(rec, val_attr, v_val47)); } } @@ -446,17 +431,17 @@ TEST(RecordFilterTest, TestLess) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(ls_50); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 4); - for ( const EntryList& rec : result ) - EXPECT_TRUE (::has_entry(rec, val_attr, v_val42) || ::has_entry(rec, val_attr, v_val47)); + for (const EntryList& rec : result) + EXPECT_TRUE(::has_entry(rec, val_attr, v_val42) || ::has_entry(rec, val_attr, v_val47)); } { @@ -465,49 +450,45 @@ TEST(RecordFilterTest, TestLess) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(le_42); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 1); - for ( const EntryList& rec : result ) { - EXPECT_TRUE (::has_entry(rec, val_attr, v_val42)); + for (const EntryList& rec : result) { + EXPECT_TRUE(::has_entry(rec, val_attr, v_val42)); EXPECT_FALSE(::has_entry(rec, val_attr, v_val47)); } } } -TEST(RecordFilterTest, TestGreater) { +TEST(RecordFilterTest, TestGreater) +{ CaliperMetadataDB db; IdMap idmap; // create some context attributes - - Attribute ctx1 = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute ctx2 = - db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - Attribute val_attr = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + + Attribute ctx1 = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx2 = db.create_attribute("ctx.2", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute val_attr = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); // make some nodes - + const struct NodeInfo { cali_id_t node_id; cali_id_t attr_id; cali_id_t prnt_id; Variant data; - } test_nodes[] = { - { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 5) }, - { 101, ctx2.id(), 100, Variant(42) }, - { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 5) } - }; + } test_nodes[] = { { 100, ctx1.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "outer", 5) }, + { 101, ctx2.id(), 100, Variant(42) }, + { 102, ctx1.id(), 101, Variant(CALI_TYPE_STRING, "inner", 5) } }; - for ( const NodeInfo& nI : test_nodes ) + for (const NodeInfo& nI : test_nodes) db.merge_node(nI.node_id, nI.attr_id, nI.prnt_id, nI.data, idmap); Variant v_val47(47); @@ -520,16 +501,16 @@ TEST(RecordFilterTest, TestGreater) { std::vector in; in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(1, &node_ctx1, 0, nullptr, nullptr, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx1, 1, &attr_id, &v_val47, idmap)); in.push_back(db.merge_snapshot(1, &node_ctx2, 1, &attr_id, &v_val47, idmap)); - in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val42, idmap)); - in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(1, &node_ctx2, 0, nullptr, nullptr, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 1, &attr_id, &v_val42, idmap)); + in.push_back(db.merge_snapshot(0, nullptr, 0, nullptr, nullptr, idmap)); - QuerySpec::Condition gt_45 { QuerySpec::Condition::Op::GreaterThan, "val", "45" }; - QuerySpec::Condition gt_40 { QuerySpec::Condition::Op::GreaterThan, "val", "40" }; - QuerySpec::Condition ge_47 { QuerySpec::Condition::Op::GreaterOrEqual, "val", "47" }; + QuerySpec::Condition gt_45 { QuerySpec::Condition::Op::GreaterThan, "val", "45" }; + QuerySpec::Condition gt_40 { QuerySpec::Condition::Op::GreaterThan, "val", "40" }; + QuerySpec::Condition ge_47 { QuerySpec::Condition::Op::GreaterOrEqual, "val", "47" }; { QuerySpec spec; @@ -537,17 +518,17 @@ TEST(RecordFilterTest, TestGreater) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(gt_45); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 3); - for ( const EntryList& rec : result ) { - EXPECT_TRUE (::has_entry(rec, val_attr, v_val47)); + for (const EntryList& rec : result) { + EXPECT_TRUE(::has_entry(rec, val_attr, v_val47)); EXPECT_FALSE(::has_entry(rec, val_attr, v_val42)); } } @@ -558,17 +539,17 @@ TEST(RecordFilterTest, TestGreater) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(gt_40); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 4); - for ( const EntryList& rec : result ) - EXPECT_TRUE (::has_entry(rec, val_attr, v_val42) || ::has_entry(rec, val_attr, v_val47)); + for (const EntryList& rec : result) + EXPECT_TRUE(::has_entry(rec, val_attr, v_val42) || ::has_entry(rec, val_attr, v_val47)); } { @@ -577,17 +558,17 @@ TEST(RecordFilterTest, TestGreater) { spec.filter.selection = QuerySpec::FilterSelection::List; spec.filter.list.push_back(ge_47); - RecordSelector filter(spec); + RecordSelector filter(spec); std::vector result; - for ( const EntryList& rec : in ) + for (const EntryList& rec : in) if (filter.pass(db, rec)) result.push_back(rec); EXPECT_EQ(static_cast(result.size()), 3); - for ( const EntryList& rec : result ) { - EXPECT_TRUE (::has_entry(rec, val_attr, v_val47)); + for (const EntryList& rec : result) { + EXPECT_TRUE(::has_entry(rec, val_attr, v_val47)); EXPECT_FALSE(::has_entry(rec, val_attr, v_val42)); } } diff --git a/src/reader/test/test_flatexclusiveregionprofile.cpp b/src/reader/test/test_flatexclusiveregionprofile.cpp index 77acf7b92..7439e0c8b 100644 --- a/src/reader/test/test_flatexclusiveregionprofile.cpp +++ b/src/reader/test/test_flatexclusiveregionprofile.cpp @@ -8,43 +8,32 @@ using namespace cali; -TEST(FlatExclusiveRegionProfileTest, NestedRegion) { +TEST(FlatExclusiveRegionProfileTest, NestedRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - Node* a_node = - db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); - Node* c_node = - db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* a_node = db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); + Node* c_node = db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); FlatExclusiveRegionProfile rp(db, "metric.attr"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); @@ -57,44 +46,32 @@ TEST(FlatExclusiveRegionProfileTest, NestedRegion) { EXPECT_DOUBLE_EQ(reg_profile["b"], 40.0); } - -TEST(FlatExclusiveRegionProfileTest, GivenRegion) { +TEST(FlatExclusiveRegionProfileTest, GivenRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - Node* a_node = - db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); - Node* c_node = - db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* a_node = db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); + Node* c_node = db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); FlatExclusiveRegionProfile rp(db, "metric.attr", "reg_c"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); diff --git a/src/reader/test/test_flatinclusiveregionprofile.cpp b/src/reader/test/test_flatinclusiveregionprofile.cpp index 50d4a04e2..3a3209ebd 100644 --- a/src/reader/test/test_flatinclusiveregionprofile.cpp +++ b/src/reader/test/test_flatinclusiveregionprofile.cpp @@ -8,43 +8,32 @@ using namespace cali; -TEST(FlatInclusiveRegionProfileTest, NestedRegion) { +TEST(FlatInclusiveRegionProfileTest, NestedRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - Node* a_node = - db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); - Node* c_node = - db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* a_node = db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); + Node* c_node = db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); FlatInclusiveRegionProfile rp(db, "metric.attr"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); @@ -57,44 +46,32 @@ TEST(FlatInclusiveRegionProfileTest, NestedRegion) { EXPECT_DOUBLE_EQ(reg_profile["b"], 140.0); } - -TEST(FlatInclusiveRegionProfileTest, GivenRegion) { +TEST(FlatInclusiveRegionProfileTest, GivenRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - - Node* c_node = - db.merge_node(200, reg_c_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "c", 2), idmap); - Node* a_node = - db.merge_node(201, reg_a_attr.id(), 200, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(202, reg_b_attr.id(), 201, Variant(CALI_TYPE_STRING, "b", 2), idmap); + + Node* c_node = db.merge_node(200, reg_c_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* a_node = db.merge_node(201, reg_a_attr.id(), 200, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(202, reg_b_attr.id(), 201, Variant(CALI_TYPE_STRING, "b", 2), idmap); FlatInclusiveRegionProfile rp(db, "metric.attr", "reg_c"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); diff --git a/src/reader/test/test_metadb.cpp b/src/reader/test/test_metadb.cpp index b020af9bb..7ceb17671 100644 --- a/src/reader/test/test_metadb.cpp +++ b/src/reader/test/test_metadb.cpp @@ -6,27 +6,23 @@ using namespace cali; -TEST(MetaDBTest, MergeSnapshotFromDB) { +TEST(MetaDBTest, MergeSnapshotFromDB) +{ CaliperMetadataDB db1; - Attribute str_attr = - db1.create_attribute("str.attr", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute int_attr = - db1.create_attribute("int.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute str_attr = db1.create_attribute("str.attr", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute int_attr = db1.create_attribute("int.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); IdMap idmap; - Node* a_in = - db1.merge_node(200, str_attr.id(), CALI_INV_ID, Variant("a"), idmap); - Node* b_in = - db1.merge_node(201, str_attr.id(), 200, Variant("b"), idmap); + Node* a_in = db1.merge_node(200, str_attr.id(), CALI_INV_ID, Variant("a"), idmap); + Node* b_in = db1.merge_node(201, str_attr.id(), 200, Variant("b"), idmap); EntryList list_in { Entry(b_in), Entry(int_attr, Variant(42)) }; CaliperMetadataDB db2; - EntryList list_out = - db2.merge_snapshot(db1, list_in); + EntryList list_out = db2.merge_snapshot(db1, list_in); Attribute str_attr_out = db2.get_attribute("str.attr"); Attribute int_attr_out = db2.get_attribute("int.attr"); @@ -74,19 +70,16 @@ int count_in_record(const std::vector& rec, const Attribute& a, const Var return count; } -} +} // namespace -TEST(MetaDBTest, SetGlobal) { +TEST(MetaDBTest, SetGlobal) +{ CaliperMetadataDB db; - Attribute g_str_attr = - db.create_attribute("global.str", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); - Attribute g_int_attr = - db.create_attribute("global.int", CALI_TYPE_INT, CALI_ATTR_GLOBAL); - Attribute g_val_attr = - db.create_attribute("global.val", CALI_TYPE_INT, CALI_ATTR_GLOBAL | CALI_ATTR_ASVALUE); - Attribute no_g_attr = - db.create_attribute("noglobal", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute g_str_attr = db.create_attribute("global.str", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute g_int_attr = db.create_attribute("global.int", CALI_TYPE_INT, CALI_ATTR_GLOBAL); + Attribute g_val_attr = db.create_attribute("global.val", CALI_TYPE_INT, CALI_ATTR_GLOBAL | CALI_ATTR_ASVALUE); + Attribute no_g_attr = db.create_attribute("noglobal", CALI_TYPE_INT, CALI_ATTR_DEFAULT); Variant v_str_a(CALI_TYPE_STRING, "a", 1); Variant v_str_b(CALI_TYPE_STRING, "b", 1); @@ -95,11 +88,11 @@ TEST(MetaDBTest, SetGlobal) { Variant v_no(-42); db.set_global(g_str_attr, v_str_a); - db.set_global(g_int_attr, v_int ); - db.set_global(g_val_attr, v_val ); + db.set_global(g_int_attr, v_int); + db.set_global(g_val_attr, v_val); db.set_global(g_str_attr, v_str_a); // should be set only once db.set_global(g_str_attr, v_str_b); - db.set_global(no_g_attr, v_no ); + db.set_global(no_g_attr, v_no); std::vector globals = db.get_globals(); @@ -107,9 +100,9 @@ TEST(MetaDBTest, SetGlobal) { EXPECT_EQ(count_in_record(globals, g_str_attr, v_str_a), 1); EXPECT_EQ(count_in_record(globals, g_str_attr, v_str_b), 1); - EXPECT_EQ(count_in_record(globals, g_int_attr, v_int ), 1); - EXPECT_EQ(count_in_record(globals, g_val_attr, v_val ), 1); - EXPECT_EQ(count_in_record(globals, no_g_attr, v_no ), 0); + EXPECT_EQ(count_in_record(globals, g_int_attr, v_int), 1); + EXPECT_EQ(count_in_record(globals, g_val_attr, v_val), 1); + EXPECT_EQ(count_in_record(globals, no_g_attr, v_no), 0); CaliperMetadataDB db_imp; @@ -125,26 +118,22 @@ TEST(MetaDBTest, SetGlobal) { EXPECT_EQ(count_in_record(imp_globals, g_imp_str_attr, v_str_a), 1); EXPECT_EQ(count_in_record(imp_globals, g_imp_str_attr, v_str_b), 1); - EXPECT_EQ(count_in_record(imp_globals, g_imp_int_attr, v_int ), 1); - EXPECT_EQ(count_in_record(imp_globals, g_imp_val_attr, v_val ), 1); + EXPECT_EQ(count_in_record(imp_globals, g_imp_int_attr, v_int), 1); + EXPECT_EQ(count_in_record(imp_globals, g_imp_val_attr, v_val), 1); } -TEST(MetadataDBTest, StringDB) { +TEST(MetadataDBTest, StringDB) +{ CaliperMetadataDB db; - Attribute attr = - db.create_attribute("string.attr", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute attr = db.create_attribute("string.attr", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - Node* n0 = - db.merge_node(100, attr.id(), CALI_INV_ID, Variant("a.b" ), idmap); - Node* n1 = - db.merge_node(101, attr.id(), 100, Variant("a" ), idmap); - Node* n2 = - db.merge_node(102, attr.id(), 101, Variant("a.b.c"), idmap); - Node* n3 = - db.merge_node(103, attr.id(), 102, Variant("a.b" ), idmap); + Node* n0 = db.merge_node(100, attr.id(), CALI_INV_ID, Variant("a.b"), idmap); + Node* n1 = db.merge_node(101, attr.id(), 100, Variant("a"), idmap); + Node* n2 = db.merge_node(102, attr.id(), 101, Variant("a.b.c"), idmap); + Node* n3 = db.merge_node(103, attr.id(), 102, Variant("a.b"), idmap); EXPECT_EQ(n0->data().to_string(), std::string("a.b")); EXPECT_EQ(n1->data().to_string(), std::string("a")); @@ -161,21 +150,21 @@ TEST(MetadataDBTest, StringDB) { EXPECT_EQ(os.str(), std::string("CaliperMetadataDB: stored 21 nodes, 6 strings.\n")); } -TEST(MetadataDBTest, AliasesAndUnits) { +TEST(MetadataDBTest, AliasesAndUnits) +{ CaliperMetadataDB db; std::map aliases = { std::make_pair("x.attr", "x alias") }; - std::map units = { std::make_pair("x.attr", "x unit") }; + std::map units = { std::make_pair("x.attr", "x unit") }; db.add_attribute_aliases(aliases); db.add_attribute_units(units); - Attribute attr = - db.create_attribute("x.attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + Attribute attr = db.create_attribute("x.attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT); Attribute alias_attr = db.get_attribute("attribute.alias"); Attribute unit_attr = db.get_attribute("attribute.unit"); EXPECT_EQ(attr.get(alias_attr).to_string(), "x alias"); - EXPECT_EQ(attr.get(unit_attr).to_string(), "x unit"); + EXPECT_EQ(attr.get(unit_attr).to_string(), "x unit"); } diff --git a/src/reader/test/test_nestedexclusiveregionprofile.cpp b/src/reader/test/test_nestedexclusiveregionprofile.cpp index 70bfb43bb..55f6e5de9 100644 --- a/src/reader/test/test_nestedexclusiveregionprofile.cpp +++ b/src/reader/test/test_nestedexclusiveregionprofile.cpp @@ -8,47 +8,34 @@ using namespace cali; -TEST(NestedExclusiveRegionProfileTest, NestedRegion) { +TEST(NestedExclusiveRegionProfileTest, NestedRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - Node* a_node = - db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); - Node* c_node = - db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); - Node* d_node = - db.merge_node(203, reg_b_attr.id(), 202, Variant(CALI_TYPE_STRING, "d", 2), idmap); + Node* a_node = db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); + Node* c_node = db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* d_node = db.merge_node(203, reg_b_attr.id(), 202, Variant(CALI_TYPE_STRING, "d", 2), idmap); NestedExclusiveRegionProfile rp(db, "metric.attr"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(d_node), Entry(metric_attr, Variant(400)) } ); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(d_node), Entry(metric_attr, Variant(400)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); @@ -62,44 +49,32 @@ TEST(NestedExclusiveRegionProfileTest, NestedRegion) { EXPECT_DOUBLE_EQ(reg_profile["a/b/d"], 400.0); } - -TEST(NestedExclusiveRegionProfileTest, GivenRegion) { +TEST(NestedExclusiveRegionProfileTest, GivenRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - - Node* c_node = - db.merge_node(200, reg_c_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "c", 2), idmap); - Node* a_node = - db.merge_node(201, reg_a_attr.id(), 200, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(202, reg_b_attr.id(), 201, Variant(CALI_TYPE_STRING, "b", 2), idmap); + + Node* c_node = db.merge_node(200, reg_c_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* a_node = db.merge_node(201, reg_a_attr.id(), 200, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(202, reg_b_attr.id(), 201, Variant(CALI_TYPE_STRING, "b", 2), idmap); NestedExclusiveRegionProfile rp(db, "metric.attr", "reg_c"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); diff --git a/src/reader/test/test_nestedinclusiveregionprofile.cpp b/src/reader/test/test_nestedinclusiveregionprofile.cpp index de8310509..81701d5e0 100644 --- a/src/reader/test/test_nestedinclusiveregionprofile.cpp +++ b/src/reader/test/test_nestedinclusiveregionprofile.cpp @@ -8,47 +8,34 @@ using namespace cali; -TEST(NestedInclusiveRegionProfileTest, NestedRegion) { +TEST(NestedInclusiveRegionProfileTest, NestedRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - Node* a_node = - db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); - Node* c_node = - db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); - Node* d_node = - db.merge_node(203, reg_b_attr.id(), 202, Variant(CALI_TYPE_STRING, "d", 2), idmap); + Node* a_node = db.merge_node(200, reg_a_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(201, reg_b_attr.id(), 200, Variant(CALI_TYPE_STRING, "b", 2), idmap); + Node* c_node = db.merge_node(202, reg_c_attr.id(), 201, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* d_node = db.merge_node(203, reg_b_attr.id(), 202, Variant(CALI_TYPE_STRING, "d", 2), idmap); NestedInclusiveRegionProfile rp(db, "metric.attr"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(d_node), Entry(metric_attr, Variant(400)) } ); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(d_node), Entry(metric_attr, Variant(400)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); @@ -62,44 +49,32 @@ TEST(NestedInclusiveRegionProfileTest, NestedRegion) { EXPECT_DOUBLE_EQ(reg_profile["a/b/d"], 400.0); } - -TEST(NestedInclusiveRegionProfileTest, GivenRegion) { +TEST(NestedInclusiveRegionProfileTest, GivenRegion) +{ CaliperMetadataDB db; - Attribute metric_attr = - db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute reg_a_attr = - db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_b_attr = - db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute reg_c_attr = - db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute metric_attr = db.create_attribute("metric.attr", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute reg_a_attr = db.create_attribute("reg_a", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_b_attr = db.create_attribute("reg_b", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute reg_c_attr = db.create_attribute("reg_c", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; - - Node* c_node = - db.merge_node(200, reg_c_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "c", 2), idmap); - Node* a_node = - db.merge_node(201, reg_a_attr.id(), 200, Variant(CALI_TYPE_STRING, "a", 2), idmap); - Node* b_node = - db.merge_node(202, reg_b_attr.id(), 201, Variant(CALI_TYPE_STRING, "b", 2), idmap); + + Node* c_node = db.merge_node(200, reg_c_attr.id(), CALI_INV_ID, Variant(CALI_TYPE_STRING, "c", 2), idmap); + Node* a_node = db.merge_node(201, reg_a_attr.id(), 200, Variant(CALI_TYPE_STRING, "a", 2), idmap); + Node* b_node = db.merge_node(202, reg_b_attr.id(), 201, Variant(CALI_TYPE_STRING, "b", 2), idmap); NestedInclusiveRegionProfile rp(db, "metric.attr", "reg_c"); - rp(db, - { Entry(a_node), Entry(metric_attr, Variant(2)) } ); - rp(db, - { Entry(b_node), Entry(metric_attr, Variant(40)) } ); - rp(db, - { Entry(c_node), Entry(metric_attr, Variant(100)) } ); - rp(db, - { Entry(metric_attr, Variant(1000)) }); - rp(db, - { Entry(b_node) }); + rp(db, { Entry(a_node), Entry(metric_attr, Variant(2)) }); + rp(db, { Entry(b_node), Entry(metric_attr, Variant(40)) }); + rp(db, { Entry(c_node), Entry(metric_attr, Variant(100)) }); + rp(db, { Entry(metric_attr, Variant(1000)) }); + rp(db, { Entry(b_node) }); std::map reg_profile; - double total_reg = 0.0; - double total = 0.0; + double total_reg = 0.0; + double total = 0.0; std::tie(reg_profile, total_reg, total) = rp.result(); diff --git a/src/reader/test/test_nodebuffer.cpp b/src/reader/test/test_nodebuffer.cpp index 0b1b46321..b6b71ad00 100644 --- a/src/reader/test/test_nodebuffer.cpp +++ b/src/reader/test/test_nodebuffer.cpp @@ -12,41 +12,44 @@ using namespace cali; namespace { - void recursive_append_path(const CaliperMetadataAccessInterface& db, - const Node* node, - NodeBuffer& buf, - std::set& written_nodes) - { - if (!node || node->id() == CALI_INV_ID) - return; - if (written_nodes.count(node->id()) > 0) - return; - - if (node->attribute() < node->id()) - recursive_append_path(db, db.node(node->attribute()), buf, written_nodes); - - recursive_append_path(db, node->parent(), buf, written_nodes); - - if (written_nodes.count(node->id()) > 0) - return; - - written_nodes.insert(node->id()); - buf.append(node); - } - - void append_path(const CaliperMetadataAccessInterface& db, const Node* node, NodeBuffer& buf) - { - std::set written_nodes; - - recursive_append_path(db, node, buf, written_nodes); - } +void recursive_append_path( + const CaliperMetadataAccessInterface& db, + const Node* node, + NodeBuffer& buf, + std::set& written_nodes +) +{ + if (!node || node->id() == CALI_INV_ID) + return; + if (written_nodes.count(node->id()) > 0) + return; + + if (node->attribute() < node->id()) + recursive_append_path(db, db.node(node->attribute()), buf, written_nodes); + + recursive_append_path(db, node->parent(), buf, written_nodes); + + if (written_nodes.count(node->id()) > 0) + return; + + written_nodes.insert(node->id()); + buf.append(node); } -TEST(NodeBufferTest, Append) { +void append_path(const CaliperMetadataAccessInterface& db, const Node* node, NodeBuffer& buf) +{ + std::set written_nodes; + + recursive_append_path(db, node, buf, written_nodes); +} +} // namespace + +TEST(NodeBufferTest, Append) +{ // Quick NodeBuffer in/out test using some attributes - + CaliperMetadataDB in_db; - + Attribute in_1_attr = in_db.create_attribute("my.string.attr", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); Attribute in_2_attr = in_db.create_attribute("my.int.attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT); @@ -55,7 +58,7 @@ TEST(NodeBufferTest, Append) { ASSERT_NE(in_1, nullptr); ASSERT_NE(in_2, nullptr); - + NodeBuffer buf; ::append_path(in_db, in_1, buf); @@ -64,7 +67,7 @@ TEST(NodeBufferTest, Append) { EXPECT_GE(buf.count(), static_cast(2)); CaliperMetadataDB out_db; - IdMap idmap; + IdMap idmap; auto add_node = [&out_db, &idmap](const NodeBuffer::NodeInfo& info) { out_db.merge_node(info.node_id, info.attr_id, info.parent_id, info.value, idmap); @@ -77,17 +80,18 @@ TEST(NodeBufferTest, Append) { ASSERT_TRUE(out_1_attr); ASSERT_TRUE(out_2_attr); - + EXPECT_STREQ(out_1_attr.name().c_str(), "my.string.attr"); EXPECT_EQ(out_1_attr.type(), CALI_TYPE_STRING); EXPECT_EQ(out_1_attr.properties(), in_1_attr.properties()); - + EXPECT_STREQ(out_2_attr.name().c_str(), "my.int.attr"); - EXPECT_EQ(out_2_attr.type(), CALI_TYPE_INT); + EXPECT_EQ(out_2_attr.type(), CALI_TYPE_INT); EXPECT_EQ(out_2_attr.properties(), in_2_attr.properties()); } -TEST(NodeBufferTest, Import) { +TEST(NodeBufferTest, Import) +{ CaliperMetadataDB in_db; Attribute in_1_attr = in_db.create_attribute("my.string.attr", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); @@ -98,7 +102,7 @@ TEST(NodeBufferTest, Import) { ASSERT_NE(in_1, nullptr); ASSERT_NE(in_2, nullptr); - + NodeBuffer in_buf; ::append_path(in_db, in_1, in_buf); @@ -113,7 +117,7 @@ TEST(NodeBufferTest, Import) { EXPECT_EQ(out_buf.count(), in_buf.count()); CaliperMetadataDB out_db; - IdMap idmap; + IdMap idmap; auto add_node = [&out_db, &idmap](const NodeBuffer::NodeInfo& info) { out_db.merge_node(info.node_id, info.attr_id, info.parent_id, info.value, idmap); @@ -126,12 +130,12 @@ TEST(NodeBufferTest, Import) { ASSERT_TRUE(out_1_attr); ASSERT_TRUE(out_2_attr); - + EXPECT_STREQ(out_1_attr.name().c_str(), "my.string.attr"); EXPECT_EQ(out_1_attr.type(), CALI_TYPE_STRING); EXPECT_EQ(out_1_attr.properties(), in_1_attr.properties()); - + EXPECT_STREQ(out_2_attr.name().c_str(), "my.int.attr"); - EXPECT_EQ(out_2_attr.type(), CALI_TYPE_INT); - EXPECT_EQ(out_2_attr.properties(), in_2_attr.properties()); + EXPECT_EQ(out_2_attr.type(), CALI_TYPE_INT); + EXPECT_EQ(out_2_attr.properties(), in_2_attr.properties()); } diff --git a/src/reader/test/test_preprocessor.cpp b/src/reader/test/test_preprocessor.cpp index bd315a476..bd6e107e4 100644 --- a/src/reader/test/test_preprocessor.cpp +++ b/src/reader/test/test_preprocessor.cpp @@ -11,13 +11,17 @@ using namespace cali; namespace { -QuerySpec::AggregationOp -make_op(const char* name, const char* arg1 = nullptr, const char* arg2 = nullptr, const char* arg3 = nullptr) +QuerySpec::AggregationOp make_op( + const char* name, + const char* arg1 = nullptr, + const char* arg2 = nullptr, + const char* arg3 = nullptr +) { - QuerySpec::AggregationOp op; + QuerySpec::AggregationOp op; const QuerySpec::FunctionSignature* p = Preprocessor::preprocess_defs(); - for ( ; p && p->name && (0 != strcmp(p->name, name)); ++p ) + for (; p && p->name && (0 != strcmp(p->name, name)); ++p) ; if (p->name) { @@ -34,25 +38,29 @@ make_op(const char* name, const char* arg1 = nullptr, const char* arg2 = nullptr return op; } -QuerySpec::PreprocessSpec -make_spec(const std::string& target, const QuerySpec::AggregationOp& op) +QuerySpec::PreprocessSpec make_spec(const std::string& target, const QuerySpec::AggregationOp& op) { QuerySpec::PreprocessSpec spec; - spec.target = target; - spec.op = op; + spec.target = target; + spec.op = op; spec.cond.op = QuerySpec::Condition::None; return spec; } -QuerySpec::PreprocessSpec -make_spec_with_cond(const std::string& target, const QuerySpec::AggregationOp& op, const QuerySpec::Condition::Op cond, const char* cond_attr, const char* cond_val = nullptr) +QuerySpec::PreprocessSpec make_spec_with_cond( + const std::string& target, + const QuerySpec::AggregationOp& op, + const QuerySpec::Condition::Op cond, + const char* cond_attr, + const char* cond_val = nullptr +) { QuerySpec::PreprocessSpec spec; - spec.target = target; - spec.op = op; + spec.target = target; + spec.op = op; spec.cond.op = cond; if (cond_attr) spec.cond.attr_name = cond_attr; @@ -62,12 +70,11 @@ make_spec_with_cond(const std::string& target, const QuerySpec::AggregationOp& o return spec; } -std::map -make_dict_from_entrylist(const EntryList& list) +std::map make_dict_from_entrylist(const EntryList& list) { std::map dict; - for ( const Entry& e : list ) + for (const Entry& e : list) dict[e.attribute()] = e; return dict; @@ -75,8 +82,8 @@ make_dict_from_entrylist(const EntryList& list) } // namespace - -TEST(PreprocessorTest, Ratio) { +TEST(PreprocessorTest, Ratio) +{ // // --- setup // @@ -84,12 +91,9 @@ TEST(PreprocessorTest, Ratio) { CaliperMetadataDB db; IdMap idmap; - Attribute ctx = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute nom = - db.create_attribute("nom", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute dnm = - db.create_attribute("dnm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute nom = db.create_attribute("nom", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute dnm = db.create_attribute("dnm", CALI_TYPE_INT, CALI_ATTR_ASVALUE); EntryList rec; @@ -107,7 +111,7 @@ TEST(PreprocessorTest, Ratio) { // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute d_attr = db.get_attribute("d.ratio"); Attribute s_attr = db.get_attribute("s.ratio"); @@ -124,12 +128,12 @@ TEST(PreprocessorTest, Ratio) { ASSERT_NE(d_it, res.end()) << "d.ratio attribute not found\n"; ASSERT_NE(s_it, res.end()) << "s.ratio attribute not found\n"; - EXPECT_DOUBLE_EQ(d_it->second.value().to_double(), 6.0); + EXPECT_DOUBLE_EQ(d_it->second.value().to_double(), 6.0); EXPECT_DOUBLE_EQ(s_it->second.value().to_double(), 12.0); } - -TEST(PreprocessorTest, Scale) { +TEST(PreprocessorTest, Scale) +{ // // --- setup // @@ -137,10 +141,8 @@ TEST(PreprocessorTest, Scale) { CaliperMetadataDB db; IdMap idmap; - Attribute ctx_a = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_a = - db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx_a = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_a = db.create_attribute("val", CALI_TYPE_INT, CALI_ATTR_ASVALUE); EntryList rec; @@ -157,7 +159,7 @@ TEST(PreprocessorTest, Scale) { // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute v_attr = db.get_attribute("val"); Attribute d_attr = db.get_attribute("valx2.0"); @@ -180,8 +182,8 @@ TEST(PreprocessorTest, Scale) { EXPECT_DOUBLE_EQ(h_it->second.value().to_double(), 21.0); } - -TEST(PreprocessorTest, First) { +TEST(PreprocessorTest, First) +{ // // --- setup // @@ -189,12 +191,9 @@ TEST(PreprocessorTest, First) { CaliperMetadataDB db; IdMap idmap; - Attribute ctx_a = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_a = - db.create_attribute("val.a", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute val_b = - db.create_attribute("val.b", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx_a = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_a = db.create_attribute("val.a", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute val_b = db.create_attribute("val.b", CALI_TYPE_INT, CALI_ATTR_ASVALUE); EntryList rec; @@ -204,15 +203,15 @@ TEST(PreprocessorTest, First) { QuerySpec spec; - spec.preprocess_ops.push_back(::make_spec("val.a.out", ::make_op("first", "dummy.0", "val.a", "dummy.1"))); - spec.preprocess_ops.push_back(::make_spec("val.b.out", ::make_op("first", "val.b", "val.a", "dummy.0"))); + spec.preprocess_ops.push_back(::make_spec("val.a.out", ::make_op("first", "dummy.0", "val.a", "dummy.1"))); + spec.preprocess_ops.push_back(::make_spec("val.b.out", ::make_op("first", "val.b", "val.a", "dummy.0"))); // // --- run // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute vao_attr = db.get_attribute("val.a.out"); Attribute vbo_attr = db.get_attribute("val.b.out"); @@ -233,8 +232,8 @@ TEST(PreprocessorTest, First) { EXPECT_EQ(b_it->second.value().to_int(), 24); } - -TEST(PreprocessorTest, Truncate) { +TEST(PreprocessorTest, Truncate) +{ // // --- setup // @@ -242,10 +241,8 @@ TEST(PreprocessorTest, Truncate) { CaliperMetadataDB db; IdMap idmap; - Attribute ctx_a = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_a = - db.create_attribute("val", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); + Attribute ctx_a = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_a = db.create_attribute("val", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); EntryList rec; @@ -262,9 +259,9 @@ TEST(PreprocessorTest, Truncate) { // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); - Attribute v_attr = db.get_attribute("val"); + Attribute v_attr = db.get_attribute("val"); Attribute t6_attr = db.get_attribute("valt6"); Attribute td_attr = db.get_attribute("valtd"); @@ -274,7 +271,7 @@ TEST(PreprocessorTest, Truncate) { EXPECT_EQ(td_attr.type(), CALI_TYPE_DOUBLE); EXPECT_TRUE(td_attr.store_as_value()); - auto res = ::make_dict_from_entrylist(out); + auto res = ::make_dict_from_entrylist(out); auto t6_it = res.find(t6_attr.id()); auto td_it = res.find(td_attr.id()); @@ -285,8 +282,8 @@ TEST(PreprocessorTest, Truncate) { EXPECT_DOUBLE_EQ(td_it->second.value().to_double(), 15.0); } - -TEST(PreprocessorTest, Chain) { +TEST(PreprocessorTest, Chain) +{ // // --- setup // @@ -294,10 +291,8 @@ TEST(PreprocessorTest, Chain) { CaliperMetadataDB db; IdMap idmap; - Attribute ctx_a = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_a = - db.create_attribute("val", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); + Attribute ctx_a = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_a = db.create_attribute("val", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); EntryList rec; @@ -306,7 +301,7 @@ TEST(PreprocessorTest, Chain) { QuerySpec spec; - spec.preprocess_ops.push_back(::make_spec("valx2", ::make_op("scale", "val", "2.0"))); + spec.preprocess_ops.push_back(::make_spec("valx2", ::make_op("scale", "val", "2.0"))); spec.preprocess_ops.push_back(::make_spec("valx2t5", ::make_op("truncate", "valx2", "10"))); // @@ -314,7 +309,7 @@ TEST(PreprocessorTest, Chain) { // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute v_attr = db.get_attribute("val"); Attribute d_attr = db.get_attribute("valx2"); @@ -337,7 +332,8 @@ TEST(PreprocessorTest, Chain) { EXPECT_DOUBLE_EQ(t_it->second.value().to_double(), 10.0); } -TEST(PreprocessorTest, Conditions) { +TEST(PreprocessorTest, Conditions) +{ // // --- setup // @@ -345,12 +341,9 @@ TEST(PreprocessorTest, Conditions) { CaliperMetadataDB db; IdMap idmap; - Attribute ctx_a = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_a = - db.create_attribute("val.a", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute val_b = - db.create_attribute("val.b", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx_a = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_a = db.create_attribute("val.a", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute val_b = db.create_attribute("val.b", CALI_TYPE_INT, CALI_ATTR_ASVALUE); EntryList rec; @@ -360,15 +353,19 @@ TEST(PreprocessorTest, Conditions) { QuerySpec spec; - spec.preprocess_ops.push_back(::make_spec_with_cond("val.a.out", ::make_op("first", "val.a"), QuerySpec::Condition::Exist, "ctx.1")); - spec.preprocess_ops.push_back(::make_spec_with_cond("val.b.out", ::make_op("first", "val.b"), QuerySpec::Condition::NotExist, "ctx.1")); + spec.preprocess_ops.push_back( + ::make_spec_with_cond("val.a.out", ::make_op("first", "val.a"), QuerySpec::Condition::Exist, "ctx.1") + ); + spec.preprocess_ops.push_back( + ::make_spec_with_cond("val.b.out", ::make_op("first", "val.b"), QuerySpec::Condition::NotExist, "ctx.1") + ); // // --- run // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute vao_attr = db.get_attribute("val.a.out"); Attribute vbo_attr = db.get_attribute("val.b.out"); @@ -388,14 +385,11 @@ TEST(PreprocessorTest, Conditions) { TEST(PreprocessorTest, SumKernel) { CaliperMetadataDB db; - IdMap idmap; + IdMap idmap; - Attribute ctx_a = - db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - Attribute val_a = - db.create_attribute("val.a", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute val_b = - db.create_attribute("val.b", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute ctx_a = db.create_attribute("ctx.1", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute val_a = db.create_attribute("val.a", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute val_b = db.create_attribute("val.b", CALI_TYPE_INT, CALI_ATTR_ASVALUE); EntryList rec; @@ -405,15 +399,15 @@ TEST(PreprocessorTest, SumKernel) QuerySpec spec; - spec.preprocess_ops.push_back(::make_spec("val.a.out", ::make_op("sum", "dummy.0", "val.a", "dummy.1"))); - spec.preprocess_ops.push_back(::make_spec("val.s.out", ::make_op("sum", "val.b", "val.a", "dummy.0"))); + spec.preprocess_ops.push_back(::make_spec("val.a.out", ::make_op("sum", "dummy.0", "val.a", "dummy.1"))); + spec.preprocess_ops.push_back(::make_spec("val.s.out", ::make_op("sum", "val.b", "val.a", "dummy.0"))); // // --- run // Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute vao_attr = db.get_attribute("val.a.out"); Attribute vso_attr = db.get_attribute("val.s.out"); @@ -438,8 +432,7 @@ TEST(PreprocessorTest, LeafKernel) { CaliperMetadataDB db; - Attribute ctx = - db.create_attribute("ctx", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute ctx = db.create_attribute("ctx", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); Attribute a[2] = { ctx, ctx }; Variant v[2] = { Variant("foo"), Variant("bar") }; @@ -453,7 +446,7 @@ TEST(PreprocessorTest, LeafKernel) spec.preprocess_ops.push_back(::make_spec("leaf", ::make_op("leaf", "ctx"))); Preprocessor pp(spec); - EntryList out = pp.process(db, rec); + EntryList out = pp.process(db, rec); Attribute leaf_attr = db.get_attribute("leaf"); diff --git a/src/reader/test/test_snapshottableformatter.cpp b/src/reader/test/test_snapshottableformatter.cpp index 5683ccfe7..d00995235 100644 --- a/src/reader/test/test_snapshottableformatter.cpp +++ b/src/reader/test/test_snapshottableformatter.cpp @@ -10,18 +10,19 @@ using namespace cali; -TEST(SnapshotTableFormatter, Format) { +TEST(SnapshotTableFormatter, Format) +{ CaliperMetadataDB db; - Attribute a = db.create_attribute("aaaa", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - Attribute b = db.create_attribute("bb", CALI_TYPE_UINT, CALI_ATTR_ASVALUE); - Attribute s = db.create_attribute("str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute a = db.create_attribute("aaaa", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + Attribute b = db.create_attribute("bb", CALI_TYPE_UINT, CALI_ATTR_ASVALUE); + Attribute s = db.create_attribute("str", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); IdMap idmap; Node* node = db.merge_node(101, s.id(), CALI_INV_ID, Variant("a string value"), idmap); std::vector rec; - rec.push_back(Entry(a, Variant(42))); + rec.push_back(Entry(a, Variant(42))); rec.push_back(Entry(b, Variant(4242))); rec.push_back(Entry(node)); diff --git a/src/services/LoopStatistics.cpp b/src/services/LoopStatistics.cpp index 65dbb86bd..c39135e02 100644 --- a/src/services/LoopStatistics.cpp +++ b/src/services/LoopStatistics.cpp @@ -22,7 +22,7 @@ namespace cali extern Attribute class_iteration_attr; extern Attribute loop_attr; -} +} // namespace cali namespace { @@ -33,7 +33,7 @@ class LoopStatisticsService struct LoopInfo { clock::time_point iter_start_time; - uint64_t num_iterations; + uint64_t num_iterations; }; std::vector m_loop_info; @@ -41,7 +41,8 @@ class LoopStatisticsService Attribute m_iter_duration_attr; Attribute m_iter_count_attr; - void begin_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& data) { + void begin_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& data) + { if (attr == loop_attr) { m_loop_info.emplace_back(LoopInfo { clock::now(), 0 }); } else if (attr.get(class_iteration_attr).to_bool() && !m_loop_info.empty()) { @@ -50,7 +51,8 @@ class LoopStatisticsService } } - void end_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& data) { + void end_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& data) + { if (m_loop_info.empty()) return; if (attr == loop_attr) { @@ -58,8 +60,9 @@ class LoopStatisticsService c->push_snapshot(channel, SnapshotView(e)); m_loop_info.pop_back(); } else if (attr.get(class_iteration_attr).to_bool()) { - uint64_t t = std::chrono::duration_cast( - clock::now() - m_loop_info.back().iter_start_time).count(); + uint64_t t = + std::chrono::duration_cast(clock::now() - m_loop_info.back().iter_start_time) + .count(); Entry e { m_iter_duration_attr, Variant(t) }; c->push_snapshot(channel, SnapshotView(e)); } @@ -67,12 +70,16 @@ class LoopStatisticsService LoopStatisticsService(Caliper* c, Channel* channel) { - m_iter_duration_attr = - c->create_attribute("iter.duration.ns", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE | CALI_ATTR_SKIP_EVENTS); - m_iter_count_attr = - c->create_attribute("iter.count", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE | CALI_ATTR_SKIP_EVENTS); + m_iter_duration_attr = c->create_attribute( + "iter.duration.ns", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE | CALI_ATTR_SKIP_EVENTS + ); + m_iter_count_attr = c->create_attribute( + "iter.count", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE | CALI_ATTR_SKIP_EVENTS + ); m_loop_info.reserve(4); } @@ -81,21 +88,21 @@ class LoopStatisticsService static const char* s_spec; - static void create(Caliper* c, Channel* channel) { + static void create(Caliper* c, Channel* channel) + { LoopStatisticsService* instance = new LoopStatisticsService(c, channel); channel->events().pre_begin_evt.connect( - [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& data){ - instance->begin_cb(c, channel, attr, data); - }); + [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& data) { + instance->begin_cb(c, channel, attr, data); + } + ); channel->events().pre_end_evt.connect( - [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& data){ - instance->end_cb(c, channel, attr, data); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - delete instance; - }); + [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& data) { + instance->end_cb(c, channel, attr, data); + } + ); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { delete instance; }); Log(1).stream() << channel->name() << ": registered loop_statistics service\n"; } @@ -108,7 +115,7 @@ const char* LoopStatisticsService::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/Services.cpp b/src/services/Services.cpp index b95fd59e2..1f73fe886 100644 --- a/src/services/Services.cpp +++ b/src/services/Services.cpp @@ -28,7 +28,7 @@ namespace std::string get_name_from_spec(const char* name_or_spec) { // try to parse the given string as spec, otherwise return it as-is - bool ok = false; + bool ok = false; auto dict = StringConverter(name_or_spec).rec_dict(&ok); if (!ok) @@ -41,12 +41,14 @@ std::string get_name_from_spec(const char* name_or_spec) return std::string { name_or_spec }; } -class ServicesManager { +class ServicesManager +{ std::map m_services; public: - std::vector get_available_services() const { + std::vector get_available_services() const + { std::vector ret; ret.reserve(m_services.size()); @@ -56,8 +58,9 @@ class ServicesManager { return ret; } - bool register_service(const char* name, Caliper* c, Channel* channel) const { - for (const auto &p : m_services) + bool register_service(const char* name, Caliper* c, Channel* channel) const + { + for (const auto& p : m_services) if (p.first == name && p.second.register_fn) { (*p.second.register_fn)(c, channel); return true; @@ -66,26 +69,29 @@ class ServicesManager { return false; } - void add_services(const CaliperService list[]) { + void add_services(const CaliperService list[]) + { for (const CaliperService* s = list; s && s->name_or_spec && s->register_fn; ++s) m_services[get_name_from_spec(s->name_or_spec)] = *s; } - std::string get_service_description(const std::string& name) { + std::string get_service_description(const std::string& name) + { auto service_itr = m_services.find(name); if (service_itr == m_services.end()) return std::string(); - auto dict = StringConverter(service_itr->second.name_or_spec).rec_dict(); + auto dict = StringConverter(service_itr->second.name_or_spec).rec_dict(); auto spec_itr = dict.find("description"); return spec_itr != dict.end() ? spec_itr->second.to_string() : std::string(); } - std::ostream& print_service_documentation(std::ostream& os, const std::string& name) { + std::ostream& print_service_documentation(std::ostream& os, const std::string& name) + { auto service_itr = m_services.find(name); if (service_itr == m_services.end()) return os; - auto dict = StringConverter(service_itr->second.name_or_spec).rec_dict(); + auto dict = StringConverter(service_itr->second.name_or_spec).rec_dict(); auto spec_itr = dict.find("description"); if (spec_itr != dict.end()) os << ' ' << spec_itr->second.to_string() << '\n'; @@ -100,7 +106,7 @@ class ServicesManager { auto list = spec_itr->second.rec_list(); for (const auto& s : list) { auto cfg_dict = s.rec_dict(); - auto it = cfg_dict.find("name"); + auto it = cfg_dict.find("name"); if (it == cfg_dict.end()) continue; @@ -133,14 +139,14 @@ class ServicesManager { return os; } - static ServicesManager* instance() { + static ServicesManager* instance() + { static std::unique_ptr inst { new ServicesManager }; return inst.get(); } }; -} // namespace [anonymous] - +} // namespace namespace cali { @@ -155,9 +161,7 @@ bool register_service(Caliper* c, Channel* channel, const char* name) void register_configured_services(Caliper* c, Channel* channel) { - const RuntimeConfig::config_entry_list_t configdata { - { "enable", "" } - }; + const RuntimeConfig::config_entry_list_t configdata { { "enable", "" } }; std::vector services = channel->config().init("services", configdata).get("enable").to_stringlist(",:"); @@ -194,14 +198,14 @@ ConfigSet init_config_from_spec(RuntimeConfig config, const char* spec) { RuntimeConfig::config_entry_list_t list; - auto dict = StringConverter(spec).rec_dict(); + auto dict = StringConverter(spec).rec_dict(); auto spec_itr = dict.find("config"); if (spec_itr != dict.end()) { - for (const auto &e : spec_itr->second.rec_list()) { + for (const auto& e : spec_itr->second.rec_list()) { auto cfg_dict = e.rec_dict(); std::string key, val; - auto itr = cfg_dict.find("name"); + auto itr = cfg_dict.find("name"); assert(itr != cfg_dict.end() && "config entry name missing"); key = itr->second.to_string(); itr = cfg_dict.find("value"); diff --git a/src/services/Services.h b/src/services/Services.h index 9f572e3a5..d6fe5b3f4 100644 --- a/src/services/Services.h +++ b/src/services/Services.h @@ -38,12 +38,10 @@ void register_configured_services(Caliper* c, Channel* chn); ConfigSet init_config_from_spec(RuntimeConfig cfg, const char* spec); /// \brief Find and print service documentation (description and options) -std::ostream& -print_service_documentation(std::ostream& os, const std::string& name); +std::ostream& print_service_documentation(std::ostream& os, const std::string& name); /// \brief Get description string for service -std::string -get_service_description(const std::string& name); +std::string get_service_description(const std::string& name); /// \brief Get all currently available service names. std::vector get_available_services(); diff --git a/src/services/adiak/AdiakExport.cpp b/src/services/adiak/AdiakExport.cpp index 960da029b..b65582131 100644 --- a/src/services/adiak/AdiakExport.cpp +++ b/src/services/adiak/AdiakExport.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. +// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. #include "caliper/CaliperService.h" @@ -19,12 +19,11 @@ using namespace cali; namespace { -std::map< cali_id_t, std::vector > -get_caliper_globals(Caliper* c, Channel* chn) +std::map> get_caliper_globals(Caliper* c, Channel* chn) { - std::map< cali_id_t, std::vector > ret; - auto globals = c->get_globals(*chn); - + std::map> ret; + auto globals = c->get_globals(*chn); + // expand globals for (const Entry& e : globals) { @@ -39,8 +38,7 @@ get_caliper_globals(Caliper* c, Channel* chn) return ret; } -void -export_globals_to_adiak(Caliper* c, Channel* chn) +void export_globals_to_adiak(Caliper* c, Channel* chn) { auto globals_map = get_caliper_globals(c, chn); @@ -53,56 +51,53 @@ export_globals_to_adiak(Caliper* c, Channel* chn) switch (attr.type()) { case CALI_TYPE_INT: case CALI_TYPE_BOOL: - { - std::vector values(p.second.size()); - std::transform(p.second.begin(), p.second.end(), values.begin(), [](const Variant& v){ + { + std::vector values(p.second.size()); + std::transform(p.second.begin(), p.second.end(), values.begin(), [](const Variant& v) { return v.to_int(); }); - if (values.size() > 1) - adiak::value(attr.name(), values); - else - adiak::value(attr.name(), values.front()); - } - break; + if (values.size() > 1) + adiak::value(attr.name(), values); + else + adiak::value(attr.name(), values.front()); + } + break; case CALI_TYPE_UINT: - { - std::vector values(p.second.size()); - std::transform(p.second.begin(), p.second.end(), values.begin(), [](const Variant& v){ + { + std::vector values(p.second.size()); + std::transform(p.second.begin(), p.second.end(), values.begin(), [](const Variant& v) { return static_cast(v.to_uint()); }); - if (values.size() > 1) - adiak::value(attr.name(), values); - else - adiak::value(attr.name(), values.front()); - } - break; + if (values.size() > 1) + adiak::value(attr.name(), values); + else + adiak::value(attr.name(), values.front()); + } + break; default: - { - std::vector values(p.second.size()); - std::transform(p.second.begin(), p.second.end(), values.begin(), [](const Variant& v){ + { + std::vector values(p.second.size()); + std::transform(p.second.begin(), p.second.end(), values.begin(), [](const Variant& v) { return v.to_string(); }); - if (values.size() > 1) - adiak::value(attr.name(), values); - else - adiak::value(attr.name(), values.front()); - } + if (values.size() > 1) + adiak::value(attr.name(), values); + else + adiak::value(attr.name(), values.front()); + } } } } -void -register_adiak_export(Caliper* c, Channel* chn) +void register_adiak_export(Caliper* c, Channel* chn) { - chn->events().pre_flush_evt.connect( - [](Caliper* c, Channel* chn, SnapshotView){ - export_globals_to_adiak(c, chn); - }); - + chn->events().pre_flush_evt.connect([](Caliper* c, Channel* chn, SnapshotView) { export_globals_to_adiak(c, chn); } + ); + Log(1).stream() << chn->name() << ": Registered adiak_export service" << std::endl; } -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/adiak/AdiakImport.cpp b/src/services/adiak/AdiakImport.cpp index aaab1800b..176fa3f65 100644 --- a/src/services/adiak/AdiakImport.cpp +++ b/src/services/adiak/AdiakImport.cpp @@ -30,8 +30,7 @@ Attribute meta_attr[3]; unsigned s_unknown_type_error = 0; -std::ostream& -recursive_unpack(std::ostream& os, adiak_value_t *val, adiak_datatype_t* t) +std::ostream& recursive_unpack(std::ostream& os, adiak_value_t* val, adiak_datatype_t* t) { switch (t->dtype) { case adiak_date: @@ -59,11 +58,11 @@ recursive_unpack(std::ostream& os, adiak_value_t *val, adiak_datatype_t* t) break; #endif case adiak_timeval: - { - struct timeval *tval = static_cast(val->v_ptr); - os << tval->tv_sec + tval->tv_usec / 1000000.0; - } - break; + { + struct timeval* tval = static_cast(val->v_ptr); + os << tval->tv_sec + tval->tv_usec / 1000000.0; + } + break; case adiak_version: case adiak_string: case adiak_catstring: @@ -71,63 +70,63 @@ recursive_unpack(std::ostream& os, adiak_value_t *val, adiak_datatype_t* t) os << static_cast(val->v_ptr); break; case adiak_range: - { - adiak_value_t subvals[2]; - adiak_datatype_t* subtypes[2]; + { + adiak_value_t subvals[2]; + adiak_datatype_t* subtypes[2]; - adiak_get_subval(t, val, 0, subtypes+0, subvals+0); - adiak_get_subval(t, val, 1, subtypes+1, subvals+1); + adiak_get_subval(t, val, 0, subtypes + 0, subvals + 0); + adiak_get_subval(t, val, 1, subtypes + 1, subvals + 1); - recursive_unpack(os, subvals+0, *(subtypes+0)); - os << '-'; - recursive_unpack(os, subvals+1, *(subtypes+1)); - } - break; + recursive_unpack(os, subvals + 0, *(subtypes + 0)); + os << '-'; + recursive_unpack(os, subvals + 1, *(subtypes + 1)); + } + break; case adiak_set: - { - os << '['; - int n = adiak_num_subvals(t); - for (int i = 0; i < n; i++) { - adiak_value_t subval; - adiak_datatype_t* subtype; - adiak_get_subval(t, val, i, &subtype, &subval); - if (i > 0) - os << ','; - recursive_unpack(os, &subval, subtype); + { + os << '['; + int n = adiak_num_subvals(t); + for (int i = 0; i < n; i++) { + adiak_value_t subval; + adiak_datatype_t* subtype; + adiak_get_subval(t, val, i, &subtype, &subval); + if (i > 0) + os << ','; + recursive_unpack(os, &subval, subtype); + } + os << ']'; } - os << ']'; - } - break; + break; case adiak_list: - { - os << '{'; - int n = adiak_num_subvals(t); - for (int i = 0; i < n; i++) { - adiak_value_t subval; - adiak_datatype_t* subtype; - adiak_get_subval(t, val, i, &subtype, &subval); - if (i > 0) - os << ','; - recursive_unpack(os, &subval, subtype); + { + os << '{'; + int n = adiak_num_subvals(t); + for (int i = 0; i < n; i++) { + adiak_value_t subval; + adiak_datatype_t* subtype; + adiak_get_subval(t, val, i, &subtype, &subval); + if (i > 0) + os << ','; + recursive_unpack(os, &subval, subtype); + } + os << '}'; } - os << '}'; - } - break; + break; case adiak_tuple: - { - os << '('; - int n = adiak_num_subvals(t); - for (int i = 0; i < n; i++) { - adiak_value_t subval; - adiak_datatype_t* subtype; - adiak_get_subval(t, val, i, &subtype, &subval); - if (i > 0) - os << ','; - recursive_unpack(os, &subval, subtype); + { + os << '('; + int n = adiak_num_subvals(t); + for (int i = 0; i < n; i++) { + adiak_value_t subval; + adiak_datatype_t* subtype; + adiak_get_subval(t, val, i, &subtype, &subval); + if (i > 0) + os << ','; + recursive_unpack(os, &subval, subtype); + } + os << ')'; } - os << ')'; - } - break; + break; default: ++s_unknown_type_error; } @@ -135,8 +134,14 @@ recursive_unpack(std::ostream& os, adiak_value_t *val, adiak_datatype_t* t) return os; } -void -set_val(Channel& channel, const char* name, const Variant& val, adiak_datatype_t* type, adiak_category_t category, const char *subcategory) +void set_val( + Channel& channel, + const char* name, + const Variant& val, + adiak_datatype_t* type, + adiak_category_t category, + const char* subcategory +) { Caliper c; Variant v_metavals[3]; @@ -146,23 +151,28 @@ set_val(Channel& channel, const char* name, const Variant& val, adiak_datatype_t v_metavals[1] = Variant(static_cast(category)); if (!subcategory || subcategory[0] == '\0') - subcategory = "none"; + subcategory = "none"; v_metavals[2] = Variant(subcategory); Attribute attr = - c.create_attribute(name, val.type(), CALI_ATTR_GLOBAL | CALI_ATTR_SKIP_EVENTS, - 3, meta_attr, v_metavals); + c.create_attribute(name, val.type(), CALI_ATTR_GLOBAL | CALI_ATTR_SKIP_EVENTS, 3, meta_attr, v_metavals); c.set(&channel, attr, val); } struct nameval_usr_args_t { Channel channel; - int count; + int count; }; -void -nameval_cb(const char *name, adiak_category_t category, const char *subcategory, adiak_value_t *val, adiak_datatype_t *t, void* usr_args) +void nameval_cb( + const char* name, + adiak_category_t category, + const char* subcategory, + adiak_value_t* val, + adiak_datatype_t* t, + void* usr_args +) { nameval_usr_args_t* args = static_cast(usr_args); @@ -171,22 +181,22 @@ nameval_cb(const char *name, adiak_category_t category, const char *subcategory, if (!channel) return; if (category == adiak_control) - return; + return; Caliper c; switch (t->dtype) { case adiak_type_unset: - { - Attribute attr = c.get_attribute(name); + { + Attribute attr = c.get_attribute(name); - if (attr && attr.is_global()) - c.end(attr); - else - Log(0).stream() << "adiak: unset invoked for unknown key " << name << std::endl; + if (attr && attr.is_global()) + c.end(attr); + else + Log(0).stream() << "adiak: unset invoked for unknown key " << name << std::endl; - return; - } + return; + } case adiak_long: set_val(channel, name, Variant(static_cast(val->v_long)), t, category, subcategory); ++args->count; @@ -205,7 +215,14 @@ nameval_cb(const char *name, adiak_category_t category, const char *subcategory, ++args->count; break; case adiak_ulonglong: - set_val(channel, name, Variant(cali_make_variant_from_uint(static_cast(val->v_longlong))), t, category, subcategory); + set_val( + channel, + name, + Variant(cali_make_variant_from_uint(static_cast(val->v_longlong))), + t, + category, + subcategory + ); ++args->count; break; #endif @@ -222,32 +239,39 @@ nameval_cb(const char *name, adiak_category_t category, const char *subcategory, ++args->count; break; case adiak_timeval: - { - struct timeval *tval = static_cast(val->v_ptr); - set_val(channel, name, Variant(tval->tv_sec + tval->tv_usec / 1000000.0), t, category, subcategory); - ++args->count; - break; - } + { + struct timeval* tval = static_cast(val->v_ptr); + set_val(channel, name, Variant(tval->tv_sec + tval->tv_usec / 1000000.0), t, category, subcategory); + ++args->count; + break; + } case adiak_version: case adiak_string: case adiak_catstring: case adiak_path: - set_val(channel, name, Variant(CALI_TYPE_STRING, val->v_ptr, strlen(static_cast(val->v_ptr))), t, category, subcategory); + set_val( + channel, + name, + Variant(CALI_TYPE_STRING, val->v_ptr, strlen(static_cast(val->v_ptr))), + t, + category, + subcategory + ); ++args->count; break; case adiak_range: case adiak_set: case adiak_list: case adiak_tuple: - { - std::ostringstream os; - recursive_unpack(os, val, t); - std::string str = os.str(); - - set_val(channel, name, Variant(CALI_TYPE_STRING, str.c_str(), str.length()+1), t, category, subcategory); - ++args->count; - break; - } + { + std::ostringstream os; + recursive_unpack(os, val, t); + std::string str = os.str(); + + set_val(channel, name, Variant(CALI_TYPE_STRING, str.c_str(), str.length() + 1), t, category, subcategory); + ++args->count; + break; + } default: ++s_unknown_type_error; } @@ -266,37 +290,32 @@ const char* spec = R"json( } )json"; -void -register_adiak_import(Caliper* c, Channel* channel) +void register_adiak_import(Caliper* c, Channel* channel) { - ConfigSet cfg = services::init_config_from_spec(channel->config(), spec); + ConfigSet cfg = services::init_config_from_spec(channel->config(), spec); std::vector categories; for (const std::string& s : cfg.get("categories").to_stringlist()) categories.push_back(std::stoi(s)); - meta_attr[0] = - c->create_attribute("adiak.type", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - meta_attr[1] = - c->create_attribute("adiak.category", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + meta_attr[0] = c->create_attribute("adiak.type", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + meta_attr[1] = c->create_attribute("adiak.category", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); meta_attr[2] = - c->create_attribute("adiak.subcategory", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + c->create_attribute("adiak.subcategory", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - channel->events().pre_flush_evt.connect( - [categories](Caliper*, Channel* channel, SnapshotView){ - nameval_usr_args_t args { *channel, 0 }; + channel->events().pre_flush_evt.connect([categories](Caliper*, Channel* channel, SnapshotView) { + nameval_usr_args_t args { *channel, 0 }; - for (int category : categories) - adiak_list_namevals(1, static_cast(category), nameval_cb, &args); + for (int category : categories) + adiak_list_namevals(1, static_cast(category), nameval_cb, &args); - Log(1).stream() << channel->name() << ": adiak_import: Imported " << args.count - << " adiak values" << std::endl; - }); + Log(1).stream() << channel->name() << ": adiak_import: Imported " << args.count << " adiak values" << std::endl; + }); Log(1).stream() << channel->name() << ": Registered adiak_import service" << std::endl; } -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/adiak/test/test_adiak.cpp b/src/services/adiak/test/test_adiak.cpp index eb3076dcb..5e572fd01 100644 --- a/src/services/adiak/test/test_adiak.cpp +++ b/src/services/adiak/test/test_adiak.cpp @@ -13,8 +13,16 @@ using namespace cali; namespace { -void extract_cb(const char* name, adiak_category_t cat, const char* subcategory, adiak_value_t* val, adiak_datatype_t* t, void* usr_val) { - std::map *res = static_cast< std::map* >(usr_val); +void extract_cb( + const char* name, + adiak_category_t cat, + const char* subcategory, + adiak_value_t* val, + adiak_datatype_t* t, + void* usr_val +) +{ + std::map* res = static_cast*>(usr_val); switch (t->dtype) { case adiak_int: @@ -28,14 +36,11 @@ void extract_cb(const char* name, adiak_category_t cat, const char* subcategory, } } -} +} // namespace TEST(AdiakServiceTest, AdiakImport) { - cali_id_t import_chn_id = - cali::create_channel("adiak_import", 0, { - { "CALI_SERVICES_ENABLE", "adiak_import" } - }); + cali_id_t import_chn_id = cali::create_channel("adiak_import", 0, { { "CALI_SERVICES_ENABLE", "adiak_import" } }); adiak::value("import.int", 42); adiak::value("import.str", "import"); @@ -69,14 +74,10 @@ TEST(AdiakServiceTest, AdiakImport) EXPECT_EQ(c.get(&chn, vec_attr).value().to_string(), std::string("{1,4,16}")); } - #ifdef ADIAK_HAVE_LONGLONG TEST(AdiakServiceTest, AdiakImportLonglong) { - cali_id_t import_chn_id = - cali::create_channel("adiak_import", 0, { - { "CALI_SERVICES_ENABLE", "adiak_import" } - }); + cali_id_t import_chn_id = cali::create_channel("adiak_import", 0, { { "CALI_SERVICES_ENABLE", "adiak_import" } }); long long llv = -9876543210; @@ -112,11 +113,11 @@ TEST(AdiakServiceTest, AdiakImportLonglong) TEST(AdiakServiceTest, AdiakImportCategoryFilter) { - cali_id_t import_chn_id = - cali::create_channel("adiak_import", 0, { - { "CALI_SERVICES_ENABLE", "adiak_import" }, - { "CALI_ADIAK_IMPORT_CATEGORIES", "424242,12345" } - }); + cali_id_t import_chn_id = cali::create_channel( + "adiak_import", + 0, + { { "CALI_SERVICES_ENABLE", "adiak_import" }, { "CALI_ADIAK_IMPORT_CATEGORIES", "424242,12345" } } + ); adiak_namevalue("do.not.import", adiak_general, "none", "%d", 23); adiak_namevalue("do.import.1", static_cast(424242), "import.category", "%d", 42); @@ -127,8 +128,8 @@ TEST(AdiakServiceTest, AdiakImportCategoryFilter) chn.events().pre_flush_evt(&c, &chn, SnapshotView()); - Attribute do_import_attr_1 = c.get_attribute("do.import.1"); - Attribute do_import_attr_2 = c.get_attribute("do.import.2"); + Attribute do_import_attr_1 = c.get_attribute("do.import.1"); + Attribute do_import_attr_2 = c.get_attribute("do.import.2"); Attribute do_not_import_attr = c.get_attribute("do.not.import"); EXPECT_EQ(do_import_attr_1.type(), CALI_TYPE_INT); @@ -152,10 +153,7 @@ TEST(AdiakServiceTest, AdiakImportCategoryFilter) TEST(AdiakServiceTest, AdiakExport) { - cali_id_t export_chn_id = - cali::create_channel("adiak_export", 0, { - { "CALI_SERVICES_ENABLE", "adiak_export" } - }); + cali_id_t export_chn_id = cali::create_channel("adiak_export", 0, { { "CALI_SERVICES_ENABLE", "adiak_export" } }); cali_set_global_int_byname("export.int", 42); cali_set_global_string_byname("export.str", "export"); diff --git a/src/services/aggregate/Aggregate.cpp b/src/services/aggregate/Aggregate.cpp index afd338508..82631a87a 100644 --- a/src/services/aggregate/Aggregate.cpp +++ b/src/services/aggregate/Aggregate.cpp @@ -52,50 +52,48 @@ class Aggregate std::atomic stopped; std::atomic retired; - ThreadDB* next = nullptr; - ThreadDB* prev = nullptr; + ThreadDB* next = nullptr; + ThreadDB* prev = nullptr; - AggregationDB db; + AggregationDB db; - void unlink() { + void unlink() + { if (next) next->prev = prev; if (prev) prev->next = next; } - ThreadDB(Caliper* c) - : stopped(false), retired(false), db(c) - { } + ThreadDB(Caliper* c) : stopped(false), retired(false), db(c) {} }; - ConfigSet config; + ConfigSet config; - ThreadDB* tdb_list = nullptr; - util::spinlock tdb_lock; + ThreadDB* tdb_list = nullptr; + util::spinlock tdb_lock; - AttributeInfo info; - std::vector key_attribute_names; - std::vector aggr_attribute_names; + AttributeInfo info; + std::vector key_attribute_names; + std::vector aggr_attribute_names; - Attribute tdb_attr; + Attribute tdb_attr; - size_t num_dropped_snapshots; + size_t num_dropped_snapshots; - ThreadDB* acquire_tdb(Caliper* c, Channel* chn, bool can_alloc) { + ThreadDB* acquire_tdb(Caliper* c, Channel* chn, bool can_alloc) + { // we store a pointer to the thread-local aggregation DB for this channel // on the thread's blackboard - ThreadDB* tdb = - static_cast(c->get(tdb_attr).value().get_ptr()); + ThreadDB* tdb = static_cast(c->get(tdb_attr).value().get_ptr()); if (!tdb && can_alloc) { tdb = new ThreadDB(c); c->set(tdb_attr, Variant(cali_make_variant_from_ptr(tdb))); - std::lock_guard - g(tdb_lock); + std::lock_guard g(tdb_lock); if (tdb_list) tdb_list->prev = tdb; @@ -107,67 +105,69 @@ class Aggregate return tdb; } - ResultAttributes make_result_attributes(Caliper* c, const Attribute& attr) { - std::string name = attr.name(); + ResultAttributes make_result_attributes(Caliper* c, const Attribute& attr) + { + std::string name = attr.name(); ResultAttributes res; - int prop = CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS; + int prop = CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS; cali_attr_type type = attr.type(); res.min_attr = c->create_attribute(std::string("min#") + name, type, prop); res.max_attr = c->create_attribute(std::string("max#") + name, type, prop); res.sum_attr = c->create_attribute(std::string("sum#") + name, type, prop); res.avg_attr = c->create_attribute(std::string("avg#") + name, CALI_TYPE_DOUBLE, prop); - #ifdef CALIPER_ENABLE_HISTOGRAMS +#ifdef CALIPER_ENABLE_HISTOGRAMS for (int jj = 0; jj < CALI_AGG_HISTOGRAM_BINS; jj++) { - res.histogram_attr[jj] = - c->create_attribute(std::string("histogram.bin.") + std::to_string(jj) + std::string("#") + name, - CALI_TYPE_INT, prop); + res.histogram_attr[jj] = c->create_attribute( + std::string("histogram.bin.") + std::to_string(jj) + std::string("#") + name, + CALI_TYPE_INT, + prop + ); } - #endif +#endif return res; } - void check_aggregation_attribute(Caliper* c, const Attribute& attr) { + void check_aggregation_attribute(Caliper* c, const Attribute& attr) + { if (!(attr.properties() & CALI_ATTR_AGGREGATABLE)) return; - if (std::find(info.aggr_attrs.begin(), info.aggr_attrs.end(), - attr) != info.aggr_attrs.end()) + if (std::find(info.aggr_attrs.begin(), info.aggr_attrs.end(), attr) != info.aggr_attrs.end()) return; info.aggr_attrs.push_back(attr); info.result_attrs.push_back(make_result_attributes(c, attr)); } - void init_aggregation_attributes(Caliper* c) { + void init_aggregation_attributes(Caliper* c) + { auto attrs = c->find_attributes_with_prop(CALI_ATTR_AGGREGATABLE); - for (const auto &a : attrs) + for (const auto& a : attrs) check_aggregation_attribute(c, a); const int prop = CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS; - info.count_attr = - c->create_attribute("count", CALI_TYPE_UINT, prop); - info.slot_attr = - c->create_attribute("aggregate.slot", CALI_TYPE_UINT, prop); + info.count_attr = c->create_attribute("count", CALI_TYPE_UINT, prop); + info.slot_attr = c->create_attribute("aggregate.slot", CALI_TYPE_UINT, prop); } - void flush_cb(Caliper* c, Channel* chn, SnapshotFlushFn proc_fn) { + void flush_cb(Caliper* c, Channel* chn, SnapshotFlushFn proc_fn) + { ThreadDB* tdb = nullptr; { - std::lock_guard - g(tdb_lock); + std::lock_guard g(tdb_lock); tdb = tdb_list; } size_t num_written = 0; - for ( ; tdb; tdb = tdb->next) { + for (; tdb; tdb = tdb->next) { tdb->stopped.store(true); num_written += tdb->db.flush(info, c, proc_fn); tdb->stopped.store(false); @@ -176,12 +176,12 @@ class Aggregate Log(1).stream() << chn->name() << ": Aggregate: flushed " << num_written << " snapshots." << std::endl; } - void clear_cb(Caliper* c, Channel* chn) { + void clear_cb(Caliper* c, Channel* chn) + { ThreadDB* tdb = nullptr; { - std::lock_guard - g(tdb_lock); + std::lock_guard g(tdb_lock); tdb = tdb_list; } @@ -195,11 +195,11 @@ class Aggregate while (tdb) { tdb->stopped.store(true); - num_entries += tdb->db.num_entries(); - num_kernels += tdb->db.num_kernels(); + num_entries += tdb->db.num_entries(); + num_kernels += tdb->db.num_kernels(); bytes_reserved += tdb->db.bytes_reserved(); - num_dropped += tdb->db.num_dropped(); - max_hash_len = std::max(max_hash_len, tdb->db.max_hash_len()); + num_dropped += tdb->db.num_dropped(); + max_hash_len = std::max(max_hash_len, tdb->db.max_hash_len()); tdb->db.clear(); @@ -209,8 +209,7 @@ class Aggregate ThreadDB* tmp = tdb->next; { - std::lock_guard - g(tdb_lock); + std::lock_guard g(tdb_lock); tdb->unlink(); @@ -226,26 +225,21 @@ class Aggregate } if (Log::verbosity() >= 2) { - unitfmt_result bytes_reserved_fmt = - unitfmt(bytes_reserved, unitfmt_bytes); - - Log(2).stream() << chn->name() << ": Aggregate: Releasing aggregation DB.\n" - << " max hash len: " - << max_hash_len << ", " - << num_entries << " entries, " - << num_kernels << " kernels, " - << bytes_reserved_fmt.val << " " - << bytes_reserved_fmt.symbol << " reserved." - << std::endl; + unitfmt_result bytes_reserved_fmt = unitfmt(bytes_reserved, unitfmt_bytes); + + Log(2).stream() << chn->name() << ": Aggregate: Releasing aggregation DB.\n" + << " max hash len: " << max_hash_len << ", " << num_entries << " entries, " << num_kernels + << " kernels, " << bytes_reserved_fmt.val << " " << bytes_reserved_fmt.symbol + << " reserved." << std::endl; } if (num_dropped > 0) Log(1).stream() << chn->name() << ": Aggregate: " << num_dropped - << " entries dropped because aggregation buffers are full!" - << std::endl; + << " entries dropped because aggregation buffers are full!" << std::endl; } - void process_snapshot_cb(Caliper* c, Channel* chn, SnapshotView rec) { + void process_snapshot_cb(Caliper* c, Channel* chn, SnapshotView rec) + { ThreadDB* tdb = acquire_tdb(c, chn, !c->is_signal()); if (tdb && !tdb->stopped.load()) @@ -254,23 +248,19 @@ class Aggregate ++num_dropped_snapshots; } - void check_key_attribute(const Attribute& attr) { - auto it = std::find(key_attribute_names.begin(), key_attribute_names.end(), - attr.name()); + void check_key_attribute(const Attribute& attr) + { + auto it = std::find(key_attribute_names.begin(), key_attribute_names.end(), attr.name()); if (it != key_attribute_names.end()) { if (attr.store_as_value()) { cali_attr_type type = attr.type(); - if (type != CALI_TYPE_INT && - type != CALI_TYPE_UINT && - type != CALI_TYPE_ADDR && - type != CALI_TYPE_BOOL && - type != CALI_TYPE_TYPE) { + if (type != CALI_TYPE_INT && type != CALI_TYPE_UINT && type != CALI_TYPE_ADDR && type != CALI_TYPE_BOOL + && type != CALI_TYPE_TYPE) { Log(1).stream() << "Aggregate: warning: type " << cali_type2string(type) << " in as-value attribute \"" << attr.name() - << "\" is not supported in aggregation key and will be dropped." - << std::endl; + << "\" is not supported in aggregation key and will be dropped." << std::endl; return; } @@ -283,7 +273,8 @@ class Aggregate } } - void post_init_cb(Caliper* c, Channel* chn) { + void post_init_cb(Caliper* c, Channel* chn) + { // Update key attributes for (const Attribute& a : c->get_all_attributes()) check_key_attribute(a); @@ -294,35 +285,35 @@ class Aggregate acquire_tdb(c, chn, true); } - void create_attribute_cb(Caliper* c, const Attribute& attr) { + void create_attribute_cb(Caliper* c, const Attribute& attr) + { check_key_attribute(attr); check_aggregation_attribute(c, attr); } - void create_thread_cb(Caliper* c, Channel* chn) { - acquire_tdb(c, chn, true); - } + void create_thread_cb(Caliper* c, Channel* chn) { acquire_tdb(c, chn, true); } - void release_thread_cb(Caliper* c, Channel* chn) { + void release_thread_cb(Caliper* c, Channel* chn) + { ThreadDB* tdb = acquire_tdb(c, chn, false); if (tdb) tdb->retired.store(true); } - void finish_cb(Caliper* c, Channel* chn) { + void finish_cb(Caliper* c, Channel* chn) + { // report attribute keys we haven't found for (const std::string& s : key_attribute_names) - Log(1).stream() << chn->name() << ": Aggregate: warning: key attribute \"" - << s - << "\" unused" << std::endl; + Log(1).stream() << chn->name() << ": Aggregate: warning: key attribute \"" << s << "\" unused" << std::endl; if (num_dropped_snapshots > 0) - Log(1).stream() << chn->name() << ": Aggregate: dropped " << num_dropped_snapshots - << " snapshots." << std::endl; + Log(1).stream() << chn->name() << ": Aggregate: dropped " << num_dropped_snapshots << " snapshots." + << std::endl; } - void apply_key_config() { + void apply_key_config() + { if (key_attribute_names.empty()) { info.implicit_grouping = true; return; @@ -357,33 +348,31 @@ class Aggregate } } - Aggregate(Caliper* c, Channel* chn) - : num_dropped_snapshots(0) - { - config = services::init_config_from_spec(chn->config(), s_spec); + Aggregate(Caliper* c, Channel* chn) : num_dropped_snapshots(0) + { + config = services::init_config_from_spec(chn->config(), s_spec); - tdb_lock.unlock(); + tdb_lock.unlock(); - info.implicit_grouping = true; - info.group_nested = false; - - key_attribute_names = config.get("key").to_stringlist(","); - apply_key_config(); - - tdb_attr = - c->create_attribute(std::string("aggregate.tdb.") + std::to_string(chn->id()), - CALI_TYPE_PTR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_HIDDEN); - } + info.implicit_grouping = true; + info.group_nested = false; + + key_attribute_names = config.get("key").to_stringlist(","); + apply_key_config(); + + tdb_attr = c->create_attribute( + std::string("aggregate.tdb.") + std::to_string(chn->id()), + CALI_TYPE_PTR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN + ); + } public: static const char* s_spec; - ~Aggregate() { + ~Aggregate() + { ThreadDB* tdb = tdb_list; while (tdb) { @@ -399,43 +388,32 @@ class Aggregate } } - static void aggregate_register(Caliper* c, Channel* chn) { + static void aggregate_register(Caliper* c, Channel* chn) + { Aggregate* instance = new Aggregate(c, chn); - chn->events().create_attr_evt.connect( - [instance](Caliper* c, Channel*, const Attribute& attr){ - instance->create_attribute_cb(c, attr); - }); - chn->events().post_init_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->post_init_cb(c, chn); - }); - chn->events().create_thread_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->create_thread_cb(c, chn); - }); - chn->events().release_thread_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->release_thread_cb(c, chn); - }); - chn->events().process_snapshot.connect( - [instance](Caliper* c, Channel* chn, SnapshotView, SnapshotView rec){ - instance->process_snapshot_cb(c, chn, rec); - }); - chn->events().flush_evt.connect( - [instance](Caliper* c, Channel* chn, SnapshotView, SnapshotFlushFn proc_fn){ - instance->flush_cb(c, chn, proc_fn); - }); - chn->events().clear_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->clear_cb(c, chn); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->clear_cb(c, chn); // prints logs - instance->finish_cb(c, chn); - delete instance; - }); + chn->events().create_attr_evt.connect([instance](Caliper* c, Channel*, const Attribute& attr) { + instance->create_attribute_cb(c, attr); + }); + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* chn) { instance->post_init_cb(c, chn); }); + chn->events().create_thread_evt.connect([instance](Caliper* c, Channel* chn) { + instance->create_thread_cb(c, chn); + }); + chn->events().release_thread_evt.connect([instance](Caliper* c, Channel* chn) { + instance->release_thread_cb(c, chn); + }); + chn->events().process_snapshot.connect([instance](Caliper* c, Channel* chn, SnapshotView, SnapshotView rec) { + instance->process_snapshot_cb(c, chn, rec); + }); + chn->events().flush_evt.connect([instance](Caliper* c, Channel* chn, SnapshotView, SnapshotFlushFn proc_fn) { + instance->flush_cb(c, chn, proc_fn); + }); + chn->events().clear_evt.connect([instance](Caliper* c, Channel* chn) { instance->clear_cb(c, chn); }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->clear_cb(c, chn); // prints logs + instance->finish_cb(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered aggregation service" << std::endl; } @@ -454,7 +432,7 @@ const char* Aggregate::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/aggregate/AggregationDB.cpp b/src/services/aggregate/AggregationDB.cpp index 6e86150b0..3b5cb8557 100644 --- a/src/services/aggregate/AggregationDB.cpp +++ b/src/services/aggregate/AggregationDB.cpp @@ -30,25 +30,27 @@ struct AggregateKernel { #ifdef CALIPER_ENABLE_HISTOGRAMS int histogram_max; - int histogram[CALI_AGG_HISTOGRAM_BINS] = {0}; + int histogram[CALI_AGG_HISTOGRAM_BINS] = { 0 }; // Quick way to get expoent out of a double. struct getExponent { - union { - double val; - std::uint16_t sh[4]; - }; + union { + double val; + std::uint16_t sh[4]; + }; }; #endif AggregateKernel() : count(0) #ifdef CALIPER_ENABLE_HISTOGRAMS - , histogram_max(0) + , + histogram_max(0) #endif - { } + {} - inline void update(const Variant& val) { + inline void update(const Variant& val) + { Variant::update_minmaxsum(val, min, max, sum); ++count; @@ -63,23 +65,23 @@ struct AggregateKernel { //powers of 4 for ease of documentation, we need the bias to //be 1024. //making bins of size 4x, which means dividing exponent by 2. - int exponent = (val_uint+1)/2; + int exponent = (val_uint + 1) / 2; if (exponent > histogram_max) { //shift down values as necessary. - int shift = std::min(exponent - histogram_max,CALI_AGG_HISTOGRAM_BINS-1); - for (int ii=1; ii m_entries; std::vector m_keyents; @@ -112,7 +112,8 @@ struct AggregationDB::AggregationDBImpl // --- // - Node* make_key_node(Caliper* c, SnapshotView rec, const std::vector& ref_key_attrs) { + Node* make_key_node(Caliper* c, SnapshotView rec, const std::vector& ref_key_attrs) + { Node* key_node = &m_aggr_root_node; for (const Attribute& attr : ref_key_attrs) { @@ -122,13 +123,13 @@ struct AggregationDB::AggregationDBImpl continue; cali_id_t attr_id = attr.id(); - size_t count = 0; + size_t count = 0; for (const Node* node = e.node(); node; node = node->parent()) if (node->attribute() == attr_id) ++count; - const Node* *node_vec = static_cast(alloca(count * sizeof(const Node*))); + const Node** node_vec = static_cast(alloca(count * sizeof(const Node*))); memset(node_vec, 0, count * sizeof(const Node*)); size_t n = count; @@ -146,14 +147,15 @@ struct AggregationDB::AggregationDBImpl return key_node == &m_aggr_root_node ? nullptr : key_node; } - AggregateEntry* find_or_create_entry(SnapshotView key, std::size_t hash, std::size_t num_aggr_attrs, bool can_alloc) { - hash = hash % m_hashmap.size(); + AggregateEntry* find_or_create_entry(SnapshotView key, std::size_t hash, std::size_t num_aggr_attrs, bool can_alloc) + { + hash = hash % m_hashmap.size(); size_t key_len = key.size(); - size_t count = 0; + size_t count = 0; for (std::size_t idx = m_hashmap[hash]; idx != 0; idx = m_entries[idx].next_entry_idx) { AggregateEntry* e = &m_entries[idx]; - if (key_len == e->key_len && std::equal(key.begin(), key.end(), m_keyents.begin()+e->key_idx)) + if (key_len == e->key_len && std::equal(key.begin(), key.end(), m_keyents.begin() + e->key_idx)) return e; ++count; } @@ -189,19 +191,20 @@ struct AggregationDB::AggregationDBImpl m_entries.push_back(e); m_hashmap[hash] = entry_idx; - m_max_hash_len = std::max(m_max_hash_len, count+1); + m_max_hash_len = std::max(m_max_hash_len, count + 1); return &m_entries[entry_idx]; } - void process_snapshot(Caliper* c, SnapshotView rec, const AttributeInfo& info) { + void process_snapshot(Caliper* c, SnapshotView rec, const AttributeInfo& info) + { if (rec.empty()) return; // --- extract key entries FixedSizeSnapshotRecord key; - std::size_t hash = 0; + std::size_t hash = 0; if (info.implicit_grouping) { for (const Entry& e : rec) @@ -252,7 +255,8 @@ struct AggregationDB::AggregationDBImpl } } - void clear() { + void clear() + { m_hashmap.assign(m_hashmap.size(), 0); m_entries.resize(1); m_kernels.resize(0); @@ -261,7 +265,8 @@ struct AggregationDB::AggregationDBImpl m_entries[0].count = 0; } - size_t flush(const AttributeInfo& info, Caliper* c, SnapshotFlushFn proc_fn) { + size_t flush(const AttributeInfo& info, Caliper* c, SnapshotFlushFn proc_fn) + { size_t num_written = 0; for (const AggregateEntry& entry : m_entries) { @@ -271,7 +276,7 @@ struct AggregationDB::AggregationDBImpl SnapshotView kv(entry.key_len, &m_keyents[entry.key_idx]); std::vector rec; - rec.reserve(kv.size() + 4*entry.num_kernels + 2); + rec.reserve(kv.size() + 4 * entry.num_kernels + 2); std::copy(kv.begin(), kv.end(), std::back_inserter(rec)); @@ -287,11 +292,14 @@ struct AggregationDB::AggregationDBImpl rec.push_back(Entry(info.result_attrs[a].max_attr, Variant(k->max))); rec.push_back(Entry(info.result_attrs[a].sum_attr, Variant(k->sum))); rec.push_back(Entry(info.result_attrs[a].avg_attr, Variant(avg))); - #ifdef CALIPER_ENABLE_HISTOGRAMS - for (int ii=0; iihistogram[ii])))); +#ifdef CALIPER_ENABLE_HISTOGRAMS + for (int ii = 0; ii < CALI_AGG_HISTOGRAM_BINS; ii++) { + rec.push_back(Entry( + info.stats_attributes[a].histogram_attr[ii], + Variant(cali_make_variant_from_uint(k->histogram[ii])) + )); } - #endif +#endif } rec.push_back(Entry(info.count_attr, cali_make_variant_from_uint(entry.count))); @@ -305,95 +313,81 @@ struct AggregationDB::AggregationDBImpl return num_written; } - AggregationDBImpl(Caliper* c) - : m_aggr_root_node(CALI_INV_ID, CALI_INV_ID, Variant()), - m_max_hash_len(0) - { - m_kernels.reserve(16384); - m_keyents.reserve(16384); - m_entries.reserve(4096); - m_hashmap.assign(8192, static_cast(0)); + AggregationDBImpl(Caliper* c) : m_aggr_root_node(CALI_INV_ID, CALI_INV_ID, Variant()), m_max_hash_len(0) + { + m_kernels.reserve(16384); + m_keyents.reserve(16384); + m_entries.reserve(4096); + m_hashmap.assign(8192, static_cast(0)); - Attribute attr = - c->create_attribute("skipped.records", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - Node* node = - c->make_tree_entry(attr, Variant("SKIPPED"), &m_aggr_root_node); + Attribute attr = + c->create_attribute("skipped.records", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + Node* node = c->make_tree_entry(attr, Variant("SKIPPED"), &m_aggr_root_node); - m_keyents.push_back(Entry(node)); + m_keyents.push_back(Entry(node)); - AggregateEntry e; + AggregateEntry e; - e.count = 0; - e.key_idx = 0; - e.key_len = 1; - e.kernels_idx = 0; - e.num_kernels = 0; - e.next_entry_idx = 0; + e.count = 0; + e.key_idx = 0; + e.key_len = 1; + e.kernels_idx = 0; + e.num_kernels = 0; + e.next_entry_idx = 0; - m_entries.push_back(e); - } + m_entries.push_back(e); + } }; // // --- AggregationDB public interface // -AggregationDB::AggregationDB(Caliper* c) - : mP(new AggregationDBImpl(c)) -{ } +AggregationDB::AggregationDB(Caliper* c) : mP(new AggregationDBImpl(c)) +{} AggregationDB::~AggregationDB() { mP.reset(); } -void -AggregationDB::process_snapshot(Caliper* c, SnapshotView rec, const AttributeInfo& info) +void AggregationDB::process_snapshot(Caliper* c, SnapshotView rec, const AttributeInfo& info) { mP->process_snapshot(c, rec, info); } -void -AggregationDB::clear() +void AggregationDB::clear() { mP->clear(); } -size_t -AggregationDB::flush(const AttributeInfo& info, Caliper* c, SnapshotFlushFn proc_fn) +size_t AggregationDB::flush(const AttributeInfo& info, Caliper* c, SnapshotFlushFn proc_fn) { return mP->flush(info, c, proc_fn); } -size_t -AggregationDB::num_dropped() const +size_t AggregationDB::num_dropped() const { return mP->m_entries[0].count; } -size_t -AggregationDB::max_hash_len() const +size_t AggregationDB::max_hash_len() const { return mP->m_max_hash_len; } -size_t -AggregationDB::num_entries() const +size_t AggregationDB::num_entries() const { return mP->m_entries.size(); } -size_t -AggregationDB::num_kernels() const +size_t AggregationDB::num_kernels() const { return mP->m_kernels.size(); } -size_t -AggregationDB::bytes_reserved() const +size_t AggregationDB::bytes_reserved() const { - return mP->m_hashmap.capacity() * sizeof(size_t) - + mP->m_kernels.capacity() * sizeof(AggregateKernel) - + mP->m_keyents.capacity() * sizeof(Entry) - + mP->m_entries.capacity() * sizeof(AggregateEntry); + return mP->m_hashmap.capacity() * sizeof(size_t) + mP->m_kernels.capacity() * sizeof(AggregateKernel) + + mP->m_keyents.capacity() * sizeof(Entry) + mP->m_entries.capacity() * sizeof(AggregateEntry); } diff --git a/src/services/aggregate/AggregationDB.h b/src/services/aggregate/AggregationDB.h index 044255313..be5f5157e 100644 --- a/src/services/aggregate/AggregationDB.h +++ b/src/services/aggregate/AggregationDB.h @@ -13,13 +13,12 @@ #include #include -#define CALI_AGG_HISTOGRAM_BINS 10 +#define CALI_AGG_HISTOGRAM_BINS 10 namespace aggregate { -struct ResultAttributes -{ +struct ResultAttributes { cali::Attribute min_attr; cali::Attribute max_attr; cali::Attribute sum_attr; @@ -29,17 +28,16 @@ struct ResultAttributes #endif }; -struct AttributeInfo -{ - std::vector ref_key_attrs; - std::vector imm_key_attrs; +struct AttributeInfo { + std::vector ref_key_attrs; + std::vector imm_key_attrs; - std::vector aggr_attrs; + std::vector aggr_attrs; std::vector result_attrs; - cali::Attribute count_attr; - cali::Attribute slot_attr; + cali::Attribute count_attr; + cali::Attribute slot_attr; bool implicit_grouping; bool group_nested; @@ -60,7 +58,7 @@ class AggregationDB ~AggregationDB(); - void process_snapshot(cali::Caliper*, cali::SnapshotView, const AttributeInfo&); + void process_snapshot(cali::Caliper*, cali::SnapshotView, const AttributeInfo&); void clear(); size_t flush(const AttributeInfo&, cali::Caliper*, cali::SnapshotFlushFn); diff --git a/src/services/alloc/AllocService.cpp b/src/services/alloc/AllocService.cpp index 2dcd3f721..738da17db 100644 --- a/src/services/alloc/AllocService.cpp +++ b/src/services/alloc/AllocService.cpp @@ -39,31 +39,30 @@ namespace #define MAX_ADDRESS_ATTRIBUTES 4 struct AllocInfo { - uint64_t start_addr; - uint64_t total_size; - Variant v_uid; - size_t elem_size; - size_t num_elems; - - cali::Node* alloc_label_node; - cali::Node* free_label_node; - cali::Node* addr_label_nodes[MAX_ADDRESS_ATTRIBUTES]; - - size_t index_1D(uint64_t addr) const { - return (addr - start_addr) / elem_size; - } + uint64_t start_addr; + uint64_t total_size; + Variant v_uid; + size_t elem_size; + size_t num_elems; + + cali::Node* alloc_label_node; + cali::Node* free_label_node; + cali::Node* addr_label_nodes[MAX_ADDRESS_ATTRIBUTES]; + + size_t index_1D(uint64_t addr) const { return (addr - start_addr) / elem_size; } }; /// Three-way predicate to tell if given address is within AllocInfo's address range, less, or higher -class ContainsAddress { +class ContainsAddress +{ uint64_t address; public: - ContainsAddress(uint64_t a) - : address(a) { } + ContainsAddress(uint64_t a) : address(a) {} - inline int operator ()(const AllocInfo& info) const { + inline int operator() (const AllocInfo& info) const + { if (address < info.start_addr) return -1; else if (address >= info.start_addr && address < info.start_addr + info.total_size) @@ -74,15 +73,16 @@ class ContainsAddress { }; /// Three-way predicate to tell if given address is AllocInfo's start address, less, or higher -class HasStartAddress { +class HasStartAddress +{ uint64_t address; public: - HasStartAddress(uint64_t a) - : address(a) { } + HasStartAddress(uint64_t a) : address(a) {} - inline int operator ()(const AllocInfo& info) const { + inline int operator() (const AllocInfo& info) const + { if (address < info.start_addr) return -1; else if (address == info.start_addr) @@ -93,7 +93,8 @@ class HasStartAddress { }; struct AllocInfoCmp { - int operator ()(const AllocInfo& lhs, const AllocInfo& rhs) const { + int operator() (const AllocInfo& lhs, const AllocInfo& rhs) const + { if (lhs.start_addr < rhs.start_addr) return -1; else if (lhs.start_addr == rhs.start_addr) @@ -104,12 +105,12 @@ struct AllocInfoCmp { class AllocService { - bool g_resolve_addresses { false }; - bool g_track_allocations { true }; - bool g_record_active_mem { false }; - bool g_record_highwatermark { false }; + bool g_resolve_addresses { false }; + bool g_track_allocations { true }; + bool g_record_active_mem { false }; + bool g_record_highwatermark { false }; -// DataTracker attributes + // DataTracker attributes Attribute mem_alloc_attr; Attribute mem_free_attr; Attribute alloc_uid_attr; @@ -121,7 +122,7 @@ class AllocService Attribute region_hwm_attr; -// Derived attributes for class.memoryaddress attributes + // Derived attributes for class.memoryaddress attributes struct alloc_attrs { Attribute memoryaddress_attr; Attribute alloc_label_attr; @@ -129,44 +130,55 @@ class AllocService Attribute alloc_index_attr; }; - std::atomic g_alloc_uid { 0 }; + std::atomic g_alloc_uid { 0 }; std::vector g_memoryaddress_attrs; - cali::Node g_alloc_root_node { CALI_INV_ID, CALI_INV_ID, Variant() }; + cali::Node g_alloc_root_node { CALI_INV_ID, CALI_INV_ID, Variant() }; util::SplayTree g_tree; - std::mutex g_tree_lock; - - uint64_t g_active_mem { 0 }; - uint64_t g_hwm { 0 }; - uint64_t g_region_hwm { 0 }; - std::mutex g_hwm_lock; - - unsigned long g_current_tracked { 0 }; - unsigned long g_max_tracked { 0 }; - unsigned long g_total_tracked { 0 }; - unsigned g_failed_untrack { 0 }; - - void track_mem_snapshot(Caliper* c, - Channel* chn, - cali::Node* label_node, - const Variant& v_size, - const Variant& v_uid, - const Variant& v_addr) { - Entry data[] = { - { alloc_total_size_attr, v_size }, - { alloc_uid_attr, v_uid }, - { alloc_addr_attr, v_addr }, - { label_node } - }; + std::mutex g_tree_lock; + + uint64_t g_active_mem { 0 }; + uint64_t g_hwm { 0 }; + uint64_t g_region_hwm { 0 }; + std::mutex g_hwm_lock; + + unsigned long g_current_tracked { 0 }; + unsigned long g_max_tracked { 0 }; + unsigned long g_total_tracked { 0 }; + unsigned g_failed_untrack { 0 }; + + void track_mem_snapshot( + Caliper* c, + Channel* chn, + cali::Node* label_node, + const Variant& v_size, + const Variant& v_uid, + const Variant& v_addr + ) + { + Entry data[] = { { alloc_total_size_attr, v_size }, + { alloc_uid_attr, v_uid }, + { alloc_addr_attr, v_addr }, + { label_node } }; c->push_snapshot(chn, SnapshotView(4, data)); } - void track_mem_cb(Caliper* c, Channel* chn, const void* ptr, const char* label, size_t elem_size, size_t ndims, const size_t* dims, - size_t nextra, const Attribute* extra_attrs, const Variant* extra_vals) { - size_t total_size = - std::accumulate(dims, dims+ndims, elem_size, std::multiplies()); + void track_mem_cb( + Caliper* c, + Channel* chn, + const void* ptr, + const char* label, + size_t elem_size, + size_t ndims, + const size_t* dims, + size_t nextra, + const Attribute* extra_attrs, + const Variant* extra_vals + ) + { + size_t total_size = std::accumulate(dims, dims + ndims, elem_size, std::multiplies()); AllocInfo info; @@ -183,10 +195,8 @@ class AllocService for (size_t i = 0; i < nextra; ++i) root_node = c->make_tree_entry(extra_attrs[i], extra_vals[i], root_node); - info.alloc_label_node = - c->make_tree_entry(mem_alloc_attr, v_label, root_node); - info.free_label_node = - c->make_tree_entry(mem_free_attr, v_label, root_node); + info.alloc_label_node = c->make_tree_entry(mem_alloc_attr, v_label, root_node); + info.free_label_node = c->make_tree_entry(mem_free_attr, v_label, root_node); std::fill_n(info.addr_label_nodes, MAX_ADDRESS_ATTRIBUTES, nullptr); @@ -196,37 +206,39 @@ class AllocService } if (g_track_allocations) - track_mem_snapshot(c, chn, info.alloc_label_node, - Variant(static_cast(total_size)), - info.v_uid, - Variant(CALI_TYPE_ADDR, &ptr, sizeof(void*))); + track_mem_snapshot( + c, + chn, + info.alloc_label_node, + Variant(static_cast(total_size)), + info.v_uid, + Variant(CALI_TYPE_ADDR, &ptr, sizeof(void*)) + ); { - std::lock_guard - g(g_hwm_lock); + std::lock_guard g(g_hwm_lock); - g_active_mem += total_size; - g_hwm = std::max(g_hwm, g_active_mem); - g_region_hwm = std::max(g_region_hwm, g_active_mem); + g_active_mem += total_size; + g_hwm = std::max(g_hwm, g_active_mem); + g_region_hwm = std::max(g_region_hwm, g_active_mem); } { - std::lock_guard - g(g_tree_lock); + std::lock_guard g(g_tree_lock); g_tree.insert(info); - g_max_tracked = std::max(++g_current_tracked, g_max_tracked); + g_max_tracked = std::max(++g_current_tracked, g_max_tracked); ++g_total_tracked; } } - void untrack_mem_cb(Caliper* c, Channel* chn, const void* ptr) { + void untrack_mem_cb(Caliper* c, Channel* chn, const void* ptr) + { AllocInfo info; { - std::lock_guard - g(g_tree_lock); + std::lock_guard g(g_tree_lock); auto tree_node = g_tree.find(HasStartAddress(reinterpret_cast(ptr))); @@ -242,20 +254,24 @@ class AllocService } if (g_track_allocations) - track_mem_snapshot(c, chn, info.free_label_node, - Variant(-static_cast(info.total_size)), - info.v_uid, - Variant(CALI_TYPE_ADDR, &ptr, sizeof(void*))); + track_mem_snapshot( + c, + chn, + info.free_label_node, + Variant(-static_cast(info.total_size)), + info.v_uid, + Variant(CALI_TYPE_ADDR, &ptr, sizeof(void*)) + ); { - std::lock_guard - g(g_hwm_lock); + std::lock_guard g(g_hwm_lock); g_active_mem -= info.total_size; } } - void resolve_addresses(Caliper* c, const SnapshotView trigger_info, SnapshotBuilder& snapshot) { + void resolve_addresses(Caliper* c, const SnapshotView trigger_info, SnapshotBuilder& snapshot) + { for (int i = 0; i < std::min(g_memoryaddress_attrs.size(), MAX_ADDRESS_ATTRIBUTES); ++i) { Entry e = trigger_info.get(g_memoryaddress_attrs[i].memoryaddress_attr); @@ -268,18 +284,18 @@ class AllocService cali::Node* label_node = nullptr; { - std::lock_guard - g(g_tree_lock); + std::lock_guard g(g_tree_lock); auto tree_node = g_tree.find(ContainsAddress(addr)); if (!tree_node) continue; - data[0] = Entry(g_memoryaddress_attrs[i].alloc_uid_attr, - (*tree_node).v_uid); - data[1] = Entry(g_memoryaddress_attrs[i].alloc_index_attr, - cali_make_variant_from_uint((*tree_node).index_1D(addr))); + data[0] = Entry(g_memoryaddress_attrs[i].alloc_uid_attr, (*tree_node).v_uid); + data[1] = Entry( + g_memoryaddress_attrs[i].alloc_index_attr, + cali_make_variant_from_uint((*tree_node).index_1D(addr)) + ); label_node = (*tree_node).addr_label_nodes[i]; } @@ -291,12 +307,12 @@ class AllocService } } - void record_highwatermark(Caliper* c, Channel* chn, SnapshotBuilder& rec) { + void record_highwatermark(Caliper* c, Channel* chn, SnapshotBuilder& rec) + { uint64_t hwm = 0; { - std::lock_guard - g(g_hwm_lock); + std::lock_guard g(g_hwm_lock); hwm = g_region_hwm; g_region_hwm = g_active_mem; @@ -305,7 +321,8 @@ class AllocService rec.append(region_hwm_attr, Variant(hwm)); } - void snapshot_cb(Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot) { + void snapshot_cb(Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot) + { // Record currently active amount of allocated memory if (g_record_active_mem) snapshot.append(active_mem_attr, Variant(cali_make_variant_from_uint(g_active_mem))); @@ -317,150 +334,170 @@ class AllocService record_highwatermark(c, chn, snapshot); } - void make_address_attributes(Caliper* c, const Attribute& attr) { - struct alloc_attrs attrs = { - attr, - c->create_attribute("alloc.label#" + attr.name(), CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS), - c->create_attribute("alloc.uid#" + attr.name(), CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS), - c->create_attribute("alloc.index#" + attr.name(), CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS) - }; + void make_address_attributes(Caliper* c, const Attribute& attr) + { + struct alloc_attrs attrs = { attr, + c->create_attribute( + "alloc.label#" + attr.name(), + CALI_TYPE_STRING, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS + ), + c->create_attribute( + "alloc.uid#" + attr.name(), + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ), + c->create_attribute( + "alloc.index#" + attr.name(), + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ) }; if (g_memoryaddress_attrs.size() >= MAX_ADDRESS_ATTRIBUTES) - Log(1).stream() << "alloc: Can't perform lookup for more than " - << MAX_ADDRESS_ATTRIBUTES - << " attributes. Skipping " - << attr.name() << std::endl; + Log(1).stream() << "alloc: Can't perform lookup for more than " << MAX_ADDRESS_ATTRIBUTES + << " attributes. Skipping " << attr.name() << std::endl; else g_memoryaddress_attrs.push_back(attrs); } - void create_attr_cb(Caliper* c, const Attribute& attr) { + void create_attr_cb(Caliper* c, const Attribute& attr) + { // Note: this isn't threadsafe! if (attr.get(class_memoryaddress_attr).to_bool() == true) make_address_attributes(c, attr); } - void post_init_cb(Caliper *c, Channel* chn) { + void post_init_cb(Caliper* c, Channel* chn) + { if (!g_resolve_addresses) return; - std::vector address_attrs = - c->find_attributes_with(c->get_attribute("class.memoryaddress")); + std::vector address_attrs = c->find_attributes_with(c->get_attribute("class.memoryaddress")); for (auto attr : address_attrs) make_address_attributes(c, attr); - chn->events().create_attr_evt.connect( - [this](Caliper* c, Channel* chn, const Attribute& attr){ - this->create_attr_cb(c, attr); - }); + chn->events().create_attr_evt.connect([this](Caliper* c, Channel* chn, const Attribute& attr) { + this->create_attr_cb(c, attr); + }); } - void finish_cb(Caliper* c, Channel* chn) { - Log(1).stream() << chn->name() << ": alloc: " - << g_total_tracked << " memory allocations tracked (max " - << g_max_tracked << " simultaneous), " - << g_failed_untrack << " untrack lookups failed." + void finish_cb(Caliper* c, Channel* chn) + { + Log(1).stream() << chn->name() << ": alloc: " << g_total_tracked << " memory allocations tracked (max " + << g_max_tracked << " simultaneous), " << g_failed_untrack << " untrack lookups failed." << std::endl; } AllocService(Caliper* c, Channel* chn) - { - struct attr_info_t { - const char* name; - cali_attr_type type; - int prop; - int meta_count; - Attribute* meta_attr; - Variant* meta_vals; - Attribute* attr; - } attr_info[] = { - { "mem.alloc", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, - 0, nullptr, nullptr, - &mem_alloc_attr - }, - { "mem.free" , CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, - 0, nullptr, nullptr, - &mem_free_attr - }, - { "mem.active" , CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE, - 0, nullptr, nullptr, - &active_mem_attr - }, - { "alloc.uid", CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE, - 0, nullptr, nullptr, - &alloc_uid_attr - }, - { "alloc.address", CALI_TYPE_ADDR, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE, - 0, nullptr, nullptr, - &alloc_addr_attr - }, - { "alloc.elem_size", CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD, - 0, nullptr, nullptr, - &alloc_elem_size_attr - }, - { "alloc.num_elems", CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD, - 0, nullptr, nullptr, - &alloc_num_elems_attr - }, - { "alloc.total_size", CALI_TYPE_INT, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE, - 0, nullptr, nullptr, - &alloc_total_size_attr - }, - { "alloc.region.highwatermark", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE, - 0, nullptr, nullptr, - ®ion_hwm_attr - }, - { 0, CALI_TYPE_INV, CALI_ATTR_DEFAULT, 0, nullptr, nullptr, nullptr } - }; - - for (attr_info_t *p = attr_info; p->name; ++p) - *(p->attr) = c->create_attribute(p->name, p->type, p->prop | CALI_ATTR_SKIP_EVENTS, p->meta_count, p->meta_attr, p->meta_vals); - - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - - g_resolve_addresses = config.get("resolve_addresses").to_bool(); - g_track_allocations = config.get("track_allocations").to_bool(); - g_record_active_mem = config.get("record_active_mem").to_bool(); - g_record_highwatermark = config.get("record_highwatermark").to_bool(); - } + { + struct attr_info_t { + const char* name; + cali_attr_type type; + int prop; + int meta_count; + Attribute* meta_attr; + Variant* meta_vals; + Attribute* attr; + } attr_info[] = { + { "mem.alloc", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, 0, nullptr, nullptr, &mem_alloc_attr }, + { "mem.free", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, 0, nullptr, nullptr, &mem_free_attr }, + { "mem.active", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE, + 0, + nullptr, + nullptr, + &active_mem_attr }, + { "alloc.uid", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE, + 0, + nullptr, + nullptr, + &alloc_uid_attr }, + { "alloc.address", + CALI_TYPE_ADDR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE, + 0, + nullptr, + nullptr, + &alloc_addr_attr }, + { "alloc.elem_size", CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD, 0, nullptr, nullptr, &alloc_elem_size_attr }, + { "alloc.num_elems", CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD, 0, nullptr, nullptr, &alloc_num_elems_attr }, + { "alloc.total_size", + CALI_TYPE_INT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE, + 0, + nullptr, + nullptr, + &alloc_total_size_attr }, + { "alloc.region.highwatermark", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE, + 0, + nullptr, + nullptr, + ®ion_hwm_attr }, + { 0, CALI_TYPE_INV, CALI_ATTR_DEFAULT, 0, nullptr, nullptr, nullptr } + }; + + for (attr_info_t* p = attr_info; p->name; ++p) + *(p->attr) = c->create_attribute( + p->name, + p->type, + p->prop | CALI_ATTR_SKIP_EVENTS, + p->meta_count, + p->meta_attr, + p->meta_vals + ); + + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); + + g_resolve_addresses = config.get("resolve_addresses").to_bool(); + g_track_allocations = config.get("track_allocations").to_bool(); + g_record_active_mem = config.get("record_active_mem").to_bool(); + g_record_highwatermark = config.get("record_highwatermark").to_bool(); + } public: static const char* s_spec; - static void allocservice_initialize(Caliper* c, Channel* chn) { + static void allocservice_initialize(Caliper* c, Channel* chn) + { AllocService* instance = new AllocService(c, chn); - chn->events().track_mem_evt.connect( - [instance](Caliper* c, Channel* chn, const void* ptr, const char* label, size_t elem_size, size_t ndims, const size_t* dims, size_t n, const Attribute* attrs, const Variant* vals){ - instance->track_mem_cb(c, chn, ptr, label, elem_size, ndims, dims, n, attrs, vals); - }); - chn->events().untrack_mem_evt.connect( - [instance](Caliper* c, Channel* chn, const void* ptr){ - instance->untrack_mem_cb(c, chn, ptr); - }); + chn->events().track_mem_evt.connect([instance]( + Caliper* c, + Channel* chn, + const void* ptr, + const char* label, + size_t elem_size, + size_t ndims, + const size_t* dims, + size_t n, + const Attribute* attrs, + const Variant* vals + ) { + instance->track_mem_cb(c, chn, ptr, label, elem_size, ndims, dims, n, attrs, vals); + }); + chn->events().untrack_mem_evt.connect([instance](Caliper* c, Channel* chn, const void* ptr) { + instance->untrack_mem_cb(c, chn, ptr); + }); if (instance->g_resolve_addresses || instance->g_record_active_mem || instance->g_record_highwatermark) chn->events().snapshot.connect( - [instance](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot){ + [instance](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot) { instance->snapshot_cb(c, chn, info, snapshot); - }); - - chn->events().post_init_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->post_init_cb(c, chn); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finish_cb(c, chn); - delete instance; - }); + } + ); + + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* chn) { instance->post_init_cb(c, chn); }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finish_cb(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered alloc service" << std::endl; } @@ -494,7 +531,7 @@ const char* AllocService::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/alloc/SplayTree.hpp b/src/services/alloc/SplayTree.hpp index 38235ecff..3a5a67588 100644 --- a/src/services/alloc/SplayTree.hpp +++ b/src/services/alloc/SplayTree.hpp @@ -6,34 +6,32 @@ namespace util { -template -class SplayTree { - enum HAND { - LEFT = -1, NA = 0, RIGHT = 1 - }; +template +class SplayTree +{ + enum HAND { LEFT = -1, NA = 0, RIGHT = 1 }; struct STNode { STNode* parent; STNode* left; STNode* right; - T val; + T val; - HAND handedness; + HAND handedness; - STNode(STNode* p, const T& v, HAND h) - : parent(p), left(nullptr), right(nullptr), val(v), handedness(h) - { } + STNode(STNode* p, const T& v, HAND h) : parent(p), left(nullptr), right(nullptr), val(v), handedness(h) {} - STNode* insert(const T& v) { + STNode* insert(const T& v) + { Cmp cmp; if (cmp(v, val) < 0) { if (left) return left->insert(v); else { - left = new STNode(this, v, LEFT); + left = new STNode(this, v, LEFT); return left; - } + } } else if (cmp(v, val) > 0) { if (right) return right->insert(v); @@ -47,19 +45,21 @@ class SplayTree { } } - template - STNode* find(const ThreeWayPred& p) { + template + STNode* find(const ThreeWayPred& p) + { int res = p(val); - if (res < 0) - return left ? left->find(p) : nullptr; - else if (res > 0) + if (res < 0) + return left ? left->find(p) : nullptr; + else if (res > 0) return right ? right->find(p) : nullptr; return this; } - STNode* findMax() { + STNode* findMax() + { if (right) return right->findMax(); return this; @@ -68,43 +68,46 @@ class SplayTree { STNode* m_root; - void rotate_left(STNode *node) { + void rotate_left(STNode* node) + { if (node->left) { - node->left->parent = node->parent; + node->left->parent = node->parent; node->left->handedness = RIGHT; } - STNode *npp = node->parent->parent; - HAND np_hand = node->parent->handedness; + STNode* npp = node->parent->parent; + HAND np_hand = node->parent->handedness; - node->parent->parent = node; + node->parent->parent = node; node->parent->handedness = LEFT; - node->parent->right = node->left; + node->parent->right = node->left; - node->left = node->parent; - node->parent = npp; + node->left = node->parent; + node->parent = npp; node->handedness = (npp ? np_hand : NA); } - void rotate_right(STNode *node) { + void rotate_right(STNode* node) + { if (node->right) { - node->right->parent = node->parent; + node->right->parent = node->parent; node->right->handedness = LEFT; } - STNode *npp = node->parent->parent; - HAND np_hand = node->parent->handedness; + STNode* npp = node->parent->parent; + HAND np_hand = node->parent->handedness; - node->parent->parent = node; + node->parent->parent = node; node->parent->handedness = RIGHT; - node->parent->left = node->right; + node->parent->left = node->right; - node->right = node->parent; - node->parent = npp; + node->right = node->parent; + node->parent = npp; node->handedness = (npp ? np_hand : NA); } - void splay(STNode *node) { + void splay(STNode* node) + { while (node->parent) { if (node->parent->parent == nullptr) { // Case: single zig @@ -138,20 +141,16 @@ class SplayTree { m_root = node; } - SplayTree(STNode* p) - : m_root(p) - { } - + SplayTree(STNode* p) : m_root(p) {} + public: - - constexpr SplayTree() - : m_root(nullptr) - { } - ~SplayTree() - { } + constexpr SplayTree() : m_root(nullptr) {} + + ~SplayTree() {} - void insert(const T& v) { + void insert(const T& v) + { if (!m_root) m_root = new STNode(nullptr, v, NA); else { @@ -160,25 +159,26 @@ class SplayTree { } } - void remove(SplayTree& ref) { + void remove(SplayTree& ref) + { STNode* node = ref.m_root; splay(node); - SplayTree ltree(node->left); - + SplayTree ltree(node->left); + if (ltree) { - ltree.m_root->parent = nullptr; + ltree.m_root->parent = nullptr; ltree.m_root->handedness = NA; - STNode *lMax = ltree.m_root->findMax(); + STNode* lMax = ltree.m_root->findMax(); ltree.splay(lMax); - m_root = lMax; + m_root = lMax; m_root->right = node->right; if (m_root->right) m_root->right->parent = m_root; } else if (node->right) { - m_root = node->right; - m_root->parent = nullptr; + m_root = node->right; + m_root->parent = nullptr; m_root->handedness = NA; } else { m_root = nullptr; @@ -187,23 +187,20 @@ class SplayTree { delete node; } - template - SplayTree find(const ThreeWayPred& p) { + template + SplayTree find(const ThreeWayPred& p) + { STNode* ret = nullptr; if (m_root) ret = m_root->find(p); - - return SplayTree(ret); - } - T& operator * () { - return m_root->val; + return SplayTree(ret); } - operator bool () const { - return m_root != nullptr; - } + T& operator* () { return m_root->val; } + + operator bool () const { return m_root != nullptr; } }; -} +} // namespace util diff --git a/src/services/callpath/Callpath.cpp b/src/services/callpath/Callpath.cpp index a0fdc449d..a6cb21b5b 100644 --- a/src/services/callpath/Callpath.cpp +++ b/src/services/callpath/Callpath.cpp @@ -28,7 +28,7 @@ #endif #define MAX_PATH 40 -#define NAMELEN 100 +#define NAMELEN 100 using namespace cali; using namespace std; @@ -42,25 +42,26 @@ class Callpath Attribute callpath_addr_attr; Attribute ucursor_attr; - bool use_name { false }; - bool use_addr { false }; - bool skip_internal { false }; + bool use_name { false }; + bool use_addr { false }; + bool skip_internal { false }; - unsigned skip_frames { 0 }; + unsigned skip_frames { 0 }; - Node callpath_root_node; + Node callpath_root_node; uintptr_t caliper_start_addr { 0 }; - uintptr_t caliper_end_addr { 0 }; + uintptr_t caliper_end_addr { 0 }; - void snapshot_cb(Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot) { + void snapshot_cb(Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot) + { Variant v_addr[MAX_PATH]; Variant v_name[MAX_PATH]; - char strbuf[MAX_PATH][NAMELEN]; + char strbuf[MAX_PATH][NAMELEN]; // Init unwind context - unw_cursor_t unw_cursor; + unw_cursor_t unw_cursor; Entry e; if (ucursor_attr) @@ -70,11 +71,11 @@ class Callpath } else { unw_context_t unw_ctx; - #ifdef __aarch64__ +#ifdef __aarch64__ unw_getcontext(unw_ctx); - #else +#else unw_getcontext(&unw_ctx); - #endif +#endif if (unw_init_local(&unw_cursor, &unw_ctx) < 0) { Log(0).stream() << "callpath: unable to init libunwind cursor" << endl; @@ -103,8 +104,8 @@ class Callpath // store path from top to bottom if (use_addr) { - uint64_t uint = ip; - v_addr[MAX_PATH-(n+1)] = Variant(CALI_TYPE_ADDR, &uint, sizeof(uint64_t)); + uint64_t uint = ip; + v_addr[MAX_PATH - (n + 1)] = Variant(CALI_TYPE_ADDR, &uint, sizeof(uint64_t)); } if (use_name) { unw_word_t offs; @@ -112,7 +113,7 @@ class Callpath if (unw_get_proc_name(&unw_cursor, strbuf[n], NAMELEN, &offs) < 0) strncpy(strbuf[n], "UNKNOWN", NAMELEN); - v_name[MAX_PATH-(n+1)] = Variant(CALI_TYPE_STRING, strbuf[n], strlen(strbuf[n])); + v_name[MAX_PATH - (n + 1)] = Variant(CALI_TYPE_STRING, strbuf[n], strlen(strbuf[n])); } ++n; @@ -121,22 +122,23 @@ class Callpath if (n > 0) { if (use_addr) snapshot.append( - Entry(c->make_tree_entry(callpath_addr_attr, n, v_addr+(MAX_PATH-n), - &callpath_root_node))); + Entry(c->make_tree_entry(callpath_addr_attr, n, v_addr + (MAX_PATH - n), &callpath_root_node)) + ); if (use_name) snapshot.append( - Entry(c->make_tree_entry(callpath_name_attr, n, v_name+(MAX_PATH-n), - &callpath_root_node))); + Entry(c->make_tree_entry(callpath_name_attr, n, v_name + (MAX_PATH - n), &callpath_root_node)) + ); } } - void get_caliper_module_addresses() { + void get_caliper_module_addresses() + { #ifdef CALIPER_HAVE_LIBDW // initialize dwarf - char* debuginfopath = nullptr; + char* debuginfopath = nullptr; Dwfl_Callbacks callbacks; - callbacks.find_elf = dwfl_linux_proc_find_elf; + callbacks.find_elf = dwfl_linux_proc_find_elf; callbacks.find_debuginfo = dwfl_standard_find_debuginfo; callbacks.debuginfo_path = &debuginfopath; @@ -160,82 +162,74 @@ class Callpath unw_word_t ip; unw_get_reg(&unw_cursor, UNW_REG_IP, &ip); - Dwfl_Module* mod = dwfl_addrmodule(dwfl, ip); - Dwarf_Addr start = 0; - Dwarf_Addr end = 0; + Dwfl_Module* mod = dwfl_addrmodule(dwfl, ip); + Dwarf_Addr start = 0; + Dwarf_Addr end = 0; dwfl_module_info(mod, nullptr, &start, &end, nullptr, nullptr, nullptr, nullptr); caliper_start_addr = start; - caliper_end_addr = end; + caliper_end_addr = end; if (Log::verbosity() >= 2) { std::ostringstream os; os << std::hex << caliper_start_addr << ":" << caliper_end_addr; - Log(2).stream() << "callpath: skipping internal caliper frames (" - << os.str() << ")" - << std::endl; + Log(2).stream() << "callpath: skipping internal caliper frames (" << os.str() << ")" << std::endl; } dwfl_end(dwfl); #endif } - void post_init_evt(Caliper* c, Channel*) { - ucursor_attr = c->get_attribute("cali.unw_cursor"); - } + void post_init_evt(Caliper* c, Channel*) { ucursor_attr = c->get_attribute("cali.unw_cursor"); } + + Callpath(Caliper* c, Channel* chn) : callpath_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) + { + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - Callpath(Caliper* c, Channel* chn) - : callpath_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) - { - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - - use_name = config.get("use_name").to_bool(); - use_addr = config.get("use_address").to_bool(); - skip_frames = config.get("skip_frames").to_uint(); - skip_internal = config.get("skip_internal").to_bool(); - - Attribute symbol_class_attr = c->get_attribute("class.symboladdress"); - Variant v_true(true); - - callpath_addr_attr = - c->create_attribute("callpath.address", CALI_TYPE_ADDR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS, - 1, &symbol_class_attr, &v_true); - callpath_name_attr = - c->create_attribute("callpath.regname", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS); + use_name = config.get("use_name").to_bool(); + use_addr = config.get("use_address").to_bool(); + skip_frames = config.get("skip_frames").to_uint(); + skip_internal = config.get("skip_internal").to_bool(); + + Attribute symbol_class_attr = c->get_attribute("class.symboladdress"); + Variant v_true(true); + + callpath_addr_attr = c->create_attribute( + "callpath.address", + CALI_TYPE_ADDR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS, + 1, + &symbol_class_attr, + &v_true + ); + callpath_name_attr = + c->create_attribute("callpath.regname", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); #ifdef CALIPER_HAVE_LIBDW - if (skip_internal) - get_caliper_module_addresses(); + if (skip_internal) + get_caliper_module_addresses(); #else - skip_internal = false; + skip_internal = false; #endif - } + } public: static const char* s_spec; - static void callpath_service_register(Caliper* c, Channel* chn) { + static void callpath_service_register(Caliper* c, Channel* chn) + { Callpath* instance = new Callpath(c, chn); - chn->events().post_init_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->post_init_evt(c, chn); - }); + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* chn) { instance->post_init_evt(c, chn); }); chn->events().snapshot.connect( - [instance](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot){ + [instance](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& snapshot) { instance->snapshot_cb(c, chn, info, snapshot); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - delete instance; - }); + } + ); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { delete instance; }); Log(1).stream() << chn->name() << ": Registered callpath service" << std::endl; } @@ -270,7 +264,7 @@ const char* Callpath::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/cpuinfo/CpuInfo.cpp b/src/services/cpuinfo/CpuInfo.cpp index 9de8dcdfb..1d26b662c 100644 --- a/src/services/cpuinfo/CpuInfo.cpp +++ b/src/services/cpuinfo/CpuInfo.cpp @@ -34,24 +34,23 @@ void snapshot_cb(Caliper*, Channel*, SnapshotView, SnapshotBuilder& rec) void cpuinfo_register(Caliper* c, Channel* chn) { - cpu_attr = - c->create_attribute("cpuinfo.cpu", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_SCOPE_THREAD); - node_attr = - c->create_attribute("cpuinfo.numa_node", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_SCOPE_THREAD); + cpu_attr = c->create_attribute( + "cpuinfo.cpu", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_SCOPE_THREAD + ); + node_attr = c->create_attribute( + "cpuinfo.numa_node", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_SCOPE_THREAD + ); chn->events().snapshot.connect(snapshot_cb); Log(1).stream() << chn->name() << ": Registered cpuinfo service" << std::endl; } -} // namespace [anonymous] - +} // namespace namespace cali { diff --git a/src/services/craypat/CrayPAT.cpp b/src/services/craypat/CrayPAT.cpp index 0f4c5a471..56ac57325 100644 --- a/src/services/craypat/CrayPAT.cpp +++ b/src/services/craypat/CrayPAT.cpp @@ -22,10 +22,9 @@ using namespace cali; extern "C" { -void *sbrk(__intptr_t increment); -void __pat_init (int, const char *[ ], const char *[ ], const void *, const void *); -void __pat_exit (void); - +void* sbrk(__intptr_t increment); +void __pat_init(int, const char*[], const char*[], const void*, const void*); +void __pat_exit(void); } namespace @@ -36,7 +35,7 @@ std::vector read_cmdline() std::vector ret; std::ifstream f("/proc/self/cmdline"); - std::string s; + std::string s; while (f) { char c = f.get(); @@ -61,24 +60,25 @@ class CrayPATBinding : public AnnotationBinding public: - void on_begin(Caliper*, Channel*, const Attribute &attr, const Variant& value) { + void on_begin(Caliper*, Channel*, const Attribute& attr, const Variant& value) + { if (attr.is_nested() && attr.type() == CALI_TYPE_STRING) PAT_region_push(static_cast(value.data())); } - void on_end(Caliper*, Channel*, const Attribute& attr, const Variant& value) { + void on_end(Caliper*, Channel*, const Attribute& attr, const Variant& value) + { if (attr.is_nested() && attr.type() == CALI_TYPE_STRING) PAT_region_pop(static_cast(value.data())); } - void initialize(Caliper*, Channel* channel) { + void initialize(Caliper*, Channel* channel) + { m_args = read_cmdline(); if (m_args.empty()) { - Log(0).stream() - << channel->name() - << ": craypat: Unable to initialize CrayPAT: cannot read command line" - << std::endl; + Log(0).stream() << channel->name() << ": craypat: Unable to initialize CrayPAT: cannot read command line" + << std::endl; return; } @@ -92,12 +92,19 @@ class CrayPATBinding : public AnnotationBinding putenv(const_cast("PAT_RT_CALLSTACK_MODE=trace")); putenv(const_cast("PAT_RT_EXPERIMENT=trace")); - __pat_init(static_cast(m_argv.size()-1), m_argv.data(), const_cast(environ), __builtin_frame_address (0), sbrk (0L)); + __pat_init( + static_cast(m_argv.size() - 1), + m_argv.data(), + const_cast(environ), + __builtin_frame_address(0), + sbrk(0L) + ); Log(1).stream() << channel->name() << ": craypat: CrayPAT initialized\n"; } - void finalize(Caliper*, Channel* channel) { + void finalize(Caliper*, Channel* channel) + { Log(1).stream() << channel->name() << ": craypat: Closing CrayPAT\n"; __pat_exit(); } @@ -105,7 +112,7 @@ class CrayPATBinding : public AnnotationBinding const char* service_tag() const { return "craypat"; } }; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/cupti/Cupti.cpp b/src/services/cupti/Cupti.cpp index e1d61a9ce..1e88c02df 100644 --- a/src/services/cupti/Cupti.cpp +++ b/src/services/cupti/Cupti.cpp @@ -45,7 +45,7 @@ class CuptiService struct CallbackDomainInfo { CUpti_CallbackDomain domain; - const char* name; + const char* name; }; static const CallbackDomainInfo s_callback_domains[]; @@ -62,18 +62,18 @@ class CuptiService Attribute device_attr; Attribute stream_attr; - bool record_symbol; - } cupti_info; + bool record_symbol; + } cupti_info; CUpti_SubscriberHandle subscriber; - unsigned num_cb; - unsigned num_api_cb; - unsigned num_resource_cb; - unsigned num_sync_cb; - unsigned num_nvtx_cb; + unsigned num_cb; + unsigned num_api_cb; + unsigned num_resource_cb; + unsigned num_sync_cb; + unsigned num_nvtx_cb; - Cupti::EventSampling event_sampling; + Cupti::EventSampling event_sampling; Channel channel; @@ -81,8 +81,7 @@ class CuptiService // --- Helper functions // - static void - print_cupti_error(std::ostream& os, CUptiResult err, const char* func) + static void print_cupti_error(std::ostream& os, CUptiResult err, const char* func) { const char* errstr; @@ -95,14 +94,13 @@ class CuptiService // --- CUPTI Callback handling // - void - handle_stream_event(CUcontext context, CUstream stream, const Attribute& name_attr, const Variant& v_name) + void handle_stream_event(CUcontext context, CUstream stream, const Attribute& name_attr, const Variant& v_name) { - uint32_t context_id = 0; - uint32_t stream_id = 0; - uint32_t device_id = 0; + uint32_t context_id = 0; + uint32_t stream_id = 0; + uint32_t device_id = 0; - if (cuptiGetDeviceId(context, &device_id) != CUPTI_SUCCESS) + if (cuptiGetDeviceId(context, &device_id) != CUPTI_SUCCESS) return; if (cuptiGetContextId(context, &context_id) != CUPTI_SUCCESS) return; @@ -110,49 +108,42 @@ class CuptiService if (cuptiGetStreamId(context, stream, &stream_id) != CUPTI_SUCCESS) return; - Attribute attr[4] = { cupti_info.device_attr, - cupti_info.context_attr, - cupti_info.stream_attr, - name_attr }; + Attribute attr[4] = { cupti_info.device_attr, cupti_info.context_attr, cupti_info.stream_attr, name_attr }; Variant vals[4] = { Variant(static_cast(device_id)), Variant(static_cast(context_id)), Variant(static_cast(stream_id)), v_name }; FixedSizeSnapshotRecord<4> trigger_info; - Caliper c; + Caliper c; c.make_record(4, attr, vals, trigger_info.builder()); c.push_snapshot(&channel, trigger_info.view()); } - void - handle_context_event(CUcontext context, const Attribute& name_attr, const Variant& v_name) + void handle_context_event(CUcontext context, const Attribute& name_attr, const Variant& v_name) { - uint32_t context_id = 0; - uint32_t device_id = 0; + uint32_t context_id = 0; + uint32_t device_id = 0; - if (cuptiGetDeviceId(context, &device_id) != CUPTI_SUCCESS) + if (cuptiGetDeviceId(context, &device_id) != CUPTI_SUCCESS) return; if (cuptiGetContextId(context, &context_id) != CUPTI_SUCCESS) return; - Attribute attr[3] = { cupti_info.device_attr, - cupti_info.context_attr, - name_attr }; + Attribute attr[3] = { cupti_info.device_attr, cupti_info.context_attr, name_attr }; Variant vals[3] = { Variant(static_cast(device_id)), Variant(static_cast(context_id)), v_name }; FixedSizeSnapshotRecord<4> trigger_info; - Caliper c; + Caliper c; c.make_record(3, attr, vals, trigger_info.builder()); c.push_snapshot(&channel, trigger_info.view()); } - void - handle_resource(CUpti_CallbackIdResource cbid, CUpti_ResourceData* cbInfo) + void handle_resource(CUpti_CallbackIdResource cbid, CUpti_ResourceData* cbInfo) { ++num_resource_cb; @@ -161,47 +152,45 @@ class CuptiService if (event_sampling.is_enabled()) event_sampling.enable_sampling_for_context(cbInfo->context); - handle_context_event(cbInfo->context, - cupti_info.resource_attr, - Variant("create_context")); + handle_context_event(cbInfo->context, cupti_info.resource_attr, Variant("create_context")); break; case CUPTI_CBID_RESOURCE_CONTEXT_DESTROY_STARTING: if (event_sampling.is_enabled()) event_sampling.disable_sampling_for_context(cbInfo->context); - handle_context_event(cbInfo->context, - cupti_info.resource_attr, - Variant("destroy_context")); + handle_context_event(cbInfo->context, cupti_info.resource_attr, Variant("destroy_context")); break; case CUPTI_CBID_RESOURCE_STREAM_CREATED: - handle_stream_event(cbInfo->context, cbInfo->resourceHandle.stream, - cupti_info.resource_attr, - Variant("create_stream")); + handle_stream_event( + cbInfo->context, + cbInfo->resourceHandle.stream, + cupti_info.resource_attr, + Variant("create_stream") + ); break; case CUPTI_CBID_RESOURCE_STREAM_DESTROY_STARTING: - handle_stream_event(cbInfo->context, cbInfo->resourceHandle.stream, - cupti_info.resource_attr, - Variant("destroy_stream")); + handle_stream_event( + cbInfo->context, + cbInfo->resourceHandle.stream, + cupti_info.resource_attr, + Variant("destroy_stream") + ); break; - default: - ; + default:; } } - void - handle_synchronize(CUpti_CallbackIdSync cbid, CUpti_SynchronizeData* cbInfo) + void handle_synchronize(CUpti_CallbackIdSync cbid, CUpti_SynchronizeData* cbInfo) { ++num_sync_cb; switch (cbid) { case CUPTI_CBID_SYNCHRONIZE_STREAM_SYNCHRONIZED: - handle_stream_event(cbInfo->context, cbInfo->stream, - cupti_info.sync_attr, Variant("stream")); + handle_stream_event(cbInfo->context, cbInfo->stream, cupti_info.sync_attr, Variant("stream")); break; case CUPTI_CBID_SYNCHRONIZE_CONTEXT_SYNCHRONIZED: - handle_context_event(cbInfo->context, - cupti_info.sync_attr, Variant("context")); + handle_context_event(cbInfo->context, cupti_info.sync_attr, Variant("context")); break; default: // Do nothing @@ -209,8 +198,7 @@ class CuptiService } } - void - handle_callback(CUpti_CallbackId cbid, CUpti_CallbackData* cbInfo, const Attribute& attr) + void handle_callback(CUpti_CallbackId cbid, CUpti_CallbackData* cbInfo, const Attribute& attr) { ++num_api_cb; @@ -231,8 +219,7 @@ class CuptiService } } - void - handle_nvtx(CUpti_CallbackId cbid, CUpti_NvtxData* cbInfo) + void handle_nvtx(CUpti_CallbackId cbid, CUpti_NvtxData* cbInfo) { ++num_nvtx_cb; @@ -242,48 +229,41 @@ class CuptiService switch (cbid) { case CUPTI_CBID_NVTX_nvtxRangePushA: - { - const char* msg = - static_cast(p)->message; + { + const char* msg = static_cast(p)->message; - c.begin(cupti_info.nvtx_range_attr, Variant(msg)); - } - break; + c.begin(cupti_info.nvtx_range_attr, Variant(msg)); + } + break; case CUPTI_CBID_NVTX_nvtxRangePushEx: - { - const char* msg = - static_cast(p)->eventAttrib->message.ascii; + { + const char* msg = static_cast(p)->eventAttrib->message.ascii; - c.begin(cupti_info.nvtx_range_attr, Variant(msg)); - } - break; + c.begin(cupti_info.nvtx_range_attr, Variant(msg)); + } + break; case CUPTI_CBID_NVTX_nvtxRangePop: c.end(cupti_info.nvtx_range_attr); break; case CUPTI_CBID_NVTX_nvtxDomainRangePushEx: - { - // TODO: Use domain-specific attribute + { + // TODO: Use domain-specific attribute - const char* msg = - static_cast(p)->core.eventAttrib->message.ascii; + const char* msg = static_cast(p)->core.eventAttrib->message.ascii; - c.begin(cupti_info.nvtx_range_attr, Variant(msg)); - } - break; + c.begin(cupti_info.nvtx_range_attr, Variant(msg)); + } + break; case CUPTI_CBID_NVTX_nvtxDomainRangePop: // TODO: Use domain-specific attribute c.end(cupti_info.nvtx_range_attr); break; - default: - ; + default:; } } static void CUPTIAPI - cupti_callback(void* userdata, - CUpti_CallbackDomain domain, - CUpti_CallbackId cbid, - void* cbInfo) + cupti_callback(void* userdata, CUpti_CallbackDomain domain, CUpti_CallbackId cbid, void* cbInfo) { CuptiService* instance = static_cast(userdata); @@ -291,20 +271,23 @@ class CuptiService switch (domain) { case CUPTI_CB_DOMAIN_RESOURCE: - instance->handle_resource(static_cast(cbid), - static_cast(cbInfo)); + instance->handle_resource( + static_cast(cbid), + static_cast(cbInfo) + ); break; case CUPTI_CB_DOMAIN_SYNCHRONIZE: - instance->handle_synchronize(static_cast(cbid), - static_cast(cbInfo)); + instance->handle_synchronize( + static_cast(cbid), + static_cast(cbInfo) + ); break; case CUPTI_CB_DOMAIN_RUNTIME_API: - instance->handle_callback(cbid, static_cast(cbInfo), - instance->cupti_info.runtime_attr); + instance + ->handle_callback(cbid, static_cast(cbInfo), instance->cupti_info.runtime_attr); break; case CUPTI_CB_DOMAIN_DRIVER_API: - instance->handle_callback(cbid, static_cast(cbInfo), - instance->cupti_info.driver_attr); + instance->handle_callback(cbid, static_cast(cbInfo), instance->cupti_info.driver_attr); break; case CUPTI_CB_DOMAIN_NVTX: instance->handle_nvtx(cbid, static_cast(cbInfo)); @@ -316,100 +299,71 @@ class CuptiService return; } - void - snapshot_cb(Caliper* c, SnapshotBuilder& snapshot) - { - event_sampling.snapshot(c, snapshot); - } + void snapshot_cb(Caliper* c, SnapshotBuilder& snapshot) { event_sampling.snapshot(c, snapshot); } - void - pre_finish_cb() { + void pre_finish_cb() + { cuptiUnsubscribe(subscriber); cuptiFinalize(); event_sampling.stop_all(); } - void - finish_cb(Caliper* c, Channel* chn) + void finish_cb(Caliper* c, Channel* chn) { if (Log::verbosity() >= 2) { - Log(2).stream() << chn->name() << ": Cupti: processed " - << num_api_cb << " API callbacks, " - << num_resource_cb << " resource callbacks, " - << num_sync_cb << " sync callbacks, " - << num_nvtx_cb << " nvtx callbacks (" - << num_cb << " total)." - << std::endl; + Log(2).stream() << chn->name() << ": Cupti: processed " << num_api_cb << " API callbacks, " + << num_resource_cb << " resource callbacks, " << num_sync_cb << " sync callbacks, " + << num_nvtx_cb << " nvtx callbacks (" << num_cb << " total)." << std::endl; if (event_sampling.is_enabled()) event_sampling.print_statistics(Log(2).stream()); } } - void - subscribe_attributes(Caliper* c, Channel* channel) + void subscribe_attributes(Caliper* c, Channel* channel) { channel->events().subscribe_attribute(c, channel, cupti_info.runtime_attr); channel->events().subscribe_attribute(c, channel, cupti_info.driver_attr); channel->events().subscribe_attribute(c, channel, cupti_info.nvtx_range_attr); } - void - create_attributes(Caliper* c) + void create_attributes(Caliper* c) { Attribute subs_attr = c->get_attribute("subscription_event"); - Variant v_true(true); + Variant v_true(true); cupti_info.runtime_attr = - c->create_attribute("cupti.runtimeAPI", CALI_TYPE_STRING, - CALI_ATTR_NESTED, - 1, &subs_attr, &v_true); + c->create_attribute("cupti.runtimeAPI", CALI_TYPE_STRING, CALI_ATTR_NESTED, 1, &subs_attr, &v_true); cupti_info.driver_attr = - c->create_attribute("cupti.driverAPI", CALI_TYPE_STRING, - CALI_ATTR_NESTED, - 1, &subs_attr, &v_true); - cupti_info.resource_attr = - c->create_attribute("cupti.resource", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - cupti_info.sync_attr = - c->create_attribute("cupti.sync", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + c->create_attribute("cupti.driverAPI", CALI_TYPE_STRING, CALI_ATTR_NESTED, 1, &subs_attr, &v_true); + cupti_info.resource_attr = c->create_attribute("cupti.resource", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + cupti_info.sync_attr = c->create_attribute("cupti.sync", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); cupti_info.nvtx_range_attr = - c->create_attribute("nvtx.range", CALI_TYPE_STRING, - CALI_ATTR_NESTED, - 1, &subs_attr, &v_true); - - cupti_info.context_attr = - c->create_attribute("cupti.contextID", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - cupti_info.symbol_attr = - c->create_attribute("cupti.symbolName", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - cupti_info.device_attr = - c->create_attribute("cupti.deviceID", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - cupti_info.stream_attr = - c->create_attribute("cupti.streamID", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + c->create_attribute("nvtx.range", CALI_TYPE_STRING, CALI_ATTR_NESTED, 1, &subs_attr, &v_true); + + cupti_info.context_attr = c->create_attribute("cupti.contextID", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + cupti_info.symbol_attr = c->create_attribute("cupti.symbolName", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + cupti_info.device_attr = c->create_attribute("cupti.deviceID", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + cupti_info.stream_attr = c->create_attribute("cupti.streamID", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); } - bool - register_callback_domains() + bool register_callback_domains() { - CUptiResult res = - cuptiSubscribe(&subscriber, - (CUpti_CallbackFunc) cupti_callback, - this); + CUptiResult res = cuptiSubscribe(&subscriber, (CUpti_CallbackFunc) cupti_callback, this); if (res != CUPTI_SUCCESS) { print_cupti_error(Log(0).stream(), res, "cuptiSubscribe"); return false; } - std::vector cb_domain_names = - config.get("callback_domains").to_stringlist(",:"); + std::vector cb_domain_names = config.get("callback_domains").to_stringlist(",:"); // add "resource" domain when event sampling is enabled - if (event_sampling.is_enabled() && - std::find(cb_domain_names.begin(), cb_domain_names.end(), - "resource") == cb_domain_names.end()) { + if (event_sampling.is_enabled() + && std::find(cb_domain_names.begin(), cb_domain_names.end(), "resource") == cb_domain_names.end()) { Log(1).stream() << "cupti: Event sampling requires resource callbacks, " - "adding \"resource\" callback domain." + "adding \"resource\" callback domain." << std::endl; cb_domain_names.push_back("resource"); @@ -418,12 +372,11 @@ class CuptiService for (const std::string& s : cb_domain_names) { const CallbackDomainInfo* cbinfo = s_callback_domains; - for ( ; cbinfo->name && s != cbinfo->name; ++cbinfo) + for (; cbinfo->name && s != cbinfo->name; ++cbinfo) ; if (!cbinfo->name) { - Log(0).stream() << "cupti: warning: Unknown callback domain \"" - << s << "\"" << std::endl; + Log(0).stream() << "cupti: warning: Unknown callback domain \"" << s << "\"" << std::endl; continue; } @@ -435,9 +388,7 @@ class CuptiService return false; } - Log(2).stream() << "cupti: enabled \"" - << cbinfo->name << "\" callback domain." - << std::endl; + Log(2).stream() << "cupti: enabled \"" << cbinfo->name << "\" callback domain." << std::endl; } } @@ -452,22 +403,21 @@ class CuptiService num_sync_cb(0), num_nvtx_cb(0), channel(*chn) - { - cupti_info.record_symbol = config.get("record_symbol").to_bool(); + { + cupti_info.record_symbol = config.get("record_symbol").to_bool(); - uint64_t sample_event_id = config.get("sample_event_id").to_uint(); - if (sample_event_id > 0) - event_sampling.setup(c, static_cast(sample_event_id)); + uint64_t sample_event_id = config.get("sample_event_id").to_uint(); + if (sample_event_id > 0) + event_sampling.setup(c, static_cast(sample_event_id)); - create_attributes(c); - } + create_attributes(c); + } public: static const char* s_spec; - static void - cuptiservice_initialize(Caliper* c, Channel* chn) + static void cuptiservice_initialize(Caliper* c, Channel* chn) { CuptiService* instance = new CuptiService(c, chn); @@ -475,25 +425,19 @@ class CuptiService return; if (instance->event_sampling.is_enabled()) - chn->events().snapshot.connect( - [instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec){ - instance->snapshot_cb(c, rec); - }); - - chn->events().post_init_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->subscribe_attributes(c, channel); - }); - chn->events().pre_finish_evt.connect( - [instance](Caliper*, Channel*){ - instance->pre_finish_cb(); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finish_cb(c, chn); - delete instance; + chn->events().snapshot.connect([instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec) { + instance->snapshot_cb(c, rec); }); + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* channel) { + instance->subscribe_attributes(c, channel); + }); + chn->events().pre_finish_evt.connect([instance](Caliper*, Channel*) { instance->pre_finish_cb(); }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finish_cb(c, chn); + delete instance; + }); + Log(1).stream() << chn->name() << ": Registered cupti service" << std::endl; } @@ -526,15 +470,14 @@ const char* CuptiService::s_spec = R"json( } )json"; -const CuptiService::CallbackDomainInfo CuptiService::s_callback_domains[] = { - { CUPTI_CB_DOMAIN_RUNTIME_API, "runtime" }, - { CUPTI_CB_DOMAIN_DRIVER_API, "driver" }, - { CUPTI_CB_DOMAIN_RESOURCE, "resource" }, - { CUPTI_CB_DOMAIN_SYNCHRONIZE, "sync" }, - { CUPTI_CB_DOMAIN_NVTX, "nvtx" }, - { CUPTI_CB_DOMAIN_INVALID, "none" }, - { CUPTI_CB_DOMAIN_INVALID, 0 } -}; +const CuptiService::CallbackDomainInfo CuptiService::s_callback_domains[] = { { CUPTI_CB_DOMAIN_RUNTIME_API, + "runtime" }, + { CUPTI_CB_DOMAIN_DRIVER_API, "driver" }, + { CUPTI_CB_DOMAIN_RESOURCE, "resource" }, + { CUPTI_CB_DOMAIN_SYNCHRONIZE, "sync" }, + { CUPTI_CB_DOMAIN_NVTX, "nvtx" }, + { CUPTI_CB_DOMAIN_INVALID, "none" }, + { CUPTI_CB_DOMAIN_INVALID, 0 } }; } // namespace diff --git a/src/services/cupti/CuptiEventSampling.cpp b/src/services/cupti/CuptiEventSampling.cpp index 1089ad5a0..d76f5845c 100644 --- a/src/services/cupti/CuptiEventSampling.cpp +++ b/src/services/cupti/CuptiEventSampling.cpp @@ -14,8 +14,7 @@ using namespace cali::Cupti; namespace { -void -print_cupti_error(std::ostream& os, CUptiResult err, const char* func) +void print_cupti_error(std::ostream& os, CUptiResult err, const char* func) { const char* errstr; @@ -24,14 +23,13 @@ print_cupti_error(std::ostream& os, CUptiResult err, const char* func) os << func << ": error: " << errstr << std::endl; } -#define CHECK_CUPTI_ERR(err, cufunc) \ - if (err != CUPTI_SUCCESS) { \ +#define CHECK_CUPTI_ERR(err, cufunc) \ + if (err != CUPTI_SUCCESS) { \ ::print_cupti_error(Log(0).stream() << "cupti-sampling: error: ", err, cufunc); \ - return false; \ + return false; \ } -bool -cleanup_cupti_event_group(CUpti_EventGroup group) +bool cleanup_cupti_event_group(CUpti_EventGroup group) { CUptiResult res; @@ -43,30 +41,29 @@ cleanup_cupti_event_group(CUpti_EventGroup group) return true; } - + // Iterate over all CUpti event domains and events to // find the event with the given name - + // NOTE: This is currently broken: we get 'wrong'(?) event IDs. // They work with cuptiEventGetAttribute in this function, but not in // cuptiEventGroupAddEvent later on. // The Event IDs we get here are different from the ones that // the cupti_query program gets. No idea why. Those are the IDs that work. // That app uses the device-specific cuptiDeviceEnumEventDomains etc. -// functions, so maybe it's that. - -bool -find_event_by_name(const std::string& target_evt_name, CUpti_EventID* ret_evt_id) +// functions, so maybe it's that. + +bool find_event_by_name(const std::string& target_evt_name, CUpti_EventID* ret_evt_id) { CUptiResult res; - - uint32_t num_domains = 0; - + + uint32_t num_domains = 0; + res = cuptiGetNumEventDomains(&num_domains); CHECK_CUPTI_ERR(res, "cuptiGetNumEventDomains"); std::vector evt_domains(num_domains); - size_t dom_buf_size = evt_domains.size() * sizeof(CUpti_EventDomainID); + size_t dom_buf_size = evt_domains.size() * sizeof(CUpti_EventDomainID); res = cuptiEnumEventDomains(&dom_buf_size, evt_domains.data()); CHECK_CUPTI_ERR(res, "cuptiEnumEventDomains"); @@ -86,14 +83,12 @@ find_event_by_name(const std::string& target_evt_name, CUpti_EventID* ret_evt_id name[79] = '\0'; - Log(2).stream() << "cupti-sampling: Looking for event \"" - << target_evt_name - << "\" in event domain \"" << name << "\"" - << std::endl; + Log(2).stream() << "cupti-sampling: Looking for event \"" << target_evt_name << "\" in event domain \"" + << name << "\"" << std::endl; } std::vector events(num_events); - size_t event_buf_size = events.size() * sizeof(CUpti_EventID); + size_t event_buf_size = events.size() * sizeof(CUpti_EventID); res = cuptiEventDomainEnumEvents(domain_id, &event_buf_size, events.data()); CHECK_CUPTI_ERR(res, "cuptiEventDomainEnumEvents"); @@ -110,9 +105,8 @@ find_event_by_name(const std::string& target_evt_name, CUpti_EventID* ret_evt_id Log(2).stream() << "cupti-sampling: Event " << evt_id << ": " << name << std::endl; if (target_evt_name == name) { - Log(2).stream() << "cupti-sampling: Found event ID " - << evt_id << std::endl; - + Log(2).stream() << "cupti-sampling: Found event ID " << evt_id << std::endl; + *ret_evt_id = evt_id; return true; } @@ -121,50 +115,46 @@ find_event_by_name(const std::string& target_evt_name, CUpti_EventID* ret_evt_id return false; } - -} // namespace [anonymous] +} // namespace -EventSampling::EventSampling() - : m_enabled { false } -{ } +EventSampling::EventSampling() : m_enabled { false } +{} EventSampling::~EventSampling() { stop_all(); } - -bool -EventSampling::setup(Caliper* c, const std::string& event_name) -{ +bool EventSampling::setup(Caliper* c, const std::string& event_name) +{ if (!::find_event_by_name(event_name, &m_event_id)) { Log(0).stream() << "cupti-sampling: CUpti event \"" << event_name - << "\" not found. Disabling CUpti event sampling." - << std::endl; + << "\" not found. Disabling CUpti event sampling." << std::endl; return false; } - Log(1).stream() << "cupti-sampling: Found CUpti event \"" - << event_name - << "\" (ID = " << m_event_id << ")" + Log(1).stream() << "cupti-sampling: Found CUpti event \"" << event_name << "\" (ID = " << m_event_id << ")" << std::endl; Attribute aggr_class_attr = c->get_attribute("class.aggregatable"); Variant v_true(true); - m_event_attr = - c->create_attribute(std::string("cupti.event.")+event_name, CALI_TYPE_UINT, - CALI_ATTR_ASVALUE, - 1, &aggr_class_attr, &v_true); + m_event_attr = c->create_attribute( + std::string("cupti.event.") + event_name, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE, + 1, + &aggr_class_attr, + &v_true + ); m_enabled = true; return true; } -bool -EventSampling::setup(Caliper* c, CUpti_EventID event_id) +bool EventSampling::setup(Caliper* c, CUpti_EventID event_id) { CUptiResult res; @@ -172,32 +162,32 @@ EventSampling::setup(Caliper* c, CUpti_EventID event_id) char name[80]; size_t size = sizeof(name); - + res = cuptiEventGetAttribute(event_id, CUPTI_EVENT_ATTR_NAME, &size, name); CHECK_CUPTI_ERR(res, "cuptiEventGetAttribute"); name[79] = '\0'; - Log(1).stream() << "cupti-sampling: Using CUpti event \"" - << name - << "\" (ID = " << m_event_id << ")" - << std::endl; + Log(1).stream() << "cupti-sampling: Using CUpti event \"" << name << "\" (ID = " << m_event_id << ")" << std::endl; Attribute aggr_class_attr = c->get_attribute("class.aggregatable"); Variant v_true(true); - m_event_attr = - c->create_attribute(std::string("cupti.event.")+name, CALI_TYPE_UINT, - CALI_ATTR_ASVALUE, - 1, &aggr_class_attr, &v_true); + m_event_attr = c->create_attribute( + std::string("cupti.event.") + name, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE, + 1, + &aggr_class_attr, + &v_true + ); m_enabled = true; return true; } -bool -EventSampling::enable_sampling_for_context(CUcontext context) +bool EventSampling::enable_sampling_for_context(CUcontext context) { SamplingInfo info; CUptiResult res; @@ -208,11 +198,9 @@ EventSampling::enable_sampling_for_context(CUcontext context) uint32_t context_id; res = cuptiGetContextId(context, &context_id); CHECK_CUPTI_ERR(res, "cuptiGetContextId"); - - Log(2).stream() << "cupti-sampling: Creating event group on context " - << context_id - << " for event " << m_event_id - << std::endl; + + Log(2).stream() << "cupti-sampling: Creating event group on context " << context_id << " for event " + << m_event_id << std::endl; } res = cuptiSetEventCollectionMode(context, CUPTI_EVENT_COLLECTION_MODE_CONTINUOUS); @@ -228,9 +216,8 @@ EventSampling::enable_sampling_for_context(CUcontext context) CHECK_CUPTI_ERR(res, "cuptiEventGroupEnable"); { - std::lock_guard - g(m_sampling_mtx); - + std::lock_guard g(m_sampling_mtx); + m_sampling_info.push_back(info); } @@ -238,26 +225,21 @@ EventSampling::enable_sampling_for_context(CUcontext context) uint32_t context_id; res = cuptiGetContextId(context, &context_id); CHECK_CUPTI_ERR(res, "cuptiGetContextId"); - - Log(2).stream() << "cupti-sampling: Started event sampling on context " - << context_id - << std::endl; + + Log(2).stream() << "cupti-sampling: Started event sampling on context " << context_id << std::endl; } - + return true; } -bool -EventSampling::disable_sampling_for_context(CUcontext context) +bool EventSampling::disable_sampling_for_context(CUcontext context) { - auto it = std::find_if(m_sampling_info.begin(), m_sampling_info.end(), - [context](const SamplingInfo& info) { - return info.context == context; - } ); + auto it = std::find_if(m_sampling_info.begin(), m_sampling_info.end(), [context](const SamplingInfo& info) { + return info.context == context; + }); if (it == m_sampling_info.end()) { - Log(0).stream() << "cupti-sampling: stop sampling for unknown context " - << context << std::endl; + Log(0).stream() << "cupti-sampling: stop sampling for unknown context " << context << std::endl; return false; } @@ -268,17 +250,15 @@ EventSampling::disable_sampling_for_context(CUcontext context) return true; } -void -EventSampling::stop_all() +void EventSampling::stop_all() { - for (SamplingInfo &info : m_sampling_info) + for (SamplingInfo& info : m_sampling_info) cleanup_cupti_event_group(info.event_grp); m_sampling_info.clear(); } -void -EventSampling::snapshot(Caliper* c, SnapshotBuilder& snapshot) +void EventSampling::snapshot(Caliper* c, SnapshotBuilder& snapshot) { ++m_num_snapshots; @@ -288,11 +268,10 @@ EventSampling::snapshot(Caliper* c, SnapshotBuilder& snapshot) // for the last CUDA context created and hope for the best. This works // when only one thread runs CUDA, which should be most cases. // For a fix we need to figure out the actual current context, but it - // seems we can only do that with the driver API. + // seems we can only do that with the driver API. { - std::lock_guard - g(m_sampling_mtx); + std::lock_guard g(m_sampling_mtx); if (m_sampling_info.empty()) return; @@ -303,27 +282,19 @@ EventSampling::snapshot(Caliper* c, SnapshotBuilder& snapshot) uint64_t val; size_t bytes_read; - CUptiResult res = - cuptiEventGroupReadEvent(group, - CUPTI_EVENT_READ_FLAG_NONE, - m_event_id, - &bytes_read, - &val); + CUptiResult res = cuptiEventGroupReadEvent(group, CUPTI_EVENT_READ_FLAG_NONE, m_event_id, &bytes_read, &val); if (res != CUPTI_SUCCESS || bytes_read != sizeof(val)) return; snapshot.append(m_event_attr, Variant(cali_make_variant_from_uint(val))); - + ++m_num_reads; } -std::ostream& -EventSampling::print_statistics(std::ostream& os) +std::ostream& EventSampling::print_statistics(std::ostream& os) { - os << "cupti-sampling: " - << m_num_snapshots << " total snapshots, " - << m_num_reads << " cupti events read." + os << "cupti-sampling: " << m_num_snapshots << " total snapshots, " << m_num_reads << " cupti events read." << std::endl; return os; diff --git a/src/services/cupti/CuptiEventSampling.h b/src/services/cupti/CuptiEventSampling.h index b960ecf7b..b67a8418e 100644 --- a/src/services/cupti/CuptiEventSampling.h +++ b/src/services/cupti/CuptiEventSampling.h @@ -20,7 +20,7 @@ namespace cali class Caliper; class SnapshotBuilder; - + namespace Cupti { @@ -33,23 +33,23 @@ class EventSampling std::vector m_sampling_info; std::mutex m_sampling_mtx; - + CUpti_EventID m_event_id; - Attribute m_event_attr; + Attribute m_event_attr; + + unsigned m_num_snapshots = 0; + unsigned m_num_reads = 0; - unsigned m_num_snapshots = 0; - unsigned m_num_reads = 0; + bool m_enabled = false; - bool m_enabled = false; - public: bool is_enabled() const { return m_enabled; } // note: event-by-name lookup is currently broken for some reason bool setup(Caliper* c, const std::string& event_name); - + bool setup(Caliper* c, CUpti_EventID event_id); bool enable_sampling_for_context(CUcontext context); @@ -59,14 +59,13 @@ class EventSampling void snapshot(Caliper* c, SnapshotBuilder& snapshot); - std::ostream& - print_statistics(std::ostream& os); + std::ostream& print_statistics(std::ostream& os); EventSampling(); - ~EventSampling(); + ~EventSampling(); }; - + } // namespace Cupti - + } // namespace cali diff --git a/src/services/cupti/CuptiTrace.cpp b/src/services/cupti/CuptiTrace.cpp index e600215c9..7383f4298 100644 --- a/src/services/cupti/CuptiTrace.cpp +++ b/src/services/cupti/CuptiTrace.cpp @@ -42,11 +42,11 @@ namespace class CuptiTraceService { struct ActivityBuffer { - uint8_t* buffer; - CUcontext ctx; - uint32_t stream_id; - size_t size; - size_t valid_size; + uint8_t* buffer; + CUcontext ctx; + uint32_t stream_id; + size_t size; + size_t valid_size; ActivityBuffer* next; ActivityBuffer* prev; @@ -59,9 +59,10 @@ class CuptiTraceService valid_size(valid_size_), next(nullptr), prev(nullptr) - { } + {} - void unlink() { + void unlink() + { if (next) next->prev = prev; if (prev) @@ -78,56 +79,56 @@ class CuptiTraceService std::map device_info_map; - size_t buffer_size = 1 * 1024 * 1024; - size_t buffer_size_used = 0; + size_t buffer_size = 1 * 1024 * 1024; + size_t buffer_size_used = 0; - ActivityBuffer* retired_buffers_list = nullptr; + ActivityBuffer* retired_buffers_list = nullptr; std::mutex retired_buffers_list_lock; - unsigned num_buffers_empty = 0; - unsigned num_buffers_allocated = 0; - unsigned num_buffers_completed = 0; - unsigned num_dropped_records = 0; - - unsigned num_correlation_recs = 0; - unsigned num_device_recs = 0; - unsigned num_kernel_recs = 0; - unsigned num_driver_recs = 0; - unsigned num_memcpy_recs = 0; - unsigned num_runtime_recs = 0; - unsigned num_uvm_recs = 0; - unsigned num_unknown_recs = 0; - - unsigned num_correlations_found = 0; - unsigned num_correlations_missed = 0; - - unsigned num_snapshot_flushes = 0; - - Attribute activity_start_attr; - Attribute activity_end_attr; - Attribute activity_duration_attr; - Attribute activity_kind_attr; - Attribute kernel_name_attr; - Attribute memcpy_kind_attr; - Attribute memcpy_bytes_attr; - Attribute starttime_attr; - Attribute timestamp_attr; - Attribute duration_attr; - Attribute device_uuid_attr; - Attribute fault_address_attr; - Attribute uvm_kind_attr; - Attribute uvm_bytes_attr; - Attribute uvm_pagefault_groups_attr; - Attribute uvm_migration_cause_attr; - Attribute uvm_access_type_attr; - - bool record_host_timestamp = false; - bool record_host_duration = false; - bool flush_on_snapshot = false; - - Attribute flush_trigger_attr; - - std::vector flush_info_attributes; + unsigned num_buffers_empty = 0; + unsigned num_buffers_allocated = 0; + unsigned num_buffers_completed = 0; + unsigned num_dropped_records = 0; + + unsigned num_correlation_recs = 0; + unsigned num_device_recs = 0; + unsigned num_kernel_recs = 0; + unsigned num_driver_recs = 0; + unsigned num_memcpy_recs = 0; + unsigned num_runtime_recs = 0; + unsigned num_uvm_recs = 0; + unsigned num_unknown_recs = 0; + + unsigned num_correlations_found = 0; + unsigned num_correlations_missed = 0; + + unsigned num_snapshot_flushes = 0; + + Attribute activity_start_attr; + Attribute activity_end_attr; + Attribute activity_duration_attr; + Attribute activity_kind_attr; + Attribute kernel_name_attr; + Attribute memcpy_kind_attr; + Attribute memcpy_bytes_attr; + Attribute starttime_attr; + Attribute timestamp_attr; + Attribute duration_attr; + Attribute device_uuid_attr; + Attribute fault_address_attr; + Attribute uvm_kind_attr; + Attribute uvm_bytes_attr; + Attribute uvm_pagefault_groups_attr; + Attribute uvm_migration_cause_attr; + Attribute uvm_access_type_attr; + + bool record_host_timestamp = false; + bool record_host_duration = false; + bool flush_on_snapshot = false; + + Attribute flush_trigger_attr; + + std::vector flush_info_attributes; static CuptiTraceService* s_instance; @@ -136,8 +137,8 @@ class CuptiTraceService // --- Helpers // - static void - print_cupti_error(std::ostream& os, CUptiResult err, const char* func) { + static void print_cupti_error(std::ostream& os, CUptiResult err, const char* func) + { const char* errstr; cuptiGetResultString(err, &errstr); @@ -148,36 +149,38 @@ class CuptiTraceService // --- CUpti buffer management callbacks // - static void CUPTIAPI buffer_requested(uint8_t** buffer, size_t* size, size_t* max_num_recs) { + static void CUPTIAPI buffer_requested(uint8_t** buffer, size_t* size, size_t* max_num_recs) + { *buffer = new uint8_t[s_instance->buffer_size]; - *size = s_instance->buffer_size; + *size = s_instance->buffer_size; *max_num_recs = 0; ++s_instance->num_buffers_allocated; } - void add_completed_buffer(ActivityBuffer* acb, size_t dropped) { + void add_completed_buffer(ActivityBuffer* acb, size_t dropped) + { if (!acb->valid_size) ++num_buffers_empty; num_dropped_records += dropped; - std::lock_guard - g(retired_buffers_list_lock); + std::lock_guard g(retired_buffers_list_lock); if (retired_buffers_list) retired_buffers_list->prev = acb; - acb->next = retired_buffers_list; + acb->next = retired_buffers_list; retired_buffers_list = acb; ++num_buffers_completed; } - static void CUPTIAPI buffer_completed(CUcontext ctx, uint32_t stream, uint8_t* buffer, size_t size, size_t valid_size) { - ActivityBuffer* acb = - new ActivityBuffer(ctx, stream, buffer, size, valid_size); + static void CUPTIAPI + buffer_completed(CUcontext ctx, uint32_t stream, uint8_t* buffer, size_t size, size_t valid_size) + { + ActivityBuffer* acb = new ActivityBuffer(ctx, stream, buffer, size, valid_size); size_t dropped = 0; cuptiActivityGetNumDroppedRecords(ctx, stream, &dropped); @@ -188,8 +191,8 @@ class CuptiTraceService // --- Caliper flush // - const char* - get_memcpy_kind_string(CUpti_ActivityMemcpyKind kind) { + const char* get_memcpy_kind_string(CUpti_ActivityMemcpyKind kind) + { switch (kind) { case CUPTI_ACTIVITY_MEMCPY_KIND_HTOD: return "HtoD"; @@ -216,11 +219,9 @@ class CuptiTraceService return ""; } - const char * - get_uvm_counter_kind_string(CUpti_ActivityUnifiedMemoryCounterKind kind) + const char* get_uvm_counter_kind_string(CUpti_ActivityUnifiedMemoryCounterKind kind) { - switch (kind) - { + switch (kind) { case CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_HTOD: return "HtoD"; case CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_DTOH: @@ -241,8 +242,7 @@ class CuptiTraceService return ""; } - const char * - get_uvm_migration_cause_string(CUpti_ActivityUnifiedMemoryMigrationCause cause) + const char* get_uvm_migration_cause_string(CUpti_ActivityUnifiedMemoryMigrationCause cause) { switch (cause) { case CUPTI_ACTIVITY_UNIFIED_MEMORY_MIGRATION_CAUSE_USER: @@ -261,8 +261,7 @@ class CuptiTraceService return ""; } - const char* - get_uvm_access_type_string(CUpti_ActivityUnifiedMemoryAccessType access) + const char* get_uvm_access_type_string(CUpti_ActivityUnifiedMemoryAccessType access) { switch (access) { case CUPTI_ACTIVITY_UNIFIED_MEMORY_ACCESS_TYPE_READ: @@ -279,285 +278,285 @@ class CuptiTraceService return ""; } - size_t - flush_record(CUpti_Activity* rec, correlation_id_map_t& correlation_map, Caliper* c, const std::vector& irec, SnapshotFlushFn proc_fn) { + size_t flush_record( + CUpti_Activity* rec, + correlation_id_map_t& correlation_map, + Caliper* c, + const std::vector& irec, + SnapshotFlushFn proc_fn + ) + { switch (rec->kind) { case CUPTI_ACTIVITY_KIND_DEVICE: - { - CUpti_ActivityDevice2* device = - reinterpret_cast(rec); + { + CUpti_ActivityDevice2* device = reinterpret_cast(rec); - DeviceInfo info; + DeviceInfo info; - info.id = device->id; - info.name = device->name; - info.uuid = device->uuid; + info.id = device->id; + info.name = device->name; + info.uuid = device->uuid; - { - // make a string with the uuid bytes in hex representation + { + // make a string with the uuid bytes in hex representation - std::ostringstream os; + std::ostringstream os; - std::copy(device->uuid.bytes, device->uuid.bytes+sizeof(device->uuid.bytes), - std::ostream_iterator(os << std::hex << std::setw(2) << std::setfill('0'))); + std::copy( + device->uuid.bytes, + device->uuid.bytes + sizeof(device->uuid.bytes), + std::ostream_iterator(os << std::hex << std::setw(2) << std::setfill('0')) + ); - info.uuid_string = os.str(); - } + info.uuid_string = os.str(); + } - device_info_map[device->id] = info; + device_info_map[device->id] = info; - ++num_device_recs; + ++num_device_recs; - return 0; - } + return 0; + } case CUPTI_ACTIVITY_KIND_EXTERNAL_CORRELATION: - { - CUpti_ActivityExternalCorrelation* exco = - reinterpret_cast(rec); + { + CUpti_ActivityExternalCorrelation* exco = reinterpret_cast(rec); - if (exco->externalKind == CUPTI_EXTERNAL_CORRELATION_KIND_CUSTOM0) - correlation_map[exco->correlationId] = exco->externalId; + if (exco->externalKind == CUPTI_EXTERNAL_CORRELATION_KIND_CUSTOM0) + correlation_map[exco->correlationId] = exco->externalId; - ++num_correlation_recs; + ++num_correlation_recs; - return 0; - } + return 0; + } case CUPTI_ACTIVITY_KIND_DRIVER: - { - ++num_driver_recs; - return 0; - } + { + ++num_driver_recs; + return 0; + } case CUPTI_ACTIVITY_KIND_RUNTIME: - { - ++num_runtime_recs; - return 0; - } + { + ++num_runtime_recs; + return 0; + } case CUPTI_ACTIVITY_KIND_MEMCPY: - { - CUpti_ActivityMemcpy* memcpy = - reinterpret_cast(rec); + { + CUpti_ActivityMemcpy* memcpy = reinterpret_cast(rec); - Node* parent = nullptr; + Node* parent = nullptr; - // find a Caliper context correlation, if any + // find a Caliper context correlation, if any - auto it = correlation_map.find(memcpy->correlationId); + auto it = correlation_map.find(memcpy->correlationId); - if (it != correlation_map.end()) { - parent = c->node(it->second); + if (it != correlation_map.end()) { + parent = c->node(it->second); - ++num_correlations_found; - correlation_map.erase(it); - } else { - ++num_correlations_missed; - } + ++num_correlations_found; + correlation_map.erase(it); + } else { + ++num_correlations_missed; + } - // find a device info record + // find a device info record - { - auto it = device_info_map.find(memcpy->deviceId); - - if (it != device_info_map.end()) - parent = - c->make_tree_entry(device_uuid_attr, - Variant(CALI_TYPE_STRING, - it->second.uuid_string.c_str(), - it->second.uuid_string.size() + 1), - parent); - } + { + auto it = device_info_map.find(memcpy->deviceId); + + if (it != device_info_map.end()) + parent = c->make_tree_entry( + device_uuid_attr, + Variant( + CALI_TYPE_STRING, + it->second.uuid_string.c_str(), + it->second.uuid_string.size() + 1 + ), + parent + ); + } - // append the memcpy info - - Attribute attr[6] = { - activity_kind_attr, - memcpy_kind_attr, - memcpy_bytes_attr, - activity_start_attr, - activity_end_attr, - activity_duration_attr - }; - Variant data[6] = { - Variant("memcpy"), - Variant(get_memcpy_kind_string(static_cast(memcpy->copyKind))), - Variant(cali_make_variant_from_uint(memcpy->bytes)), - Variant(cali_make_variant_from_uint(memcpy->start)), - Variant(cali_make_variant_from_uint(memcpy->end)), - Variant(cali_make_variant_from_uint(memcpy->end - memcpy->start)) - }; - - FixedSizeSnapshotRecord<8> snapshot; - - c->make_record(6, attr, data, snapshot.builder(), parent); - SnapshotView view = snapshot.view(); - - std::vector rec; - rec.reserve(view.size() + irec.size()); - rec.assign(view.begin(), view.end()); - rec.insert(rec.end(), irec.begin(), irec.end()); - proc_fn(*c, rec); - - ++num_memcpy_recs; - - return 1; - } + // append the memcpy info + + Attribute attr[6] = { activity_kind_attr, memcpy_kind_attr, memcpy_bytes_attr, + activity_start_attr, activity_end_attr, activity_duration_attr }; + Variant data[6] = { + Variant("memcpy"), + Variant(get_memcpy_kind_string(static_cast(memcpy->copyKind))), + Variant(cali_make_variant_from_uint(memcpy->bytes)), + Variant(cali_make_variant_from_uint(memcpy->start)), + Variant(cali_make_variant_from_uint(memcpy->end)), + Variant(cali_make_variant_from_uint(memcpy->end - memcpy->start)) + }; + + FixedSizeSnapshotRecord<8> snapshot; + + c->make_record(6, attr, data, snapshot.builder(), parent); + SnapshotView view = snapshot.view(); + + std::vector rec; + rec.reserve(view.size() + irec.size()); + rec.assign(view.begin(), view.end()); + rec.insert(rec.end(), irec.begin(), irec.end()); + proc_fn(*c, rec); + + ++num_memcpy_recs; + + return 1; + } case CUPTI_ACTIVITY_KIND_KERNEL: case CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL: - { - CUpti_ActivityKernel4* kernel = - reinterpret_cast(rec); + { + CUpti_ActivityKernel4* kernel = reinterpret_cast(rec); - Node* parent = nullptr; + Node* parent = nullptr; - // find a Caliper context correlation, if any + // find a Caliper context correlation, if any - { - auto it = correlation_map.find(kernel->correlationId); + { + auto it = correlation_map.find(kernel->correlationId); - if (it != correlation_map.end()) { - parent = c->node(it->second); + if (it != correlation_map.end()) { + parent = c->node(it->second); - ++num_correlations_found; - correlation_map.erase(it); - } else { - ++num_correlations_missed; + ++num_correlations_found; + correlation_map.erase(it); + } else { + ++num_correlations_missed; + } } - } - // find a device info record + // find a device info record - { - auto it = device_info_map.find(kernel->deviceId); - - if (it != device_info_map.end()) - parent = - c->make_tree_entry(device_uuid_attr, - Variant(CALI_TYPE_STRING, - it->second.uuid_string.c_str(), - it->second.uuid_string.size() + 1), - parent); - } + { + auto it = device_info_map.find(kernel->deviceId); + + if (it != device_info_map.end()) + parent = c->make_tree_entry( + device_uuid_attr, + Variant( + CALI_TYPE_STRING, + it->second.uuid_string.c_str(), + it->second.uuid_string.size() + 1 + ), + parent + ); + } - // append the kernel info + // append the kernel info - std::string name = util::demangle(kernel->name); + std::string name = util::demangle(kernel->name); - Attribute attr[5] = { - activity_kind_attr, - kernel_name_attr, - activity_start_attr, - activity_end_attr, - activity_duration_attr - }; - Variant data[5] = { - Variant("kernel"), - Variant(name.c_str()), - Variant(cali_make_variant_from_uint(kernel->start)), - Variant(cali_make_variant_from_uint(kernel->end)), - Variant(cali_make_variant_from_uint(kernel->end - kernel->start)) - }; + Attribute attr[5] = { activity_kind_attr, + kernel_name_attr, + activity_start_attr, + activity_end_attr, + activity_duration_attr }; + Variant data[5] = { Variant("kernel"), + Variant(name.c_str()), + Variant(cali_make_variant_from_uint(kernel->start)), + Variant(cali_make_variant_from_uint(kernel->end)), + Variant(cali_make_variant_from_uint(kernel->end - kernel->start)) }; - FixedSizeSnapshotRecord<8> snapshot; + FixedSizeSnapshotRecord<8> snapshot; - c->make_record(5, attr, data, snapshot.builder(), parent); - SnapshotView view = snapshot.view(); + c->make_record(5, attr, data, snapshot.builder(), parent); + SnapshotView view = snapshot.view(); - std::vector rec; - rec.reserve(view.size() + irec.size()); - rec.assign(view.begin(), view.end()); - rec.insert(rec.end(), irec.begin(), irec.end()); - proc_fn(*c, rec); + std::vector rec; + rec.reserve(view.size() + irec.size()); + rec.assign(view.begin(), view.end()); + rec.insert(rec.end(), irec.begin(), irec.end()); + proc_fn(*c, rec); - ++num_kernel_recs; + ++num_kernel_recs; - return 1; - } + return 1; + } case CUPTI_ACTIVITY_KIND_UNIFIED_MEMORY_COUNTER: - { - CUpti_ActivityUnifiedMemoryCounter2* uvm = - reinterpret_cast(rec); + { + CUpti_ActivityUnifiedMemoryCounter2* uvm = reinterpret_cast(rec); - Attribute attr[8]; - Variant data[8]; + Attribute attr[8]; + Variant data[8]; - std::fill(std::begin(attr), std::end(attr), Attribute()); - std::fill(std::begin(data), std::end(data), Variant()); + std::fill(std::begin(attr), std::end(attr), Attribute()); + std::fill(std::begin(data), std::end(data), Variant()); - attr[0] = activity_kind_attr; - attr[1] = uvm_kind_attr; - attr[2] = fault_address_attr; + attr[0] = activity_kind_attr; + attr[1] = uvm_kind_attr; + attr[2] = fault_address_attr; - data[0] = Variant("uvm"); - data[1] = Variant(get_uvm_counter_kind_string(uvm->counterKind)); - data[2] = Variant(CALI_TYPE_ADDR, &uvm->address, sizeof(void*)); - size_t n = 3; + data[0] = Variant("uvm"); + data[1] = Variant(get_uvm_counter_kind_string(uvm->counterKind)); + data[2] = Variant(CALI_TYPE_ADDR, &uvm->address, sizeof(void*)); + size_t n = 3; - switch (uvm->counterKind) { + switch (uvm->counterKind) { case CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_HTOD: case CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_DTOH: - { - attr[n+0] = uvm_bytes_attr; - attr[n+1] = activity_start_attr; - attr[n+2] = activity_end_attr; - attr[n+3] = activity_duration_attr; - attr[n+4] = uvm_migration_cause_attr; - - CUpti_ActivityUnifiedMemoryMigrationCause cause = - static_cast(uvm->flags); - - data[n+0] = Variant(cali_make_variant_from_uint(uvm->value)); - data[n+1] = Variant(cali_make_variant_from_uint(uvm->start)); - data[n+2] = Variant(cali_make_variant_from_uint(uvm->end)); - data[n+3] = Variant(cali_make_variant_from_uint(uvm->end - uvm->start)); - data[n+4] = Variant(get_uvm_migration_cause_string(cause)); - - n += 5; - } - break; + { + attr[n + 0] = uvm_bytes_attr; + attr[n + 1] = activity_start_attr; + attr[n + 2] = activity_end_attr; + attr[n + 3] = activity_duration_attr; + attr[n + 4] = uvm_migration_cause_attr; + + CUpti_ActivityUnifiedMemoryMigrationCause cause = + static_cast(uvm->flags); + + data[n + 0] = Variant(cali_make_variant_from_uint(uvm->value)); + data[n + 1] = Variant(cali_make_variant_from_uint(uvm->start)); + data[n + 2] = Variant(cali_make_variant_from_uint(uvm->end)); + data[n + 3] = Variant(cali_make_variant_from_uint(uvm->end - uvm->start)); + data[n + 4] = Variant(get_uvm_migration_cause_string(cause)); + + n += 5; + } + break; case CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_CPU_PAGE_FAULT_COUNT: - { - attr[n+0] = activity_start_attr; - data[n+0] = Variant(cali_make_variant_from_uint(uvm->start)); - n += 1; - } - break; + { + attr[n + 0] = activity_start_attr; + data[n + 0] = Variant(cali_make_variant_from_uint(uvm->start)); + n += 1; + } + break; case CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_GPU_PAGE_FAULT: - { - attr[n+0] = uvm_pagefault_groups_attr; - attr[n+1] = activity_start_attr; - attr[n+2] = activity_end_attr; - attr[n+3] = activity_duration_attr; - attr[n+4] = uvm_access_type_attr; - - CUpti_ActivityUnifiedMemoryAccessType access = - static_cast(uvm->flags); - - data[n+0] = Variant(cali_make_variant_from_uint(uvm->value)); - data[n+1] = Variant(cali_make_variant_from_uint(uvm->start)); - data[n+2] = Variant(cali_make_variant_from_uint(uvm->end)); - data[n+3] = Variant(cali_make_variant_from_uint(uvm->end - uvm->start)); - data[n+4] = Variant(get_uvm_access_type_string(access)); - - n += 5; - } - break; + { + attr[n + 0] = uvm_pagefault_groups_attr; + attr[n + 1] = activity_start_attr; + attr[n + 2] = activity_end_attr; + attr[n + 3] = activity_duration_attr; + attr[n + 4] = uvm_access_type_attr; + + CUpti_ActivityUnifiedMemoryAccessType access = + static_cast(uvm->flags); + + data[n + 0] = Variant(cali_make_variant_from_uint(uvm->value)); + data[n + 1] = Variant(cali_make_variant_from_uint(uvm->start)); + data[n + 2] = Variant(cali_make_variant_from_uint(uvm->end)); + data[n + 3] = Variant(cali_make_variant_from_uint(uvm->end - uvm->start)); + data[n + 4] = Variant(get_uvm_access_type_string(access)); + + n += 5; + } + break; default: - break; - } + break; + } - FixedSizeSnapshotRecord<8> snapshot; - c->make_record(n, attr, data, snapshot.builder()); - SnapshotView view = snapshot.view(); + FixedSizeSnapshotRecord<8> snapshot; + c->make_record(n, attr, data, snapshot.builder()); + SnapshotView view = snapshot.view(); - std::vector rec; - rec.reserve(view.size() + irec.size()); - rec.assign(view.begin(), view.end()); - rec.insert(rec.end(), irec.begin(), irec.end()); - proc_fn(*c, rec); + std::vector rec; + rec.reserve(view.size() + irec.size()); + rec.assign(view.begin(), view.end()); + rec.insert(rec.end(), irec.begin(), irec.end()); + proc_fn(*c, rec); - ++num_uvm_recs; + ++num_uvm_recs; - return 1; - } + return 1; + } default: ++num_unknown_recs; } @@ -565,12 +564,12 @@ class CuptiTraceService return 0; } - size_t - flush_buffer(ActivityBuffer* acb, Caliper* c, const std::vector& irec, SnapshotFlushFn proc_fn) { - if (! (acb->valid_size > 0)) + size_t flush_buffer(ActivityBuffer* acb, Caliper* c, const std::vector& irec, SnapshotFlushFn proc_fn) + { + if (!(acb->valid_size > 0)) return 0; - size_t num_records = 0; + size_t num_records = 0; CUpti_Activity* rec = nullptr; CUptiResult res = CUPTI_SUCCESS; @@ -590,7 +589,8 @@ class CuptiTraceService return num_records; } - std::vector get_flush_info(Caliper* c, SnapshotView flush_info) { + std::vector get_flush_info(Caliper* c, SnapshotView flush_info) + { // Extract requested flush_info_attributes from flush_info std::vector ret; @@ -619,7 +619,8 @@ class CuptiTraceService return ret; } - size_t do_flush(Caliper* c, SnapshotView flush_info, SnapshotFlushFn proc_fn) { + size_t do_flush(Caliper* c, SnapshotView flush_info, SnapshotFlushFn proc_fn) + { // Flush CUpti. Apppends all currently active CUpti trace buffers // to the retired_buffers_list. @@ -637,15 +638,14 @@ class CuptiTraceService ActivityBuffer* acb = nullptr; { - std::lock_guard - g(retired_buffers_list_lock); + std::lock_guard g(retired_buffers_list_lock); acb = retired_buffers_list; } size_t num_written = 0; - for ( ; acb; acb = acb->next ) + for (; acb; acb = acb->next) num_written += flush_buffer(acb, c, irec, proc_fn); return num_written; @@ -654,21 +654,21 @@ class CuptiTraceService // --- Caliper callbacks // - void flush_cb(Caliper* c, Channel* channel, SnapshotView flush_info, SnapshotFlushFn proc_fn) { + void flush_cb(Caliper* c, Channel* channel, SnapshotView flush_info, SnapshotFlushFn proc_fn) + { size_t num_written = do_flush(c, flush_info, proc_fn); - Log(1).stream() << channel->name() << ": cuptitrace: Wrote " - << num_written << " records." << std::endl; + Log(1).stream() << channel->name() << ": cuptitrace: Wrote " << num_written << " records." << std::endl; } - void clear_cb(Caliper* c, Channel* chn) { + void clear_cb(Caliper* c, Channel* chn) + { ActivityBuffer* acb = nullptr; { - std::lock_guard - g(retired_buffers_list_lock); + std::lock_guard g(retired_buffers_list_lock); - acb = retired_buffers_list; + acb = retired_buffers_list; retired_buffers_list = nullptr; } @@ -680,13 +680,14 @@ class CuptiTraceService acb->unlink(); delete[] acb->buffer; - delete acb; + delete acb; acb = tmp; } } - void post_begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void post_begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if (attr.is_nested()) { Entry e = c->get(attr); @@ -700,46 +701,42 @@ class CuptiTraceService } } - void pre_end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void pre_end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if (attr.is_nested()) { - CUptiResult res = - cuptiActivityPopExternalCorrelationId(CUPTI_EXTERNAL_CORRELATION_KIND_CUSTOM0, nullptr); + CUptiResult res = cuptiActivityPopExternalCorrelationId(CUPTI_EXTERNAL_CORRELATION_KIND_CUSTOM0, nullptr); if (res != CUPTI_SUCCESS) print_cupti_error(Log(0).stream(), res, "cuptiActivityPopExternalCorrelationId"); } } - void pre_finish_cb(Caliper* c, Channel* channel) { + void pre_finish_cb(Caliper* c, Channel* channel) + { if (Log::verbosity() >= 2) Log(2).stream() << channel->name() << ": finalizing CUpti\n"; cuptiFinalize(); } - void finish_cb(Caliper* c, Channel* chn) { + void finish_cb(Caliper* c, Channel* chn) + { if (Log::verbosity() < 1) return; if (flush_on_snapshot) - Log(1).stream() << chn->name() << ": cuptitrace: Triggered " << num_snapshot_flushes - << " snapshot flushes." << std::endl; + Log(1).stream() << chn->name() << ": cuptitrace: Triggered " << num_snapshot_flushes << " snapshot flushes." + << std::endl; if (num_dropped_records > 0) - Log(1).stream() << chn->name() << ": cuptitrace: Dropped " << num_dropped_records - << " records." << std::endl; - - unitfmt_result bytes_reserved = - unitfmt(num_buffers_completed * buffer_size, unitfmt_bytes); - unitfmt_result bytes_used = - unitfmt(buffer_size_used, unitfmt_bytes); - - Log(1).stream() << chn->name() << ": cuptitrace: Allocated " << num_buffers_allocated - << " buffers (" - << bytes_reserved.val << bytes_reserved.symbol - << " reserved, " - << bytes_used.val << bytes_used.symbol - << " used). " - << num_buffers_completed << " buffers completed, " + Log(1).stream() << chn->name() << ": cuptitrace: Dropped " << num_dropped_records << " records." + << std::endl; + + unitfmt_result bytes_reserved = unitfmt(num_buffers_completed * buffer_size, unitfmt_bytes); + unitfmt_result bytes_used = unitfmt(buffer_size_used, unitfmt_bytes); + + Log(1).stream() << chn->name() << ": cuptitrace: Allocated " << num_buffers_allocated << " buffers (" + << bytes_reserved.val << bytes_reserved.symbol << " reserved, " << bytes_used.val + << bytes_used.symbol << " used). " << num_buffers_completed << " buffers completed, " << num_buffers_empty << " empty." << std::endl; if (Log::verbosity() < 2) @@ -751,43 +748,40 @@ class CuptiTraceService << "\n driver records: " << num_driver_recs << "\n runtime records: " << num_runtime_recs << "\n kernel records: " << num_kernel_recs - << "\n memcpy records: " << num_memcpy_recs - << "\n uvm records: " << num_uvm_recs - << "\n unknown records: " << num_unknown_recs - << std::endl; - - Log(2).stream() << chn->name() << ": cuptitrace: " - << num_correlations_found << " context correlations found, " - << num_correlations_missed << " missed." - << std::endl; + << "\n memcpy records: " << num_memcpy_recs << "\n uvm records: " << num_uvm_recs + << "\n unknown records: " << num_unknown_recs << std::endl; + + Log(2).stream() << chn->name() << ": cuptitrace: " << num_correlations_found << " context correlations found, " + << num_correlations_missed << " missed." << std::endl; } - void configure_uvm_recording(const ConfigSet& config) { + void configure_uvm_recording(const ConfigSet& config) + { CUpti_ActivityUnifiedMemoryCounterConfig umcfg[4]; - size_t n = 0; + size_t n = 0; if (config.get("uvm_transfers").to_bool()) { - umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; - umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_HTOD; + umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; + umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_HTOD; umcfg[n].deviceId = 0; umcfg[n].enable = 1; ++n; - umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; - umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_DTOH; + umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; + umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_BYTES_TRANSFER_DTOH; umcfg[n].deviceId = 0; umcfg[n].enable = 1; ++n; } if (config.get("uvm_pagefaults").to_bool()) { - umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; - umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_CPU_PAGE_FAULT_COUNT; + umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; + umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_CPU_PAGE_FAULT_COUNT; umcfg[n].deviceId = 0; umcfg[n].enable = 1; ++n; - umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; - umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_GPU_PAGE_FAULT; + umcfg[n].scope = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_SCOPE_PROCESS_SINGLE_DEVICE; + umcfg[n].kind = CUPTI_ACTIVITY_UNIFIED_MEMORY_COUNTER_KIND_GPU_PAGE_FAULT; umcfg[n].deviceId = 0; umcfg[n].enable = 1; ++n; @@ -801,36 +795,32 @@ class CuptiTraceService const char* errstr; cuptiGetResultString(res, &errstr); - Log(0).stream() << "cuptitrace: cuptiActivityConfigureUnifiedMemoryCounter: " - << errstr << std::endl; + Log(0).stream() << "cuptitrace: cuptiActivityConfigureUnifiedMemoryCounter: " << errstr << std::endl; } } - void enable_cupti_activities(const ConfigSet& config) { + void enable_cupti_activities(const ConfigSet& config) + { struct activity_map_t { const char* name; CUpti_ActivityKind kind; - } activity_map[] = { - { "correlation", CUPTI_ACTIVITY_KIND_EXTERNAL_CORRELATION }, - { "device", CUPTI_ACTIVITY_KIND_DEVICE }, - { "driver", CUPTI_ACTIVITY_KIND_DRIVER }, - { "runtime", CUPTI_ACTIVITY_KIND_RUNTIME }, - { "kernel", CUPTI_ACTIVITY_KIND_KERNEL }, - { "memcpy", CUPTI_ACTIVITY_KIND_MEMCPY }, - { "uvm", CUPTI_ACTIVITY_KIND_UNIFIED_MEMORY_COUNTER }, + } activity_map[] = { { "correlation", CUPTI_ACTIVITY_KIND_EXTERNAL_CORRELATION }, + { "device", CUPTI_ACTIVITY_KIND_DEVICE }, + { "driver", CUPTI_ACTIVITY_KIND_DRIVER }, + { "runtime", CUPTI_ACTIVITY_KIND_RUNTIME }, + { "kernel", CUPTI_ACTIVITY_KIND_KERNEL }, + { "memcpy", CUPTI_ACTIVITY_KIND_MEMCPY }, + { "uvm", CUPTI_ACTIVITY_KIND_UNIFIED_MEMORY_COUNTER }, - { nullptr, CUPTI_ACTIVITY_KIND_INVALID } - }; + { nullptr, CUPTI_ACTIVITY_KIND_INVALID } }; - std::vector selection = - config.get("activities").to_stringlist(); + std::vector selection = config.get("activities").to_stringlist(); if (std::find(selection.begin(), selection.end(), "uvm") != selection.end()) configure_uvm_recording(config); for (const activity_map_t* act = activity_map; act && act->name; ++act) { - auto it = std::find(selection.begin(), selection.end(), - act->name); + auto it = std::find(selection.begin(), selection.end(), act->name); if (it == selection.end()) continue; @@ -843,50 +833,53 @@ class CuptiTraceService const char* errstr; cuptiGetResultString(res, &errstr); - Log(0).stream() << "cuptitrace: cuptiActivityEnable (" - << act->name << "): " << errstr - << std::endl; + Log(0).stream() << "cuptitrace: cuptiActivityEnable (" << act->name << "): " << errstr << std::endl; } } for (const std::string& s : selection) Log(0).stream() << "cuptitrace: selected activity \"" << s << "\" not found!" << std::endl; - } - void snapshot_cb(Caliper* c, Channel* chn, const SnapshotView, SnapshotBuilder& snapshot) { + void snapshot_cb(Caliper* c, Channel* chn, const SnapshotView, SnapshotBuilder& snapshot) + { uint64_t timestamp = 0; cuptiGetTimestamp(×tamp); - Variant v_now(cali_make_variant_from_uint(timestamp)); - Variant v_prev = c->exchange(timestamp_attr, v_now); + Variant v_now(cali_make_variant_from_uint(timestamp)); + Variant v_prev = c->exchange(timestamp_attr, v_now); if (record_host_duration) - snapshot.append(Entry(duration_attr, - Variant(cali_make_variant_from_uint(timestamp - v_prev.to_uint())))); + snapshot.append(Entry(duration_attr, Variant(cali_make_variant_from_uint(timestamp - v_prev.to_uint())))); } - void snapshot_flush_activities_cb(Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& snapshot) { + void snapshot_flush_activities_cb( + Caliper* c, + Channel* channel, + SnapshotView trigger_info, + SnapshotBuilder& snapshot + ) + { if (c->is_signal()) return; if (!flush_trigger_attr || trigger_info.get(flush_trigger_attr).empty()) return; - do_flush(c, SnapshotView(), [c,channel](CaliperMetadataAccessInterface& db, const std::vector& rec){ - c->push_snapshot(channel, SnapshotView(rec.size(), rec.data())); - }); + do_flush(c, SnapshotView(), [c, channel](CaliperMetadataAccessInterface& db, const std::vector& rec) { + c->push_snapshot(channel, SnapshotView(rec.size(), rec.data())); + }); clear_cb(c, channel); ++num_snapshot_flushes; } - void post_init_cb(Caliper* c, Channel* chn) { + void post_init_cb(Caliper* c, Channel* chn) + { ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); enable_cupti_activities(config); - CUptiResult res = - cuptiActivityRegisterCallbacks(buffer_requested, buffer_completed); + CUptiResult res = cuptiActivityRegisterCallbacks(buffer_requested, buffer_completed); if (res != CUPTI_SUCCESS) { print_cupti_error(Log(0).stream(), res, "cuptiActivityRegisterCallbacks"); @@ -900,182 +893,160 @@ class CuptiTraceService if (config.get("correlate_context").to_bool()) { chn->events().post_begin_evt.connect( - [](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ + [](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { s_instance->post_begin_cb(c, chn, attr, value); - }); - chn->events().pre_end_evt.connect( - [](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ - s_instance->pre_end_cb(c, chn, attr, value); - }); + } + ); + chn->events().pre_end_evt.connect([](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value + ) { s_instance->pre_end_cb(c, chn, attr, value); }); } if (record_host_timestamp || record_host_duration) { c->set(timestamp_attr, cali_make_variant_from_uint(starttime)); - chn->events().snapshot.connect( - [](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec){ - s_instance->snapshot_cb(c, chn, info, rec); - }); + chn->events().snapshot.connect([](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec) { + s_instance->snapshot_cb(c, chn, info, rec); + }); } flush_on_snapshot = config.get("flush_on_snapshot").to_bool(); if (flush_on_snapshot) { std::string attr_name = config.get("flush_trigger").to_string(); - flush_trigger_attr = c->get_attribute(attr_name); + flush_trigger_attr = c->get_attribute(attr_name); if (!flush_trigger_attr) - chn->events().create_attr_evt.connect( - [attr_name](Caliper* c, Channel*, const Attribute& attr){ - if (attr.name() == attr_name) - s_instance->flush_trigger_attr = attr; - }); - - chn->events().snapshot.connect( - [](Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec){ - s_instance->snapshot_flush_activities_cb(c, channel, info, rec); + chn->events().create_attr_evt.connect([attr_name](Caliper* c, Channel*, const Attribute& attr) { + if (attr.name() == attr_name) + s_instance->flush_trigger_attr = attr; }); + chn->events().snapshot.connect([](Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec) { + s_instance->snapshot_flush_activities_cb(c, channel, info, rec); + }); + Log(1).stream() << chn->name() << ": cuptitrace: Using flush-on-snapshot mode." - << " Triggering on " << attr_name - << std::endl; + << " Triggering on " << attr_name << std::endl; } else { - chn->events().flush_evt.connect( - [](Caliper* c, Channel* chn, SnapshotView info, SnapshotFlushFn flush_fn){ - s_instance->flush_cb(c, chn, info, flush_fn); - }); - chn->events().clear_evt.connect( - [](Caliper* c, Channel* chn){ - s_instance->clear_cb(c, chn); - }); + chn->events().flush_evt.connect([](Caliper* c, Channel* chn, SnapshotView info, SnapshotFlushFn flush_fn) { + s_instance->flush_cb(c, chn, info, flush_fn); + }); + chn->events().clear_evt.connect([](Caliper* c, Channel* chn) { s_instance->clear_cb(c, chn); }); } - chn->events().pre_finish_evt.connect( - [](Caliper* c, Channel* chn){ - s_instance->pre_finish_cb(c, chn); - }); - chn->events().finish_evt.connect( - [](Caliper* c, Channel* chn){ - s_instance->finish_cb(c, chn); - delete s_instance; - s_instance = nullptr; - }); + chn->events().pre_finish_evt.connect([](Caliper* c, Channel* chn) { s_instance->pre_finish_cb(c, chn); }); + chn->events().finish_evt.connect([](Caliper* c, Channel* chn) { + s_instance->finish_cb(c, chn); + delete s_instance; + s_instance = nullptr; + }); Log(1).stream() << chn->name() << ": Registered cuptitrace service" << std::endl; } CuptiTraceService(Caliper* c, Channel* chn) - { - Attribute unit_attr = - c->create_attribute("time.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - Attribute addr_class_attr = - c->get_attribute("class.memoryaddress"); - Variant nsec_val(CALI_TYPE_STRING, "nsec", 4); - Variant true_val(true); - - activity_start_attr = - c->create_attribute("cupti.activity.start", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); - activity_end_attr = - c->create_attribute("cupti.activity.end", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); - activity_duration_attr = - c->create_attribute("cupti.activity.duration", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE, - 1, &unit_attr, &nsec_val); - activity_kind_attr = - c->create_attribute("cupti.activity.kind", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - kernel_name_attr = - c->create_attribute("cupti.kernel.name", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - memcpy_kind_attr = - c->create_attribute("cupti.memcpy.kind", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - memcpy_bytes_attr = - c->create_attribute("cupti.memcpy.bytes", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - starttime_attr = - c->create_attribute("cupti.starttime", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS); - device_uuid_attr = - c->create_attribute("cupti.device.uuid", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - fault_address_attr = - c->create_attribute("cupti.fault.addr", CALI_TYPE_ADDR, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - 1, &addr_class_attr, &true_val); - uvm_kind_attr = - c->create_attribute("cupti.uvm.kind", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - uvm_bytes_attr = - c->create_attribute("cupti.uvm.bytes", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - uvm_pagefault_groups_attr = - c->create_attribute("cupti.uvm.pagefault.groups", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - uvm_migration_cause_attr = - c->create_attribute("cupti.uvm.migration.cause", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - uvm_access_type_attr = - c->create_attribute("cupti.uvm.access.type", CALI_TYPE_STRING, - CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - - record_host_timestamp = config.get("snapshot_timestamps").to_bool(); - record_host_duration = config.get("snapshot_duration").to_bool(); - - if (record_host_duration || record_host_timestamp) { - int hide_offset = - record_host_timestamp ? 0 : CALI_ATTR_HIDDEN; - - timestamp_attr = - c->create_attribute("cupti.timestamp", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - hide_offset); - duration_attr = - c->create_attribute("cupti.host.duration", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE, - 1, &unit_attr, &nsec_val); - } + { + Attribute unit_attr = c->create_attribute("time.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + Attribute addr_class_attr = c->get_attribute("class.memoryaddress"); + Variant nsec_val(CALI_TYPE_STRING, "nsec", 4); + Variant true_val(true); + + activity_start_attr = + c->create_attribute("cupti.activity.start", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + activity_end_attr = + c->create_attribute("cupti.activity.end", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + activity_duration_attr = c->create_attribute( + "cupti.activity.duration", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + 1, + &unit_attr, + &nsec_val + ); + activity_kind_attr = + c->create_attribute("cupti.activity.kind", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + kernel_name_attr = + c->create_attribute("cupti.kernel.name", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + memcpy_kind_attr = + c->create_attribute("cupti.memcpy.kind", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + memcpy_bytes_attr = c->create_attribute( + "cupti.memcpy.bytes", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + starttime_attr = + c->create_attribute("cupti.starttime", CALI_TYPE_UINT, CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS); + device_uuid_attr = + c->create_attribute("cupti.device.uuid", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + fault_address_attr = c->create_attribute( + "cupti.fault.addr", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, + 1, + &addr_class_attr, + &true_val + ); + uvm_kind_attr = + c->create_attribute("cupti.uvm.kind", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); + uvm_bytes_attr = c->create_attribute( + "cupti.uvm.bytes", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + uvm_pagefault_groups_attr = c->create_attribute( + "cupti.uvm.pagefault.groups", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + uvm_migration_cause_attr = c->create_attribute( + "cupti.uvm.migration.cause", + CALI_TYPE_STRING, + CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS + ); + uvm_access_type_attr = + c->create_attribute("cupti.uvm.access.type", CALI_TYPE_STRING, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS); - flush_info_attributes = config.get("info_attributes").to_stringlist(); + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); + + record_host_timestamp = config.get("snapshot_timestamps").to_bool(); + record_host_duration = config.get("snapshot_duration").to_bool(); + + if (record_host_duration || record_host_timestamp) { + int hide_offset = record_host_timestamp ? 0 : CALI_ATTR_HIDDEN; + + timestamp_attr = c->create_attribute( + "cupti.timestamp", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | hide_offset + ); + duration_attr = c->create_attribute( + "cupti.host.duration", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + 1, + &unit_attr, + &nsec_val + ); } + flush_info_attributes = config.get("info_attributes").to_stringlist(); + } + public: static const char* s_spec; - static void cuptitrace_initialize(Caliper* c, Channel* chn) { + static void cuptitrace_initialize(Caliper* c, Channel* chn) + { if (s_instance) { - Log(0).stream() << chn->name() << ": cuptitrace service is already initialized!" - << std::endl; + Log(0).stream() << chn->name() << ": cuptitrace service is already initialized!" << std::endl; return; } s_instance = new CuptiTraceService(c, chn); - chn->events().post_init_evt.connect( - [](Caliper* c, Channel* chn){ - s_instance->post_init_cb(c, chn); - }); + chn->events().post_init_evt.connect([](Caliper* c, Channel* chn) { s_instance->post_init_cb(c, chn); }); } - }; const char* CuptiTraceService::s_spec = R"json( @@ -1134,7 +1105,7 @@ const char* CuptiTraceService::s_spec = R"json( CuptiTraceService* CuptiTraceService::s_instance = nullptr; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/debug/Debug.cpp b/src/services/debug/Debug.cpp index 447bb9373..437e71042 100644 --- a/src/services/debug/Debug.cpp +++ b/src/services/debug/Debug.cpp @@ -37,13 +37,13 @@ void begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& va void end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { lock_guard lock(dbg_mutex); - Log(1).stream() << chn->name() << ": Event: pre_end (" << attr.name() << "=" << value << ")" << endl; + Log(1).stream() << chn->name() << ": Event: pre_end (" << attr.name() << "=" << value << ")" << endl; } void set_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { lock_guard lock(dbg_mutex); - Log(1).stream() << chn->name() << ": Event: pre_set (" << attr.name() << "=" << value << ")" << endl; + Log(1).stream() << chn->name() << ": Event: pre_set (" << attr.name() << "=" << value << ")" << endl; } void create_thread_cb(Caliper* c, Channel* chn) @@ -79,12 +79,12 @@ std::ostream& print_snapshot_record(Caliper* c, std::ostream& os, SnapshotView r return (os << " }"); } - + void process_snapshot_cb(Caliper* c, Channel* chn, SnapshotView, SnapshotView rec) { - lock_guard lock(dbg_mutex); + lock_guard lock(dbg_mutex); - print_snapshot_record( c, Log(1).stream() << chn->name() << ": Event: process_snapshot: ", rec ) << std::endl; + print_snapshot_record(c, Log(1).stream() << chn->name() << ": Event: process_snapshot: ", rec) << std::endl; } void finish_cb(Caliper* c, Channel* chn) diff --git a/src/services/env/EnvironmentInfo.cpp b/src/services/env/EnvironmentInfo.cpp index 08d089d9b..d19a6a15c 100644 --- a/src/services/env/EnvironmentInfo.cpp +++ b/src/services/env/EnvironmentInfo.cpp @@ -32,18 +32,16 @@ using namespace std; namespace { -static const ConfigSet::Entry s_configdata[] = { - { "extra", CALI_TYPE_STRING, "", - "List of environment variables to add to the Caliper blackboard", - "List of environment variables to add to the Caliper blackboard" - }, - ConfigSet::Terminator -}; +static const ConfigSet::Entry s_configdata[] = { { "extra", + CALI_TYPE_STRING, + "", + "List of environment variables to add to the Caliper blackboard", + "List of environment variables to add to the Caliper blackboard" }, + ConfigSet::Terminator }; void read_cmdline(Caliper* c, Channel* chn, ConfigSet& config) { - Attribute cmdline_attr = - c->create_attribute("env.cmdline", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute cmdline_attr = c->create_attribute("env.cmdline", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); // Attempt to open /proc/self/cmdline and read it @@ -51,26 +49,23 @@ void read_cmdline(Caliper* c, Channel* chn, ConfigSet& config) std::vector args; - for (std::string arg; std::getline(fs, arg, static_cast(0)); ) + for (std::string arg; std::getline(fs, arg, static_cast(0));) c->begin(cmdline_attr, Variant(CALI_TYPE_STRING, arg.data(), arg.size())); } - + void read_uname(Caliper* c, Channel* chn, ConfigSet& config) { #ifdef _WIN32 - const struct uname_attr_info_t { + const struct uname_attr_info_t { const char* attr_name; const char* u_val; - } uname_attr_info[] = { - { "env.os.sysname", "Windows" }, - { "env.os.release", "10" }, - { "env.os.version", "10" }, - { "env.machine", "x86_64" } - }; + } uname_attr_info[] = { { "env.os.sysname", "Windows" }, + { "env.os.release", "10" }, + { "env.os.version", "10" }, + { "env.machine", "x86_64" } }; for (const uname_attr_info_t& uinfo : uname_attr_info) { - Attribute attr = - c->create_attribute(uinfo.attr_name, CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute attr = c->create_attribute(uinfo.attr_name, CALI_TYPE_STRING, CALI_ATTR_GLOBAL); c->set(attr, Variant(CALI_TYPE_STRING, uinfo.u_val, strlen(uinfo.u_val))); } @@ -78,19 +73,16 @@ void read_uname(Caliper* c, Channel* chn, ConfigSet& config) struct utsname u; if (uname(&u) == 0) { - const struct uname_attr_info_t { + const struct uname_attr_info_t { const char* attr_name; const char* u_val; - } uname_attr_info[] = { - { "env.os.sysname", u.sysname }, - { "env.os.release", u.release }, - { "env.os.version", u.version }, - { "env.machine", u.machine } - }; + } uname_attr_info[] = { { "env.os.sysname", u.sysname }, + { "env.os.release", u.release }, + { "env.os.version", u.version }, + { "env.machine", u.machine } }; for (const uname_attr_info_t& uinfo : uname_attr_info) { - Attribute attr = - c->create_attribute(uinfo.attr_name, CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute attr = c->create_attribute(uinfo.attr_name, CALI_TYPE_STRING, CALI_ATTR_GLOBAL); c->set(attr, Variant(CALI_TYPE_STRING, uinfo.u_val, strlen(uinfo.u_val))); } @@ -100,29 +92,27 @@ void read_uname(Caliper* c, Channel* chn, ConfigSet& config) void read_time(Caliper* c, Channel* chn, ConfigSet& config) { - Attribute starttime_attr = - c->create_attribute("env.starttime", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + Attribute starttime_attr = c->create_attribute("env.starttime", CALI_TYPE_STRING, CALI_ATTR_GLOBAL); - time_t t = time(NULL); - struct tm *tmp = localtime(&t); + time_t t = time(NULL); + struct tm* tmp = localtime(&t); if (!tmp) return; char buf[64]; - size_t len = strftime(buf, sizeof(buf)-1, "%a %d %b %Y %H:%M:%S %z", tmp); + size_t len = strftime(buf, sizeof(buf) - 1, "%a %d %b %Y %H:%M:%S %z", tmp); c->set(starttime_attr, Variant(CALI_TYPE_STRING, buf, len)); } void read_hostname(Caliper* c, Channel* chn, ConfigSet& config) { - Attribute hostname_attr = - c->create_attribute("env.hostname", CALI_TYPE_STRING, CALI_ATTR_SCOPE_PROCESS); + Attribute hostname_attr = c->create_attribute("env.hostname", CALI_TYPE_STRING, CALI_ATTR_SCOPE_PROCESS); char buf[256]; - if (gethostname(buf, sizeof(buf)-1) == 0) + if (gethostname(buf, sizeof(buf) - 1) == 0) c->set(hostname_attr, Variant(CALI_TYPE_STRING, buf, strlen(buf))); } @@ -133,17 +123,16 @@ void read_extra(Caliper* c, Channel* chn, ConfigSet& config) for (string& env : extra_list) { if (env.empty()) continue; - - Attribute attr = - c->create_attribute("env."+env, CALI_TYPE_STRING, CALI_ATTR_GLOBAL); - + + Attribute attr = c->create_attribute("env." + env, CALI_TYPE_STRING, CALI_ATTR_GLOBAL); + char* val = getenv(env.c_str()); if (val) c->set(attr, Variant(CALI_TYPE_STRING, val, strlen(val))); } } - + void environment_service_register(Caliper* c, Channel* chn) { Log(1).stream() << chn->name() << ": Registered env service." << std::endl; @@ -156,8 +145,8 @@ void environment_service_register(Caliper* c, Channel* chn) read_hostname(c, chn, config); read_extra(c, chn, config); } - -} + +} // namespace namespace cali { diff --git a/src/services/event/EventTrigger.cpp b/src/services/event/EventTrigger.cpp index bcf2d02b4..3d92806a1 100644 --- a/src/services/event/EventTrigger.cpp +++ b/src/services/event/EventTrigger.cpp @@ -30,9 +30,9 @@ using namespace std; namespace cali { - extern Attribute subscription_event_attr; // From api.cpp - extern Attribute phase_attr; -} +extern Attribute subscription_event_attr; // From api.cpp +extern Attribute phase_attr; +} // namespace cali namespace { @@ -43,67 +43,63 @@ class EventTrigger // --- Per-channel instance data // - Attribute trigger_begin_attr; - Attribute trigger_end_attr; - Attribute trigger_set_attr; + Attribute trigger_begin_attr; + Attribute trigger_end_attr; + Attribute trigger_set_attr; - Attribute marker_attr; + Attribute marker_attr; - Attribute region_count_attr; - Entry region_count_entry; + Attribute region_count_attr; + Entry region_count_entry; std::vector trigger_attr_names; - bool enable_snapshot_info { true }; - int region_level { 0 }; + bool enable_snapshot_info { true }; + int region_level { 0 }; - Node event_root_node; + Node event_root_node; - RegionFilter region_filter; - RegionFilter branch_filter; + RegionFilter region_filter; + RegionFilter branch_filter; - std::vector branch_filter_stack; + std::vector branch_filter_stack; // // --- Helpers / misc // - void parse_region_level(Channel* channel, const std::string& str) { + void parse_region_level(Channel* channel, const std::string& str) + { if (str == "phase") { region_level = phase_attr.level(); } else { - bool ok = false; - int level = StringConverter(str).to_int(&ok); + bool ok = false; + int level = StringConverter(str).to_int(&ok); if (!ok || level < 0 || level > 7) { - Log(0).stream() << channel->name() - << ": event: Invalid region level \"" << str << "\"\n"; + Log(0).stream() << channel->name() << ": event: Invalid region level \"" << str << "\"\n"; region_level = 0; } else { region_level = level; } } - Log(2).stream() << channel->name() - << ": event: Using region level " << region_level << "\n"; + Log(2).stream() << channel->name() << ": event: Using region level " << region_level << "\n"; } - void mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) { + void mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) + { cali_id_t evt_attr_ids[3] = { CALI_INV_ID }; struct evt_attr_setup_t { std::string prefix; int index; - } evt_attr_setup[] = { - { "event.begin#", 0 }, - { "event.set#", 1 }, - { "event.end#", 2 } - }; + } evt_attr_setup[] = { { "event.begin#", 0 }, { "event.set#", 1 }, { "event.end#", 2 } }; cali_attr_type type = attr.type(); int prop = attr.properties(); - for ( evt_attr_setup_t setup : evt_attr_setup ) { + for (evt_attr_setup_t setup : evt_attr_setup) { std::string s = setup.prefix; s.append(attr.name()); @@ -113,14 +109,13 @@ class EventTrigger c->create_attribute(s, type, (prop & ~delete_flags) | CALI_ATTR_SKIP_EVENTS).id(); } - c->make_tree_entry(marker_attr, - Variant(CALI_TYPE_USR, evt_attr_ids, sizeof(evt_attr_ids)), - attr.node()); + c->make_tree_entry(marker_attr, Variant(CALI_TYPE_USR, evt_attr_ids, sizeof(evt_attr_ids)), attr.node()); Log(2).stream() << chn->name() << ": event: Marked attribute " << attr.name() << std::endl; } - void check_attribute(Caliper* c, Channel* chn, const Attribute& attr) { + void check_attribute(Caliper* c, Channel* chn, const Attribute& attr) + { if (attr.id() < 12 /* skip fixed metadata attributes */ || attr.skip_events()) return; @@ -134,7 +129,8 @@ class EventTrigger mark_attribute(c, chn, attr); } - const Node* find_marker(const Attribute& attr) { + const Node* find_marker(const Attribute& attr) + { cali_id_t marker_id = marker_attr.id(); for (const Node* node = attr.node()->first_child(); node; node = node->next_sibling()) @@ -144,7 +140,8 @@ class EventTrigger return nullptr; } - static inline bool is_subscription_attribute(const Attribute& attr) { + static inline bool is_subscription_attribute(const Attribute& attr) + { return attr.get(cali::subscription_event_attr).to_bool(); } @@ -152,7 +149,8 @@ class EventTrigger // --- Callbacks // - void pre_begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void pre_begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { const Node* marker_node = find_marker(attr); if (!marker_node) @@ -169,8 +167,7 @@ class EventTrigger if (enable_snapshot_info) { assert(!marker_node->data().empty()); - const cali_id_t* evt_info_attr_ids = - static_cast(marker_node->data().data()); + const cali_id_t* evt_info_attr_ids = static_cast(marker_node->data().data()); assert(evt_info_attr_ids != nullptr); @@ -178,7 +175,7 @@ class EventTrigger // Construct the trigger info entry Attribute attrs[2] = { trigger_begin_attr, begin_attr }; - Variant vals[2] = { Variant(attr.id()), value }; + Variant vals[2] = { Variant(attr.id()), value }; FixedSizeSnapshotRecord<2> trigger_info; @@ -189,7 +186,8 @@ class EventTrigger } } - void pre_set_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void pre_set_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { const Node* marker_node = find_marker(attr); if (!marker_node) @@ -206,16 +204,15 @@ class EventTrigger if (enable_snapshot_info) { assert(!marker_node->data().empty()); - const cali_id_t* evt_info_attr_ids = - static_cast(marker_node->data().data()); + const cali_id_t* evt_info_attr_ids = static_cast(marker_node->data().data()); assert(evt_info_attr_ids != nullptr); Attribute set_attr = c->get_attribute(evt_info_attr_ids[1]); // Construct the trigger info entry - Attribute attrs[2] = { trigger_set_attr, set_attr }; - Variant vals[2] = { Variant(attr.id()), value }; + Attribute attrs[2] = { trigger_set_attr, set_attr }; + Variant vals[2] = { Variant(attr.id()), value }; FixedSizeSnapshotRecord<2> trigger_info; @@ -226,7 +223,8 @@ class EventTrigger } } - void pre_end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void pre_end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { const Node* marker_node = find_marker(attr); if (!marker_node) @@ -243,8 +241,7 @@ class EventTrigger if (enable_snapshot_info) { assert(!marker_node->data().empty()); - const cali_id_t* evt_info_attr_ids = - static_cast(marker_node->data().data()); + const cali_id_t* evt_info_attr_ids = static_cast(marker_node->data().data()); assert(evt_info_attr_ids != nullptr); @@ -253,7 +250,7 @@ class EventTrigger // Construct the trigger info entry with previous level Attribute attrs[3] = { trigger_end_attr, end_attr, region_count_attr }; - Variant vals[3] = { Variant(attr.id()), value, cali_make_variant_from_uint(1) }; + Variant vals[3] = { Variant(attr.id()), value, cali_make_variant_from_uint(1) }; FixedSizeSnapshotRecord<3> trigger_info; @@ -268,7 +265,8 @@ class EventTrigger // --- Constructor // - void check_existing_attributes(Caliper* c, Channel* chn) { + void check_existing_attributes(Caliper* c, Channel* chn) + { auto attributes = c->get_all_attributes(); for (const Attribute& attr : attributes) @@ -276,105 +274,94 @@ class EventTrigger check_attribute(c, chn, attr); } - EventTrigger(Caliper* c, Channel* channel) - : event_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) + EventTrigger(Caliper* c, Channel* channel) : event_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) + { + region_count_attr = c->create_attribute( + "region.count", + CALI_TYPE_UINT, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); + region_count_entry = Entry(region_count_attr, cali_make_variant_from_uint(1)); + + ConfigSet cfg = services::init_config_from_spec(channel->config(), s_spec); + + trigger_attr_names = cfg.get("trigger").to_stringlist(",:"); + enable_snapshot_info = cfg.get("enable_snapshot_info").to_bool(); + parse_region_level(channel, cfg.get("region_level").to_string()); + { - region_count_attr = - c->create_attribute("region.count", CALI_TYPE_UINT, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); - region_count_entry = Entry(region_count_attr, cali_make_variant_from_uint(1)); - - ConfigSet cfg = - services::init_config_from_spec(channel->config(), s_spec); - - trigger_attr_names = cfg.get("trigger").to_stringlist(",:"); - enable_snapshot_info = cfg.get("enable_snapshot_info").to_bool(); - parse_region_level(channel, cfg.get("region_level").to_string()); - - { - std::string i_filter = - cfg.get("include_regions").to_string(); - std::string e_filter = - cfg.get("exclude_regions").to_string(); - - auto p = RegionFilter::from_config(i_filter, e_filter); - - if (!p.second.empty()) { - Log(0).stream() << channel->name() << ": event: filter parse error: " - << p.second << std::endl; - } else { - region_filter = p.first; - } + std::string i_filter = cfg.get("include_regions").to_string(); + std::string e_filter = cfg.get("exclude_regions").to_string(); + + auto p = RegionFilter::from_config(i_filter, e_filter); + + if (!p.second.empty()) { + Log(0).stream() << channel->name() << ": event: filter parse error: " << p.second << std::endl; + } else { + region_filter = p.first; } + } - { - std::string i_filter = - cfg.get("include_branches").to_string(); + { + std::string i_filter = cfg.get("include_branches").to_string(); - auto p = RegionFilter::from_config(i_filter, ""); + auto p = RegionFilter::from_config(i_filter, ""); - if (!p.second.empty()) { - Log(0).stream() << channel->name() << ": event: branch filter parse error: " - << p.second << std::endl; - } else { - branch_filter = p.first; - } + if (!p.second.empty()) { + Log(0).stream() << channel->name() << ": event: branch filter parse error: " << p.second << std::endl; + } else { + branch_filter = p.first; } - - // register trigger events - - trigger_begin_attr = - c->create_attribute("cali.event.begin", - CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); - trigger_set_attr = - c->create_attribute("cali.event.set", - CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); - trigger_end_attr = - c->create_attribute("cali.event.end", - CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); - marker_attr = - c->create_attribute(std::string("event.marker#")+std::to_string(channel->id()), - CALI_TYPE_USR, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_HIDDEN); - - check_existing_attributes(c, channel); } + // register trigger events + + trigger_begin_attr = + c->create_attribute("cali.event.begin", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); + trigger_set_attr = + c->create_attribute("cali.event.set", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); + trigger_end_attr = + c->create_attribute("cali.event.end", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); + marker_attr = c->create_attribute( + std::string("event.marker#") + std::to_string(channel->id()), + CALI_TYPE_USR, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN + ); + + check_existing_attributes(c, channel); + } + public: static const char* s_spec; - static void event_trigger_register(Caliper* c, Channel* chn) { + static void event_trigger_register(Caliper* c, Channel* chn) + { EventTrigger* instance = new EventTrigger(c, chn); - chn->events().create_attr_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr){ - if (!is_subscription_attribute(attr)) - instance->check_attribute(c, chn, attr); - }); - chn->events().subscribe_attribute.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr){ + chn->events().create_attr_evt.connect([instance](Caliper* c, Channel* chn, const Attribute& attr) { + if (!is_subscription_attribute(attr)) instance->check_attribute(c, chn, attr); - }); + }); + chn->events().subscribe_attribute.connect([instance](Caliper* c, Channel* chn, const Attribute& attr) { + instance->check_attribute(c, chn, attr); + }); chn->events().pre_begin_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ + [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { instance->pre_begin_cb(c, chn, attr, value); - }); + } + ); chn->events().pre_set_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ + [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { instance->pre_set_cb(c, chn, attr, value); - }); + } + ); chn->events().pre_end_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ + [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { instance->pre_end_cb(c, chn, attr, value); - }); - chn->events().finish_evt.connect( - [instance](Caliper*, Channel*){ - delete instance; - }); + } + ); + chn->events().finish_evt.connect([instance](Caliper*, Channel*) { delete instance; }); Log(1).stream() << chn->name() << ": Registered event trigger service" << std::endl; } @@ -417,7 +404,6 @@ const char* EventTrigger::s_spec = R"json( } // namespace - namespace cali { diff --git a/src/services/io/CurIOus/include/curious/curious.h b/src/services/io/CurIOus/include/curious/curious.h index 98089bb1f..e58725477 100644 --- a/src/services/io/CurIOus/include/curious/curious.h +++ b/src/services/io/CurIOus/include/curious/curious.h @@ -1,76 +1,79 @@ #ifndef CURIOUS - #define CURIOUS +#define CURIOUS - #ifdef __cplusplus - extern "C" { - #endif +#ifdef __cplusplus +extern "C" +{ +#endif - #include +#include - /********* +/********* * Types * *********/ - // Error values - typedef enum curious_err { +// Error values +typedef enum curious_err { CURIOUS_ERR_INVALID_CALLBACK_TYPE = 1, - } curious_error_t; - - // Used to identify an CURIOUS instance - typedef int curious_t; - - // Indicates what API an IO function originally came from - // (each should be a power of two, so that they can be properly or-ed together) - typedef enum curious_api { - CURIOUS_POSIX = 0x1, +} curious_error_t; + +// Used to identify an CURIOUS instance +typedef int curious_t; + +// Indicates what API an IO function originally came from +// (each should be a power of two, so that they can be properly or-ed together) +typedef enum curious_api { + CURIOUS_POSIX = 0x1, CURIOUS_CSTDIO = 0x2, - } curious_api_t; +} curious_api_t; + +// Just all the curious_api_ts bitwise or-ed together - used to indicate that every API should be wrapped +#define CURIOUS_ALL_APIS (CURIOUS_POSIX | CURIOUS_CSTDIO) - // Just all the curious_api_ts bitwise or-ed together - used to indicate that every API should be wrapped - #define CURIOUS_ALL_APIS (CURIOUS_POSIX | CURIOUS_CSTDIO) +// Should be bitwise or of curious_api_ts - used to indicate which APIs to wrap functions from +typedef int curious_apis_t; - // Should be bitwise or of curious_api_ts - used to indicate which APIs to wrap functions from - typedef int curious_apis_t; +// Callback Attributes: - // Callback Attributes: +#define CURIOUS_POST_CALLBACK 0x1 // use this if the callback is to be called after the original function, - #define CURIOUS_POST_CALLBACK 0x1 // use this if the callback is to be called after the original function, - // ommit it if it should be called before the original function - // This comes before the function ids, despite the difficulty with indexing the bindings - // because it allows us to add more functions AND avoid paddding the arrays with empty space - // whil still maintaining the correspondence between ids and indices in both arrays - - // Also, if the callback is to be called after the original function, bitwise anding CURIOUS_POST_CALLBACK - // with the curious_callback_type should return 1 (0 means it should be called before the original function) +// ommit it if it should be called before the original function +// This comes before the function ids, despite the difficulty with indexing the bindings +// because it allows us to add more functions AND avoid paddding the arrays with empty space +// whil still maintaining the correspondence between ids and indices in both arrays - // Indicates what type of I/O operation the callback should be triggered by, and recieve data from - typedef enum curious_callback_category { - CURIOUS_READ_CALLBACK = 0x0, - CURIOUS_WRITE_CALLBACK = 0x2, +// Also, if the callback is to be called after the original function, bitwise anding CURIOUS_POST_CALLBACK +// with the curious_callback_type should return 1 (0 means it should be called before the original function) + +// Indicates what type of I/O operation the callback should be triggered by, and recieve data from +typedef enum curious_callback_category { + CURIOUS_READ_CALLBACK = 0x0, + CURIOUS_WRITE_CALLBACK = 0x2, CURIOUS_METADATA_CALLBACK = 0x4, - } curious_callback_category_t; - - // bitwise and this mask with an curious_callback_type_t to get the curious_callback_category - #define CURIOUS_CALLBACK_CATEGORY_MASK 0x6 - - // Bitwise or together callback attributes and IO function IDs to build an curious_callback_type - // ex: CURIOUS_READ_CALLBACK = callback for before read - // CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK = callback for after read - typedef int curious_callback_type_t; - - // The total number of valid curious_callback_types, mainly included for iteration purposes - #define CURIOUS_CALLBACK_TYPE_COUNT 6 - - // IO function IDs - // Uniquely identify IO functions which can recieve callbacks; - // specifically gives the function's index in the GOTCHA binding table - typedef enum curious_function_id { - #define CURIOUS_POSIX_START CURIOUS_READ_ID +} curious_callback_category_t; + +// bitwise and this mask with an curious_callback_type_t to get the curious_callback_category +#define CURIOUS_CALLBACK_CATEGORY_MASK 0x6 + +// Bitwise or together callback attributes and IO function IDs to build an curious_callback_type +// ex: CURIOUS_READ_CALLBACK = callback for before read +// CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK = callback for after read +typedef int curious_callback_type_t; + +// The total number of valid curious_callback_types, mainly included for iteration purposes +#define CURIOUS_CALLBACK_TYPE_COUNT 6 + +// IO function IDs +// Uniquely identify IO functions which can recieve callbacks; +// specifically gives the function's index in the GOTCHA binding table +typedef enum curious_function_id { + +#define CURIOUS_POSIX_START CURIOUS_READ_ID CURIOUS_READ_ID = 0, CURIOUS_WRITE_ID, CURIOUS_OPEN_ID, CURIOUS_CLOSE_ID, - #define CURIOUS_STAT_START CURIOUS_STAT_ID +#define CURIOUS_STAT_START CURIOUS_STAT_ID CURIOUS_STAT_ID, CURIOUS__STAT_ID, CURIOUS_LSTAT_ID, @@ -89,7 +92,7 @@ CURIOUS__FXSTAT_ID, CURIOUS_FXSTAT64_ID, CURIOUS__FXSTAT64_ID, - #define CURIOUS_CSTDIO_START CURIOUS_FOPEN_ID +#define CURIOUS_CSTDIO_START CURIOUS_FOPEN_ID CURIOUS_FOPEN_ID, CURIOUS_FOPEN64_ID, CURIOUS_FDOPEN_ID, @@ -111,74 +114,74 @@ //CURIOUS_GETS_ID, CURIOUS_FREAD_ID, CURIOUS_FWRITE_ID, - } curious_function_id_t; +} curious_function_id_t; - // This is just to help with iteration - #define CURIOUS_FUNCTION_COUNT 36 +// This is just to help with iteration +#define CURIOUS_FUNCTION_COUNT 36 - // I/O Record Types: +// I/O Record Types: - // For callbacks to read functions - // (i.e. ones registered with a curious_callback_type containing CURIOUS_READ_CALLBACK) - typedef struct curious_read_record { - size_t bytes_read; - unsigned int call_count; - char *filesystem; - char *mount_point; +// For callbacks to read functions +// (i.e. ones registered with a curious_callback_type containing CURIOUS_READ_CALLBACK) +typedef struct curious_read_record { + size_t bytes_read; + unsigned int call_count; + char* filesystem; + char* mount_point; curious_function_id_t function_id; - } curious_read_record_t; - - // For callbacks to write functions - // (i.e. ones registered with a curious_callback_type containing CURIOUS_WRITE_CALLBACK) - typedef struct curious_write_record { - size_t bytes_written; - unsigned int call_count; - char *filesystem; - char *mount_point; +} curious_read_record_t; + +// For callbacks to write functions +// (i.e. ones registered with a curious_callback_type containing CURIOUS_WRITE_CALLBACK) +typedef struct curious_write_record { + size_t bytes_written; + unsigned int call_count; + char* filesystem; + char* mount_point; curious_function_id_t function_id; - } curious_write_record_t; - - // For callbacks to metadata functions - // (i.e. ones registered with a curious_callback_type containing CURIOUS_METADATA_CALLBACK) - typedef struct curious_metadata_record { - unsigned int call_count; - char *filesystem; - char *mount_point; +} curious_write_record_t; + +// For callbacks to metadata functions +// (i.e. ones registered with a curious_callback_type containing CURIOUS_METADATA_CALLBACK) +typedef struct curious_metadata_record { + unsigned int call_count; + char* filesystem; + char* mount_point; curious_function_id_t function_id; - } curious_metadata_record_t; - - // All IO callbacks must have this basic signature, - // which allows all neccessary information to be cast as needed - // from the two arguments (usr_args being defined by the person - // using this API, io_record being defined by this API to pass in - // appropriate data for the function type) - typedef void (*curious_callback_f)(curious_callback_type_t type, void *usr_args, void *io_record); - - /***************** +} curious_metadata_record_t; + +// All IO callbacks must have this basic signature, +// which allows all neccessary information to be cast as needed +// from the two arguments (usr_args being defined by the person +// using this API, io_record being defined by this API to pass in +// appropriate data for the function type) +typedef void (*curious_callback_f)(curious_callback_type_t type, void* usr_args, void* io_record); + +/***************** * API Functions * *****************/ - // Prepares library for use; call at the start of a program which uses CURIOUS - // Use curious_apis to indicate which API's functions to wrap, by bitwise or-ing together - // the corresponding curious_api_t constants, or pass in ALL_APIS to use them all - // Returns a unique identifier for this application's use of CURIOUS - curious_t curious_init(curious_apis_t curious_apis); - - // Cleans up all library variables; call at the end of a program which uses CURIOUS - void curious_finalize(curious_t curious_inst); - - // Adds the given callback to the list of those called before or after a wrapped function, - // as indicated by type. Callback will be passed the given usr_args as its first argument - // when called and a struct containing all of the arguments to the wrapping function - // in a function-dependent struct as the second argument (see io_record.h) - int curious_register_callback( - curious_t curious_inst, +// Prepares library for use; call at the start of a program which uses CURIOUS +// Use curious_apis to indicate which API's functions to wrap, by bitwise or-ing together +// the corresponding curious_api_t constants, or pass in ALL_APIS to use them all +// Returns a unique identifier for this application's use of CURIOUS +curious_t curious_init(curious_apis_t curious_apis); + +// Cleans up all library variables; call at the end of a program which uses CURIOUS +void curious_finalize(curious_t curious_inst); + +// Adds the given callback to the list of those called before or after a wrapped function, +// as indicated by type. Callback will be passed the given usr_args as its first argument +// when called and a struct containing all of the arguments to the wrapping function +// in a function-dependent struct as the second argument (see io_record.h) +int curious_register_callback( + curious_t curious_inst, curious_callback_type_t type, - curious_callback_f callback, - void *usr_args - ); + curious_callback_f callback, + void* usr_args +); - #ifdef __cplusplus - } // extern "C" - #endif +#ifdef __cplusplus +} // extern "C" +#endif #endif diff --git a/src/services/io/CurIOus/src/callbacks.c b/src/services/io/CurIOus/src/callbacks.c index cceb3863f..12ec23560 100644 --- a/src/services/io/CurIOus/src/callbacks.c +++ b/src/services/io/CurIOus/src/callbacks.c @@ -6,80 +6,85 @@ static curious_dynamic_array_t callback_registry[CURIOUS_CALLBACK_TYPE_COUNT]; -void curious_init_callback_registry(void) { - for (int i = 0; i < CURIOUS_CALLBACK_TYPE_COUNT; ++i) { - create_curious_dynamic_array(callback_registry + i, 0, sizeof(curious_callback_data_t)); - } +void curious_init_callback_registry(void) +{ + for (int i = 0; i < CURIOUS_CALLBACK_TYPE_COUNT; ++i) { + create_curious_dynamic_array(callback_registry + i, 0, sizeof(curious_callback_data_t)); + } } -void curious_finalize_callback_registry(void) { - for (int i = 0; i < CURIOUS_CALLBACK_TYPE_COUNT; ++i) { - destroy_curious_dynamic_array(callback_registry + i, NULL); - } +void curious_finalize_callback_registry(void) +{ + for (int i = 0; i < CURIOUS_CALLBACK_TYPE_COUNT; ++i) { + destroy_curious_dynamic_array(callback_registry + i, NULL); + } } int curious_register_callback( - curious_t curious_inst, - curious_callback_type_t type, - curious_callback_f callback, - void *usr_args -) { - if (0 <= type && type < CURIOUS_CALLBACK_TYPE_COUNT) { - //we don't need to allocate space to hold the data, since it's just going to be memcpy-ed into an array - curious_callback_data_t data; + curious_t curious_inst, + curious_callback_type_t type, + curious_callback_f callback, + void* usr_args +) +{ + if (0 <= type && type < CURIOUS_CALLBACK_TYPE_COUNT) { + //we don't need to allocate space to hold the data, since it's just going to be memcpy-ed into an array + curious_callback_data_t data; - //initialise feilds - data.callback = callback; - data.usr_args = usr_args; - data.curious_inst = curious_inst; + //initialise feilds + data.callback = callback; + data.usr_args = usr_args; + data.curious_inst = curious_inst; - //add record to the part of the registry corresponding to its type - append_to_curious_dynamic_array(callback_registry + type, (void *) &data); + //add record to the part of the registry corresponding to its type + append_to_curious_dynamic_array(callback_registry + type, (void*) &data); - return 0; - } else { - return CURIOUS_ERR_INVALID_CALLBACK_TYPE; - } + return 0; + } else { + return CURIOUS_ERR_INVALID_CALLBACK_TYPE; + } } -void curious_deregister_callbacks(curious_t curious_inst) { - for (int i = 0; i < CURIOUS_CALLBACK_TYPE_COUNT; ++i) { - curious_dynamic_array_t *callbacks = callback_registry + i; - int block_size = 0; - for (int j = 0; j <= callbacks->last_el; ++j) { - curious_callback_data_t *data = (curious_callback_data_t *) (get_from_curious_dynamic_array(callbacks, j)); - - // We want to deal with contiguous blocks of functions from this CURIOUS instance - // all at once, for effeciency reasons, so when we find a callback that comes - // from that instance, we just note that we found it, for now... - if (data->curious_inst == curious_inst) { - ++block_size; - // ...when we reach the end of such a block, that's when we actually remove - // all of the call backs... - } else if (block_size) { - remove_from_curious_dynamic_array(callbacks, j - block_size, block_size, NULL); - // ...and then start looking for a new block - block_size = 0; - } - } - - // We might have a block go to the end of the array, but we still need to remove those - if (block_size) { - remove_from_curious_dynamic_array(callbacks, callbacks->last_el + 1 - block_size, block_size, NULL); +void curious_deregister_callbacks(curious_t curious_inst) +{ + for (int i = 0; i < CURIOUS_CALLBACK_TYPE_COUNT; ++i) { + curious_dynamic_array_t* callbacks = callback_registry + i; + int block_size = 0; + for (int j = 0; j <= callbacks->last_el; ++j) { + curious_callback_data_t* data = (curious_callback_data_t*) (get_from_curious_dynamic_array(callbacks, j)); + + // We want to deal with contiguous blocks of functions from this CURIOUS instance + // all at once, for effeciency reasons, so when we find a callback that comes + // from that instance, we just note that we found it, for now... + if (data->curious_inst == curious_inst) { + ++block_size; + // ...when we reach the end of such a block, that's when we actually remove + // all of the call backs... + } else if (block_size) { + remove_from_curious_dynamic_array(callbacks, j - block_size, block_size, NULL); + // ...and then start looking for a new block + block_size = 0; + } + } + + // We might have a block go to the end of the array, but we still need to remove those + if (block_size) { + remove_from_curious_dynamic_array(callbacks, callbacks->last_el + 1 - block_size, block_size, NULL); + } } - } } -void curious_call_callbacks(curious_callback_type_t type, void *io_args) { - //lookup the list of registred functions of the specified type... - curious_dynamic_array_t callbacks = callback_registry[type]; +void curious_call_callbacks(curious_callback_type_t type, void* io_args) +{ + //lookup the list of registred functions of the specified type... + curious_dynamic_array_t callbacks = callback_registry[type]; - //...and run every one - for (int i = 0; i <= callbacks.last_el; ++i) { - curious_callback_data_t *data = (curious_callback_data_t *) (get_from_curious_dynamic_array(&callbacks, i)); - - //usr_data was registered with the function earlier, - //io_args was just passed to the IO function in the main code - data->callback(type, data->usr_args, io_args); - } + //...and run every one + for (int i = 0; i <= callbacks.last_el; ++i) { + curious_callback_data_t* data = (curious_callback_data_t*) (get_from_curious_dynamic_array(&callbacks, i)); + + //usr_data was registered with the function earlier, + //io_args was just passed to the IO function in the main code + data->callback(type, data->usr_args, io_args); + } } diff --git a/src/services/io/CurIOus/src/callbacks.h b/src/services/io/CurIOus/src/callbacks.h index 0b710e1a5..90579e2c8 100644 --- a/src/services/io/CurIOus/src/callbacks.h +++ b/src/services/io/CurIOus/src/callbacks.h @@ -1,46 +1,47 @@ #ifndef IO_CALLBACKS - #define IO_CALLBACKS - - #include +#define IO_CALLBACKS - #ifdef __cplusplus - extern "C" { - #endif +#include - /********* +#ifdef __cplusplus +extern "C" +{ +#endif + +/********* * Types * *********/ - typedef struct curious_callback_data { +typedef struct curious_callback_data { //a function to be called either before or after an IO function curious_callback_f callback; //arbitrary usr-defined information which can be passed to the callback function //(note: the user is responsible for freeing this, if necessary) - void *usr_args; + void* usr_args; //indicates which CURIOUS instance registered this callback curious_t curious_inst; - } curious_callback_data_t; +} curious_callback_data_t; - /********************** +/********************** * Internal Functions * **********************/ - void curious_call_callbacks(curious_callback_type_t type, void *io_args); - void curious_destroy_curious_callback_data(curious_callback_data_t *self); - void curious_init_callback_registry(void); - void curious_finalize_callback_registry(void); +void curious_call_callbacks(curious_callback_type_t type, void* io_args); +void curious_destroy_curious_callback_data(curious_callback_data_t* self); +void curious_init_callback_registry(void); +void curious_finalize_callback_registry(void); - // Prefix is just for consistency with curious_register_callback, not an indication - // that this function is a part of the external API - void curious_deregister_callbacks(curious_t curious_inst); +// Prefix is just for consistency with curious_register_callback, not an indication +// that this function is a part of the external API +void curious_deregister_callbacks(curious_t curious_inst); - /***************** +/***************** * API Functions * *****************/ - // See curious.h under include/curious for API functions +// See curious.h under include/curious for API functions - #ifdef __cplusplus - } // extern "C" - #endif +#ifdef __cplusplus +} // extern "C" +#endif #endif diff --git a/src/services/io/CurIOus/src/curious.c b/src/services/io/CurIOus/src/curious.c index 614a0119d..e59080f2f 100644 --- a/src/services/io/CurIOus/src/curious.c +++ b/src/services/io/CurIOus/src/curious.c @@ -6,39 +6,41 @@ #include "file_registry.h" #include "mount_tree.h" -static curious_t next_inst = 0; -static int active_insts = 0; +static curious_t next_inst = 0; +static int active_insts = 0; static curious_apis_t wrapped_apis = 0; -int curious_init(curious_apis_t apis) { - if (0 == active_insts) { - //First-time setup - curious_init_file_registry(); - curious_init_callback_registry(); - } - - ++active_insts; - - //If there are any APIs which were requested - //that aren't already wrapped, wrap them - curious_apis_t missing_apis = apis & ~wrapped_apis; - if (missing_apis) { - curious_apply_wrappers(missing_apis); - wrapped_apis |= missing_apis; - } - - return ++next_inst; +int curious_init(curious_apis_t apis) +{ + if (0 == active_insts) { + //First-time setup + curious_init_file_registry(); + curious_init_callback_registry(); + } + + ++active_insts; + + //If there are any APIs which were requested + //that aren't already wrapped, wrap them + curious_apis_t missing_apis = apis & ~wrapped_apis; + if (missing_apis) { + curious_apply_wrappers(missing_apis); + wrapped_apis |= missing_apis; + } + + return ++next_inst; } -void curious_finalize(curious_t curious_inst) { - //Turn everything off if we don't have any active instances... - if(0 == --active_insts) { - curious_finalize_callback_registry(); - curious_disable_wrappers(); - curious_finalize_file_registry(); - - // ...and just remove the callbacks from this instance of CURIOUS otherwise - } else { - curious_deregister_callbacks(curious_inst); - } +void curious_finalize(curious_t curious_inst) +{ + //Turn everything off if we don't have any active instances... + if (0 == --active_insts) { + curious_finalize_callback_registry(); + curious_disable_wrappers(); + curious_finalize_file_registry(); + + // ...and just remove the callbacks from this instance of CURIOUS otherwise + } else { + curious_deregister_callbacks(curious_inst); + } } diff --git a/src/services/io/CurIOus/src/dynamic_array.c b/src/services/io/CurIOus/src/dynamic_array.c index d495dc73e..23b0860d2 100644 --- a/src/services/io/CurIOus/src/dynamic_array.c +++ b/src/services/io/CurIOus/src/dynamic_array.c @@ -3,133 +3,140 @@ #include "dynamic_array.h" -void create_curious_dynamic_array(curious_dynamic_array_t *self, int len, size_t el_size) { - //if the array is starting empty, then there isn't a first element; - //setting it to -1 means we allways get out of bounds errors - //and ensures that the next index is 0 - self->last_el = -1; - - self->len = len; - self->el_size = el_size; - self->array = malloc(len * el_size); +void create_curious_dynamic_array(curious_dynamic_array_t* self, int len, size_t el_size) +{ + //if the array is starting empty, then there isn't a first element; + //setting it to -1 means we allways get out of bounds errors + //and ensures that the next index is 0 + self->last_el = -1; + + self->len = len; + self->el_size = el_size; + self->array = malloc(len * el_size); } -void destroy_curious_dynamic_array(curious_dynamic_array_t *self, free_f destroy_el) { - if (NULL != destroy_el) { - //destroy each element in the array... - for (int i = 0; i <= self->last_el; ++i) { - destroy_el(get_from_curious_dynamic_array(self, i)); +void destroy_curious_dynamic_array(curious_dynamic_array_t* self, free_f destroy_el) +{ + if (NULL != destroy_el) { + //destroy each element in the array... + for (int i = 0; i <= self->last_el; ++i) { + destroy_el(get_from_curious_dynamic_array(self, i)); + } } - } - //...and only afterwards free the array itself - free(self->array); + //...and only afterwards free the array itself + free(self->array); } -void append_to_curious_dynamic_array(curious_dynamic_array_t *self, void *new_el) { - //if there's no more space left in the array... - if (self->last_el + 1 >= self->len) { - if (0 >= self->len) { - //...either give it a single element or... - self->len = 1; - } else { - //...double its length - self->len *= 2; +void append_to_curious_dynamic_array(curious_dynamic_array_t* self, void* new_el) +{ + //if there's no more space left in the array... + if (self->last_el + 1 >= self->len) { + if (0 >= self->len) { + //...either give it a single element or... + self->len = 1; + } else { + //...double its length + self->len *= 2; + } + + self->array = realloc(self->array, self->len * self->el_size); } - self->array = realloc(self->array, self->len * self->el_size); - } - - //either way, we can just copy the new element to space at - //the end of the array and update last_el accordingly - self->last_el++; - memcpy(get_from_curious_dynamic_array(self, self->last_el), new_el, self->el_size); + //either way, we can just copy the new element to space at + //the end of the array and update last_el accordingly + self->last_el++; + memcpy(get_from_curious_dynamic_array(self, self->last_el), new_el, self->el_size); } -int remove_from_curious_dynamic_array(curious_dynamic_array_t *self, int index, int num_els, free_f destroy_el) { - //TODO: make these actual errors - if (index < 0 || index > self->last_el) { - return -1; - } else if (num_els < 0 || num_els > self->last_el - index) { - return -2; - } else if (0 == num_els) { - //not an error, we just don't need to remove anything - return 0; - } +int remove_from_curious_dynamic_array(curious_dynamic_array_t* self, int index, int num_els, free_f destroy_el) +{ + //TODO: make these actual errors + if (index < 0 || index > self->last_el) { + return -1; + } else if (num_els < 0 || num_els > self->last_el - index) { + return -2; + } else if (0 == num_els) { + //not an error, we just don't need to remove anything + return 0; + } - // Free all the elements from the section to be removed - if (NULL != destroy_el) { - int end = index + num_els; - for (int i = index; i < end; ++i) { - destroy_el(get_from_curious_dynamic_array(self, i)); + // Free all the elements from the section to be removed + if (NULL != destroy_el) { + int end = index + num_els; + for (int i = index; i < end; ++i) { + destroy_el(get_from_curious_dynamic_array(self, i)); + } } - } - - int tail_count = self->last_el - num_els - index; - - // Move the remaining entries at the end of the array (if any) - // forward to fill the vacated space - if (tail_count > 0) { - memmove( - get_from_curious_dynamic_array(self, index), - get_from_curious_dynamic_array(self, index + num_els), - tail_count * self->el_size - ); - } - self->last_el -= num_els; - - return 0; + + int tail_count = self->last_el - num_els - index; + + // Move the remaining entries at the end of the array (if any) + // forward to fill the vacated space + if (tail_count > 0) { + memmove( + get_from_curious_dynamic_array(self, index), + get_from_curious_dynamic_array(self, index + num_els), + tail_count * self->el_size + ); + } + self->last_el -= num_els; + + return 0; } -void set_in_curious_dynamic_array(curious_dynamic_array_t *self, void *new_el, void *filler_el, int index) { - //if there's not enough space left in the array... - if (index + 1 >= self->len) { - while (index + 1 >= self->len) { - if (0 >= self->len) { - //...either give it a single element or... - self->len = 1; - } else { - //...double its length - self->len *= 2; - } +void set_in_curious_dynamic_array(curious_dynamic_array_t* self, void* new_el, void* filler_el, int index) +{ + //if there's not enough space left in the array... + if (index + 1 >= self->len) { + while (index + 1 >= self->len) { + if (0 >= self->len) { + //...either give it a single element or... + self->len = 1; + } else { + //...double its length + self->len *= 2; + } + } + + //the loop above is mostly this way so we can do a single realloc no matter what + self->array = realloc(self->array, self->len * self->el_size); } - //the loop above is mostly this way so we can do a single realloc no matter what - self->array = realloc(self->array, self->len * self->el_size); - } - - //use filler_el to fill in the space between the current end of the array - //and the one we're adding - this allows the user to handle accesses to - //this blank space, allowing us to keep the get interface simple - while(index - self->last_el > 1) { - append_to_curious_dynamic_array(self, filler_el); - } - - //either way, we can just copy the new element to the indicated space - //in the array and update last_el accordingly - if (index > self->last_el) { - ++self->last_el; - } - memcpy(get_from_curious_dynamic_array(self, index), new_el, self->el_size); + //use filler_el to fill in the space between the current end of the array + //and the one we're adding - this allows the user to handle accesses to + //this blank space, allowing us to keep the get interface simple + while (index - self->last_el > 1) { + append_to_curious_dynamic_array(self, filler_el); + } + + //either way, we can just copy the new element to the indicated space + //in the array and update last_el accordingly + if (index > self->last_el) { + ++self->last_el; + } + memcpy(get_from_curious_dynamic_array(self, index), new_el, self->el_size); } -void * get_from_curious_dynamic_array(curious_dynamic_array_t *self, int index) { - if (index < 0 || index > self->last_el) { - //just return NULL if the index is out of bounds - return NULL; - } else { - return self->array + self->el_size * index; - } +void* get_from_curious_dynamic_array(curious_dynamic_array_t* self, int index) +{ + if (index < 0 || index > self->last_el) { + //just return NULL if the index is out of bounds + return NULL; + } else { + return self->array + self->el_size * index; + } } - -void * find_in_curious_dynamic_array(curious_dynamic_array_t *self, void *value, compare_f compare_to_el) { - for (int i = 0; i <= self->last_el; ++i) { - void *el = get_from_curious_dynamic_array(self, i); - if (0 == compare_to_el(value, el)) { - return el; +void* find_in_curious_dynamic_array(curious_dynamic_array_t* self, void* value, compare_f compare_to_el) +{ + for (int i = 0; i <= self->last_el; ++i) { + void* el = get_from_curious_dynamic_array(self, i); + + if (0 == compare_to_el(value, el)) { + return el; + } } - } - return NULL; + return NULL; } diff --git a/src/services/io/CurIOus/src/dynamic_array.h b/src/services/io/CurIOus/src/dynamic_array.h index f7281a9a6..5c0182ff1 100644 --- a/src/services/io/CurIOus/src/dynamic_array.h +++ b/src/services/io/CurIOus/src/dynamic_array.h @@ -1,36 +1,36 @@ #ifndef curious_dynamic_array - #define curious_dynamic_array +#define curious_dynamic_array - /********* +/********* * Types * *********/ - typedef struct curious_dynamic_array { - int last_el; //index of lat element in array - int len; //number of elements in array +typedef struct curious_dynamic_array { + int last_el; //index of lat element in array + int len; //number of elements in array size_t el_size; //size of each element in array - void *array; //stores actual data - } curious_dynamic_array_t; + void* array; //stores actual data +} curious_dynamic_array_t; - //a function with the same signature (and essential purpose) as free - typedef void (* free_f)(void *); +//a function with the same signature (and essential purpose) as free +typedef void (*free_f)(void*); - //a function which compares two values and returns 0 if they are equal - //(ex: strcmp) - typedef int (* compare_f)(void *value_0, void *value_1); +//a function which compares two values and returns 0 if they are equal +//(ex: strcmp) +typedef int (*compare_f)(void* value_0, void* value_1); - /******************************* +/******************************* * Dynamic array API Functions * *******************************/ - void create_curious_dynamic_array(curious_dynamic_array_t *self, int len, size_t el_size); - // NOTE: need to accept free function for each element, in case they have nested pointers - void destroy_curious_dynamic_array(curious_dynamic_array_t *self, free_f destroy_el); - void append_to_curious_dynamic_array(curious_dynamic_array_t *self, void *new_el); - int remove_from_curious_dynamic_array(curious_dynamic_array_t *self, int index, int num_els, free_f destroy_el); - void set_in_curious_dynamic_array(curious_dynamic_array_t *self, void *new_el, void *filler_el, int index); - void * get_from_curious_dynamic_array(curious_dynamic_array_t *self, int index); - // Returns a pointer to the first element in the given array for which - // compare_to_el(value ) returns 0, or NULL if no such element is found - void * find_in_curious_dynamic_array(curious_dynamic_array_t *self, void *value, compare_f compare_to_el); +void create_curious_dynamic_array(curious_dynamic_array_t* self, int len, size_t el_size); +// NOTE: need to accept free function for each element, in case they have nested pointers +void destroy_curious_dynamic_array(curious_dynamic_array_t* self, free_f destroy_el); +void append_to_curious_dynamic_array(curious_dynamic_array_t* self, void* new_el); +int remove_from_curious_dynamic_array(curious_dynamic_array_t* self, int index, int num_els, free_f destroy_el); +void set_in_curious_dynamic_array(curious_dynamic_array_t* self, void* new_el, void* filler_el, int index); +void* get_from_curious_dynamic_array(curious_dynamic_array_t* self, int index); +// Returns a pointer to the first element in the given array for which +// compare_to_el(value ) returns 0, or NULL if no such element is found +void* find_in_curious_dynamic_array(curious_dynamic_array_t* self, void* value, compare_f compare_to_el); #endif diff --git a/src/services/io/CurIOus/src/file_registry.c b/src/services/io/CurIOus/src/file_registry.c index 532d2fc86..ca777d783 100644 --- a/src/services/io/CurIOus/src/file_registry.c +++ b/src/services/io/CurIOus/src/file_registry.c @@ -9,124 +9,130 @@ static curious_dynamic_array_t file_registry; -void curious_init_file_registry() { - curious_init_mount_tree(); - create_curious_dynamic_array(&file_registry, 3, sizeof(curious_file_record_t)); - - //lookup the actual filenames of the standard I/O files - curious_register_file_by_fd(STDIN_FILENO); - curious_register_file_by_fd(STDOUT_FILENO); - curious_register_file_by_fd(STDERR_FILENO); +void curious_init_file_registry() +{ + curious_init_mount_tree(); + create_curious_dynamic_array(&file_registry, 3, sizeof(curious_file_record_t)); + + //lookup the actual filenames of the standard I/O files + curious_register_file_by_fd(STDIN_FILENO); + curious_register_file_by_fd(STDOUT_FILENO); + curious_register_file_by_fd(STDERR_FILENO); } -void curious_finalize_file_registry() { - destroy_curious_dynamic_array(&file_registry, (free_f) destroy_curious_file_record); - curious_finalize_mount_tree(); +void curious_finalize_file_registry() +{ + destroy_curious_dynamic_array(&file_registry, (free_f) destroy_curious_file_record); + curious_finalize_mount_tree(); } -void destroy_curious_file_record(curious_file_record_t *record) { - // We allocated space for the path string when we duplicated it - free(record->path); - - record->path = NULL; - record->filesystem = NULL; - record->mount_point = NULL; +void destroy_curious_file_record(curious_file_record_t* record) +{ + // We allocated space for the path string when we duplicated it + free(record->path); + + record->path = NULL; + record->filesystem = NULL; + record->mount_point = NULL; } //TODO: add error states for improper fds -int curious_register_file_by_fd(int fd) { - if (fd < 0) { - return -1; - } - - // Setup the basic I/O files - // Assume we have at most a 6 digit file descriptor, since we can spare a few bytes - #define DIGITS 6 - char fd_path[] = "/proc/self/fd/\0 "; - size_t len = strlen(fd_path); - - // We make the assumption that the mount will be in the first 256 bytes, - // since that's longer than any line in /proc/mounts - #define BUF_SIZE 256 - char buf[BUF_SIZE]; - - // append fd to fd_path - int trunc_ret = snprintf(fd_path + len, DIGITS, "%d", fd); - if(trunc_ret < 0 || trunc_ret >= DIGITS) - fprintf(stderr, "Warning! err code: %i. fd (%i) may be truncated in fd_path (%s)\n", - trunc_ret, fd, fd_path); - - // see where the link stored at fd_path goes - size_t bytes_written = readlink(fd_path, buf, BUF_SIZE); - - // readlink doesn't add a null terminator, so we should - if (bytes_written < BUF_SIZE - 1) { - buf[bytes_written] = '\0'; - } else { - buf[BUF_SIZE - 1] = '\0'; - } - - // now register the file using that path - char *filesystem; - char *mount_point; - curious_get_filesystem(buf, &filesystem, &mount_point); - return curious_register_file(buf, fd, filesystem, mount_point); +int curious_register_file_by_fd(int fd) +{ + if (fd < 0) { + return -1; + } + +// Setup the basic I/O files +// Assume we have at most a 6 digit file descriptor, since we can spare a few bytes +#define DIGITS 6 + char fd_path[] = "/proc/self/fd/\0 "; + size_t len = strlen(fd_path); + +// We make the assumption that the mount will be in the first 256 bytes, +// since that's longer than any line in /proc/mounts +#define BUF_SIZE 256 + char buf[BUF_SIZE]; + + // append fd to fd_path + int trunc_ret = snprintf(fd_path + len, DIGITS, "%d", fd); + if (trunc_ret < 0 || trunc_ret >= DIGITS) + fprintf(stderr, "Warning! err code: %i. fd (%i) may be truncated in fd_path (%s)\n", trunc_ret, fd, fd_path); + + // see where the link stored at fd_path goes + size_t bytes_written = readlink(fd_path, buf, BUF_SIZE); + + // readlink doesn't add a null terminator, so we should + if (bytes_written < BUF_SIZE - 1) { + buf[bytes_written] = '\0'; + } else { + buf[BUF_SIZE - 1] = '\0'; + } + + // now register the file using that path + char* filesystem; + char* mount_point; + curious_get_filesystem(buf, &filesystem, &mount_point); + return curious_register_file(buf, fd, filesystem, mount_point); } -int curious_register_file(const char *path, int fd, char *filesystem, char *mount_point) { - curious_file_record_t *cur_record = (curious_file_record_t *) get_from_curious_dynamic_array(&file_registry, fd); - - // If the file is a pipe, then identify the pipe as the 'mount point' - if (NULL == mount_point && 0 == strncmp(path, "pipe:", 5)) { - mount_point = strdup(path); - } - - //printf("Registering %s on %s filesystem as fd %d\n", path, filesystem, fd); - - // If the fd hasn't been used before... - if (NULL == cur_record) { - // We need to set up a new record - curious_file_record_t new_record; - - // ...and intialise its fields - new_record.path = strdup(path); - new_record.filesystem = filesystem; - new_record.mount_point = mount_point; - - //Ditto for a blank record to fill any extra space created - //by adding this file record - curious_file_record_t blank_record; - blank_record.path = NULL; - blank_record.filesystem = NULL; - blank_record.mount_point = NULL; - - set_in_curious_dynamic_array(&file_registry, &new_record, &blank_record, fd); - - // ...otherwise... - } else { - // ...all we need to do is intialise the feilds, - // since we already have space for a record where we need it - cur_record->path = strdup(path); - cur_record->filesystem = filesystem; - cur_record->mount_point = mount_point; - } - - return 0; +int curious_register_file(const char* path, int fd, char* filesystem, char* mount_point) +{ + curious_file_record_t* cur_record = (curious_file_record_t*) get_from_curious_dynamic_array(&file_registry, fd); + + // If the file is a pipe, then identify the pipe as the 'mount point' + if (NULL == mount_point && 0 == strncmp(path, "pipe:", 5)) { + mount_point = strdup(path); + } + + //printf("Registering %s on %s filesystem as fd %d\n", path, filesystem, fd); + + // If the fd hasn't been used before... + if (NULL == cur_record) { + // We need to set up a new record + curious_file_record_t new_record; + + // ...and intialise its fields + new_record.path = strdup(path); + new_record.filesystem = filesystem; + new_record.mount_point = mount_point; + + //Ditto for a blank record to fill any extra space created + //by adding this file record + curious_file_record_t blank_record; + blank_record.path = NULL; + blank_record.filesystem = NULL; + blank_record.mount_point = NULL; + + set_in_curious_dynamic_array(&file_registry, &new_record, &blank_record, fd); + + // ...otherwise... + } else { + // ...all we need to do is intialise the feilds, + // since we already have space for a record where we need it + cur_record->path = strdup(path); + cur_record->filesystem = filesystem; + cur_record->mount_point = mount_point; + } + + return 0; } -int curious_deregister_file(int fd) { - curious_file_record_t *cur_record = (curious_file_record_t *) get_from_curious_dynamic_array(&file_registry, fd); - - //printf("deregistering file %s on %s filesystem as %d\n", cur_record->path, cur_record->filesystem, fd); +int curious_deregister_file(int fd) +{ + curious_file_record_t* cur_record = (curious_file_record_t*) get_from_curious_dynamic_array(&file_registry, fd); + + //printf("deregistering file %s on %s filesystem as %d\n", cur_record->path, cur_record->filesystem, fd); - free(cur_record->path); - cur_record->path = NULL; - cur_record->filesystem = NULL; + free(cur_record->path); + cur_record->path = NULL; + cur_record->filesystem = NULL; - return 0; + return 0; } // This is mostly here so we can confile dirct access to the file registry to this file -curious_file_record_t * get_curious_file_record(int fd) { - return (curious_file_record_t *) get_from_curious_dynamic_array(&file_registry, fd); +curious_file_record_t* get_curious_file_record(int fd) +{ + return (curious_file_record_t*) get_from_curious_dynamic_array(&file_registry, fd); } diff --git a/src/services/io/CurIOus/src/file_registry.h b/src/services/io/CurIOus/src/file_registry.h index 17499a61b..f72804e85 100644 --- a/src/services/io/CurIOus/src/file_registry.h +++ b/src/services/io/CurIOus/src/file_registry.h @@ -1,29 +1,29 @@ #ifndef FILE_REGISTRY - #define FILE_REGISTRY +#define FILE_REGISTRY - #define STDIO_FILESYS "stdio" +#define STDIO_FILESYS "stdio" - /********* +/********* * Types * *********/ - typedef struct curious_file_record { - char *path; - char *filesystem; - char *mount_point; - } curious_file_record_t; +typedef struct curious_file_record { + char* path; + char* filesystem; + char* mount_point; +} curious_file_record_t; - /********************** +/********************** * Internal Functions * **********************/ - void curious_init_file_registry(); - void curious_finalize_file_registry(); - void destroy_curious_file_record(curious_file_record_t *record); - // Should be called whenever a new file is opened, - // so that we can store information on that file - int curious_register_file_by_fd(int fd); - int curious_register_file(const char *path, int fd, char *filesystem, char *mount_point); - int curious_deregister_file(int fd); - curious_file_record_t * get_curious_file_record(int fd); +void curious_init_file_registry(); +void curious_finalize_file_registry(); +void destroy_curious_file_record(curious_file_record_t* record); +// Should be called whenever a new file is opened, +// so that we can store information on that file +int curious_register_file_by_fd(int fd); +int curious_register_file(const char* path, int fd, char* filesystem, char* mount_point); +int curious_deregister_file(int fd); +curious_file_record_t* get_curious_file_record(int fd); #endif diff --git a/src/services/io/CurIOus/src/mount_tree.c b/src/services/io/CurIOus/src/mount_tree.c index 44f0090d9..7c1d33f8f 100644 --- a/src/services/io/CurIOus/src/mount_tree.c +++ b/src/services/io/CurIOus/src/mount_tree.c @@ -14,169 +14,176 @@ *********/ typedef struct mount_tree { - char *name; - char *full_path; - char *filesystem; - curious_dynamic_array_t children; + char* name; + char* full_path; + char* filesystem; + curious_dynamic_array_t children; } mount_tree_t; /********************** * Internal Functions * **********************/ -static void create_mount_tree(mount_tree_t *self, char *name, char *full_path, char *filesystem); -static void destroy_mount_tree(mount_tree_t *self); -static mount_tree_t * get_mount(char *full_path, char **name); -static void add_mount(char *full_path, char *filesystem); -static int compare_mount_tree_by_name(char *name, mount_tree_t *tree); +static void create_mount_tree(mount_tree_t* self, char* name, char* full_path, char* filesystem); +static void destroy_mount_tree(mount_tree_t* self); +static mount_tree_t* get_mount(char* full_path, char** name); +static void add_mount(char* full_path, char* filesystem); +static int compare_mount_tree_by_name(char* name, mount_tree_t* tree); static mount_tree_t root; -void curious_init_mount_tree(void) { - create_mount_tree(&root, ROOT_PATH, ROOT_PATH, ""); - - FILE *mount_file = fopen("/proc/mounts", "r"); - - ssize_t bytes_read; - char *line = NULL; - size_t size = 0; - #define VAL_SEP " " - // Read every line in the list of mounts - while (0 < (bytes_read = getline(&line, &size, mount_file))) { - // Each line of /proc/mounts is a list of space-seperated values - // We don't care about the first value, so just skip it - strtok(line, VAL_SEP); - - // The second element is the full path to the mount - char *full_path = strtok(NULL, VAL_SEP); - - // The third element is the type of filesystem the mount uses - char *filesystem = strtok(NULL, VAL_SEP); - - add_mount(full_path, filesystem); - } - - free(line); - fclose(mount_file); +void curious_init_mount_tree(void) +{ + create_mount_tree(&root, ROOT_PATH, ROOT_PATH, ""); + + FILE* mount_file = fopen("/proc/mounts", "r"); + + ssize_t bytes_read; + char* line = NULL; + size_t size = 0; +#define VAL_SEP " " + // Read every line in the list of mounts + while (0 < (bytes_read = getline(&line, &size, mount_file))) { + // Each line of /proc/mounts is a list of space-seperated values + // We don't care about the first value, so just skip it + strtok(line, VAL_SEP); + + // The second element is the full path to the mount + char* full_path = strtok(NULL, VAL_SEP); + + // The third element is the type of filesystem the mount uses + char* filesystem = strtok(NULL, VAL_SEP); + + add_mount(full_path, filesystem); + } + + free(line); + fclose(mount_file); } -void curious_finalize_mount_tree(void) { - destroy_mount_tree(&root); +void curious_finalize_mount_tree(void) +{ + destroy_mount_tree(&root); } -void create_mount_tree(mount_tree_t *self, char *name, char *full_path, char *filesystem) { - //printf("registering %s mount at %s\n", filesystem, full_path); - - self->name = strdup(name); - self->full_path = strdup(full_path); - self->filesystem = strdup(filesystem); - create_curious_dynamic_array(&self->children, 0, sizeof(mount_tree_t)); +void create_mount_tree(mount_tree_t* self, char* name, char* full_path, char* filesystem) +{ + //printf("registering %s mount at %s\n", filesystem, full_path); + + self->name = strdup(name); + self->full_path = strdup(full_path); + self->filesystem = strdup(filesystem); + create_curious_dynamic_array(&self->children, 0, sizeof(mount_tree_t)); } -void destroy_mount_tree(mount_tree_t *self) { - free(self->name); - free(self->full_path); - free(self->filesystem); - destroy_curious_dynamic_array(&self->children, (free_f) destroy_mount_tree); +void destroy_mount_tree(mount_tree_t* self) +{ + free(self->name); + free(self->full_path); + free(self->filesystem); + destroy_curious_dynamic_array(&self->children, (free_f) destroy_mount_tree); } -mount_tree_t * get_mount(char *full_path, char **name) { - if (0 == strcmp(full_path, ROOT_PATH)) { - return &root; - } - - mount_tree_t *cur_mount = &root; - mount_tree_t *next_mount; - #define DIR_SEP "/" - // Look at every path elemet (i.e. each level of directories) in the full path... - for (char *path_el = strtok(full_path, DIR_SEP); NULL != path_el; path_el = strtok(NULL, DIR_SEP)) { - // The name should always be the last path element we've looked at - if (NULL != name) { - *name = path_el; +mount_tree_t* get_mount(char* full_path, char** name) +{ + if (0 == strcmp(full_path, ROOT_PATH)) { + return &root; } - // ...see if there's a mount one level down which matches the current path element - next_mount = find_in_curious_dynamic_array(&cur_mount->children, - path_el, - (compare_f) compare_mount_tree_by_name); - // ...if we couldn't find a match, we've found the deepest relevant node, - // meaning that we're done - if (NULL == next_mount) { - return cur_mount; - - // ...otherwise, we want to look at the childreen of the matching mount - } else { - cur_mount = next_mount; + mount_tree_t* cur_mount = &root; + mount_tree_t* next_mount; +#define DIR_SEP "/" + // Look at every path elemet (i.e. each level of directories) in the full path... + for (char* path_el = strtok(full_path, DIR_SEP); NULL != path_el; path_el = strtok(NULL, DIR_SEP)) { + // The name should always be the last path element we've looked at + if (NULL != name) { + *name = path_el; + } + + // ...see if there's a mount one level down which matches the current path element + next_mount = + find_in_curious_dynamic_array(&cur_mount->children, path_el, (compare_f) compare_mount_tree_by_name); + // ...if we couldn't find a match, we've found the deepest relevant node, + // meaning that we're done + if (NULL == next_mount) { + return cur_mount; + + // ...otherwise, we want to look at the childreen of the matching mount + } else { + cur_mount = next_mount; + } } - } - return &root; + return &root; } -void add_mount(char *full_path, char *filesystem) { - // If the path in question is to the root, no need to go looking; - // we should just set its filesystem now - if (0 == strcmp(full_path, ROOT_PATH)) { - // make sure we don't overwrite that first setup - // (the actual root entry is proper the first) - if (0 != strcmp(root.filesystem, "")) { - free(root.filesystem); - root.filesystem = strdup(filesystem); +void add_mount(char* full_path, char* filesystem) +{ + // If the path in question is to the root, no need to go looking; + // we should just set its filesystem now + if (0 == strcmp(full_path, ROOT_PATH)) { + // make sure we don't overwrite that first setup + // (the actual root entry is proper the first) + if (0 != strcmp(root.filesystem, "")) { + free(root.filesystem); + root.filesystem = strdup(filesystem); + } + + return; + } + + char* name = NULL; + char* path_copy = strdup(full_path); + mount_tree_t* parent_mount = get_mount(path_copy, &name); + + if (NULL == parent_mount) { + printf("No relevant mount found for %s\n", full_path); + } else if (0 != strcmp(name, parent_mount->name)) { + // Make a new child mount of the parent we found + mount_tree_t mount; + create_mount_tree(&mount, name, full_path, filesystem); + append_to_curious_dynamic_array(&parent_mount->children, &mount); } - return; - } - - char *name = NULL; - char *path_copy = strdup(full_path); - mount_tree_t *parent_mount = get_mount(path_copy, &name); - - if (NULL == parent_mount) { - printf("No relevant mount found for %s\n", full_path); - } else if (0 != strcmp(name, parent_mount->name)) { - // Make a new child mount of the parent we found - mount_tree_t mount; - create_mount_tree(&mount, name, full_path, filesystem); - append_to_curious_dynamic_array(&parent_mount->children, &mount); - } - - // wait until after the mount tree nodehas been created to free this copy, - // since name is part of that buffer - free(path_copy); + // wait until after the mount tree nodehas been created to free this copy, + // since name is part of that buffer + free(path_copy); } -void curious_get_filesystem(const char *path, char **filesystem, char **mount_point) { - // get_mount uses strtok, which mangeles its input string, - // so we only want to pass in a copy - char *path_copy; - - // if the path starts from the root, it's absolute already, so just copy it - if ('/' == path[0]) { - path_copy = strdup(path); - - // otherwise, we need to atually find the absolute path - // and if we encounter an error, just set - } else { - path_copy = realpath(path, NULL); - - // if we can't resolve the absolute path, we can't find the proper mount - if (NULL == path_copy) { - *filesystem = NULL; - *mount_point = NULL; - return; +void curious_get_filesystem(const char* path, char** filesystem, char** mount_point) +{ + // get_mount uses strtok, which mangeles its input string, + // so we only want to pass in a copy + char* path_copy; + + // if the path starts from the root, it's absolute already, so just copy it + if ('/' == path[0]) { + path_copy = strdup(path); + + // otherwise, we need to atually find the absolute path + // and if we encounter an error, just set + } else { + path_copy = realpath(path, NULL); + + // if we can't resolve the absolute path, we can't find the proper mount + if (NULL == path_copy) { + *filesystem = NULL; + *mount_point = NULL; + return; + } } - } - // Just find the deepest mount in the given path - // and return which filesystem it uses - mount_tree_t *mount = get_mount(path_copy, NULL); + // Just find the deepest mount in the given path + // and return which filesystem it uses + mount_tree_t* mount = get_mount(path_copy, NULL); - // strdup and realpath both allocate a string, so we need to free it - free(path_copy); + // strdup and realpath both allocate a string, so we need to free it + free(path_copy); - *filesystem = mount->filesystem; - *mount_point = mount->full_path; + *filesystem = mount->filesystem; + *mount_point = mount->full_path; } -int compare_mount_tree_by_name(char *name, mount_tree_t *tree) { - return strcmp(name, tree->name); +int compare_mount_tree_by_name(char* name, mount_tree_t* tree) +{ + return strcmp(name, tree->name); } diff --git a/src/services/io/CurIOus/src/mount_tree.h b/src/services/io/CurIOus/src/mount_tree.h index 4c891b6cf..1e68ed2a2 100644 --- a/src/services/io/CurIOus/src/mount_tree.h +++ b/src/services/io/CurIOus/src/mount_tree.h @@ -1,9 +1,8 @@ #ifndef MOUNT_TREE - #define MOUNT_TREE - +#define MOUNT_TREE void curious_init_mount_tree(void); void curious_finalize_mount_tree(void); -void curious_get_filesystem(const char *full_path, char **filesystem, char **mount_point); +void curious_get_filesystem(const char* full_path, char** filesystem, char** mount_point); #endif diff --git a/src/services/io/CurIOus/src/wrappers.c b/src/services/io/CurIOus/src/wrappers.c index 575984ae5..a80e1be14 100644 --- a/src/services/io/CurIOus/src/wrappers.c +++ b/src/services/io/CurIOus/src/wrappers.c @@ -13,182 +13,186 @@ *********************/ // Typedefs just for easy conversion pf original functions -typedef ssize_t (*read_f)(int fd, void *buf, size_t count); -typedef ssize_t (*write_f)(int fd, const void *buf, size_t count); -typedef int (*open_f)(const char *pathname, int flags, mode_t mode); +typedef ssize_t (*read_f)(int fd, void* buf, size_t count); +typedef ssize_t (*write_f)(int fd, const void* buf, size_t count); +typedef int (*open_f)(const char* pathname, int flags, mode_t mode); typedef int (*close_f)(int fd); -typedef int (*stat_f)(const char *path, struct stat *buf); -typedef int (*xstat_f)(int vers, const char *path, struct stat *buf); -typedef int (*fstat_f)(int fd, struct stat *buf); -typedef int (*fxstat_f)(int vers, int fd, struct stat *buf); -typedef FILE * (*fopen_f)(const char *path, const char *mode); -typedef FILE * (*fdopen_f)(int fd, const char *mode); -typedef FILE * (*freopen_f)(const char *path, const char *mode, FILE *stream); -typedef int (*fclose_f)(FILE *fp); -typedef int (*vprintf_f)(const char *format, va_list ap); -typedef int (*vfprintf_f)(FILE *stream, const char *format, va_list ap); -typedef int (*vscanf_f)(const char *format, va_list ap); -typedef int (*vfscanf_f)(FILE *stream, const char *format, va_list ap); -typedef int (*fgetc_f)(FILE *stream); -typedef char * (*fgets_f)(char *s, int size, FILE *stream); +typedef int (*stat_f)(const char* path, struct stat* buf); +typedef int (*xstat_f)(int vers, const char* path, struct stat* buf); +typedef int (*fstat_f)(int fd, struct stat* buf); +typedef int (*fxstat_f)(int vers, int fd, struct stat* buf); +typedef FILE* (*fopen_f)(const char* path, const char* mode); +typedef FILE* (*fdopen_f)(int fd, const char* mode); +typedef FILE* (*freopen_f)(const char* path, const char* mode, FILE* stream); +typedef int (*fclose_f)(FILE* fp); +typedef int (*vprintf_f)(const char* format, va_list ap); +typedef int (*vfprintf_f)(FILE* stream, const char* format, va_list ap); +typedef int (*vscanf_f)(const char* format, va_list ap); +typedef int (*vfscanf_f)(FILE* stream, const char* format, va_list ap); +typedef int (*fgetc_f)(FILE* stream); +typedef char* (*fgets_f)(char* s, int size, FILE* stream); typedef int (*getchar_f)(void); -typedef char * (*gets_f)(char *s); -typedef size_t (*fread_f)(void *ptr, size_t size, size_t nmemb, FILE *stream); -typedef size_t (*fwrite_f)(void *ptr, size_t size, size_t nmemb, FILE *stream); +typedef char* (*gets_f)(char* s); +typedef size_t (*fread_f)(void* ptr, size_t size, size_t nmemb, FILE* stream); +typedef size_t (*fwrite_f)(void* ptr, size_t size, size_t nmemb, FILE* stream); // Actual declarations of wrappers // POSIX Wrappers: -static ssize_t wrapped_read(int fd, void *buf, size_t count); -static ssize_t wrapped_write(int fd, const void *buf, size_t count); -static int wrapped_open(const char *pathname, int flags, mode_t mode); -static int wrapped_close(int fd); -static int wrapped_stat(const char *path, struct stat *buf); -static int wrapped_xstat(int vers, const char *path, struct stat *buf); -static int wrapped_xstat64(int vers, const char *path, struct stat *buf); -static int wrapped_lstat(const char *path, struct stat *buf); -static int wrapped_lxstat(int vers, const char *path, struct stat *buf); -static int wrapped_lxstat64(int vers, const char *path, struct stat *buf); -static int wrapped_fstat(int fd, struct stat *buf); -static int wrapped_fxstat(int vers, int fd, struct stat *buf); -static int wrapped_fxstat64(int vers, int fd, struct stat *buf); +static ssize_t wrapped_read(int fd, void* buf, size_t count); +static ssize_t wrapped_write(int fd, const void* buf, size_t count); +static int wrapped_open(const char* pathname, int flags, mode_t mode); +static int wrapped_close(int fd); +static int wrapped_stat(const char* path, struct stat* buf); +static int wrapped_xstat(int vers, const char* path, struct stat* buf); +static int wrapped_xstat64(int vers, const char* path, struct stat* buf); +static int wrapped_lstat(const char* path, struct stat* buf); +static int wrapped_lxstat(int vers, const char* path, struct stat* buf); +static int wrapped_lxstat64(int vers, const char* path, struct stat* buf); +static int wrapped_fstat(int fd, struct stat* buf); +static int wrapped_fxstat(int vers, int fd, struct stat* buf); +static int wrapped_fxstat64(int vers, int fd, struct stat* buf); // C stdio Wrappers: -static FILE * wrapped_fopen(const char *path, const char *mode); -static FILE * wrapped_fdopen(int fd, const char *mode); -static FILE * wrapped_freopen(const char *path, const char *mode, FILE *stream); -static int wrapped_fclose(FILE *fp); -static int wrapped_printf(const char *format, ...); -static int wrapped_fprintf(FILE *stream, const char *format, ...); -static int wrapped_vprintf(const char *format, va_list ap); -static int wrapped_vfprintf(FILE *stream, const char *format, va_list ap); +static FILE* wrapped_fopen(const char* path, const char* mode); +static FILE* wrapped_fdopen(int fd, const char* mode); +static FILE* wrapped_freopen(const char* path, const char* mode, FILE* stream); +static int wrapped_fclose(FILE* fp); +static int wrapped_printf(const char* format, ...); +static int wrapped_fprintf(FILE* stream, const char* format, ...); +static int wrapped_vprintf(const char* format, va_list ap); +static int wrapped_vfprintf(FILE* stream, const char* format, va_list ap); /* int wrapped_scanf(const char *format, ...); int wrapped_fscanf(FILE *stream, const char *format, ...); int wrapped_vscanf(const char *format, va_list ap); int wrapped_vfscanf(FILE *stream, const char *format, va_list ap); */ -static int wrapped_fgetc(FILE *stream); -static char * wrapped_fgets(char *s, int size, FILE *stream); -static int wrapped_getchar(void); +static int wrapped_fgetc(FILE* stream); +static char* wrapped_fgets(char* s, int size, FILE* stream); +static int wrapped_getchar(void); //char * wrapped_gets(char *s); -static size_t wrapped_fread(void *ptr, size_t size, size_t nmemb, FILE *stream); -static size_t wrapped_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream); - +static size_t wrapped_fread(void* ptr, size_t size, size_t nmemb, FILE* stream); +static size_t wrapped_fwrite(void* ptr, size_t size, size_t nmemb, FILE* stream); + // Note that the handles need not be intialised, just defined io_function_data_t io_fns[CURIOUS_FUNCTION_COUNT] = { - [CURIOUS_READ_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_WRITE_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_OPEN_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_CLOSE_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_STAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__STAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_LSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__LSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_XSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__XSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_XSTAT64_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__XSTAT64_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_LXSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__LXSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_LXSTAT64_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__LXSTAT64_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_FSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__FSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_FXSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__FXSTAT_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_FXSTAT64_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS__FXSTAT64_ID] = { CURIOUS_POSIX, 0 }, - [CURIOUS_FOPEN_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FOPEN64_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FDOPEN_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FREOPEN_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FCLOSE_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_PRINTF_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FPRINTF_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_VPRINTF_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_VFPRINTF_ID] = { CURIOUS_CSTDIO, 0 }, - /* + [CURIOUS_READ_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_WRITE_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_OPEN_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_CLOSE_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_STAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__STAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_LSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__LSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_XSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__XSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_XSTAT64_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__XSTAT64_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_LXSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__LXSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_LXSTAT64_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__LXSTAT64_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_FSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__FSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_FXSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__FXSTAT_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_FXSTAT64_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS__FXSTAT64_ID] = { CURIOUS_POSIX, 0 }, + [CURIOUS_FOPEN_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FOPEN64_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FDOPEN_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FREOPEN_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FCLOSE_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_PRINTF_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FPRINTF_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_VPRINTF_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_VFPRINTF_ID] = { CURIOUS_CSTDIO, 0 }, + /* [CURIOUS_SCANF_ID] = { CURIOUS_CSTDIO }, [CURIOUS_FSCANF_ID] = { CURIOUS_CSTDIO }, [CURIOUS_VSCANF_ID] = { CURIOUS_CSTDIO }, [CURIOUS_VFSCANF_ID] = { CURIOUS_CSTDIO }, */ - [CURIOUS_FGETC_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FGETS_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_GETCHAR_ID] = { CURIOUS_CSTDIO, 0 }, -//[CURIOUS_GETS_ID] = { CURIOUS_CSTDIO }, - [CURIOUS_FREAD_ID] = { CURIOUS_CSTDIO, 0 }, - [CURIOUS_FWRITE_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FGETC_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FGETS_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_GETCHAR_ID] = { CURIOUS_CSTDIO, 0 }, + //[CURIOUS_GETS_ID] = { CURIOUS_CSTDIO }, + [CURIOUS_FREAD_ID] = { CURIOUS_CSTDIO, 0 }, + [CURIOUS_FWRITE_ID] = { CURIOUS_CSTDIO, 0 }, }; // Just a shortcut to get the bindings right for all the functions -#define GET_BINDING(lower, upper) \ - { #lower, (void *) wrapped_##lower, &io_fns[CURIOUS_##upper##_ID].handle } +#define GET_BINDING(lower, upper) \ + { \ + #lower, (void*) wrapped_##lower, &io_fns[CURIOUS_##upper##_ID].handle \ + } static gotcha_binding_t bindings[CURIOUS_FUNCTION_COUNT] = { - [CURIOUS_READ_ID] = GET_BINDING(read,READ), -// GET_BINDING(read,READ), expands to { "read", read_wrapper, &io_fns[CURIOUS_READ_ID].handle } - [CURIOUS_WRITE_ID] = GET_BINDING(write,WRITE), - [CURIOUS_OPEN_ID] = GET_BINDING(open,OPEN), - [CURIOUS_CLOSE_ID] = GET_BINDING(close,CLOSE), - [CURIOUS_STAT_ID] = GET_BINDING(stat,STAT), - [CURIOUS__STAT_ID] = { "__stat", (void *) wrapped_stat, &io_fns[CURIOUS_STAT_ID].handle }, - [CURIOUS_LSTAT_ID] = GET_BINDING(lstat,LSTAT), - [CURIOUS__LSTAT_ID] = { "__lstat", (void *) wrapped_lstat, &io_fns[CURIOUS_LSTAT_ID].handle }, - [CURIOUS_XSTAT_ID] = GET_BINDING(xstat,XSTAT), - [CURIOUS__XSTAT_ID] = { "__xstat", (void *) wrapped_xstat, &io_fns[CURIOUS_XSTAT_ID].handle }, - [CURIOUS_XSTAT64_ID] = GET_BINDING(xstat64,XSTAT64), - [CURIOUS__XSTAT64_ID] = { "__xstat64", (void *) wrapped_xstat64, &io_fns[CURIOUS_XSTAT64_ID].handle }, - [CURIOUS_LXSTAT_ID] = GET_BINDING(lxstat,LXSTAT), - [CURIOUS__LXSTAT_ID] = { "__lxstat", (void *) wrapped_lxstat, &io_fns[CURIOUS_LXSTAT_ID].handle }, - [CURIOUS_LXSTAT64_ID] = GET_BINDING(lxstat64,LXSTAT64), - [CURIOUS__LXSTAT64_ID] = { "__lxstat64", (void *) wrapped_lxstat64, &io_fns[CURIOUS_LXSTAT64_ID].handle }, - [CURIOUS_FSTAT_ID] = GET_BINDING(fstat,FSTAT), - [CURIOUS__FSTAT_ID] = { "__fstat", (void *) wrapped_fstat, &io_fns[CURIOUS_FSTAT_ID].handle }, - [CURIOUS_FXSTAT_ID] = GET_BINDING(fxstat,FXSTAT), - [CURIOUS__FXSTAT_ID] = { "__fxstat", (void *) wrapped_fxstat, &io_fns[CURIOUS_FXSTAT_ID].handle }, - [CURIOUS_FXSTAT64_ID] = GET_BINDING(fxstat64,FXSTAT64), - [CURIOUS__FXSTAT64_ID] = { "__fxstat64", (void *) wrapped_fxstat64, &io_fns[CURIOUS_FXSTAT64_ID].handle }, - [CURIOUS_FOPEN_ID] = GET_BINDING(fopen,FOPEN), - [CURIOUS_FOPEN64_ID] = { "fopen64", (void *) wrapped_fopen, &io_fns[CURIOUS_FOPEN64_ID].handle }, - [CURIOUS_FDOPEN_ID] = GET_BINDING(fdopen,FDOPEN), - [CURIOUS_FREOPEN_ID] = GET_BINDING(freopen,FREOPEN), - [CURIOUS_FCLOSE_ID] = GET_BINDING(fclose,FCLOSE), - [CURIOUS_PRINTF_ID] = GET_BINDING(printf,PRINTF), - [CURIOUS_FPRINTF_ID] = GET_BINDING(fprintf,FPRINTF), - [CURIOUS_VPRINTF_ID] = GET_BINDING(vprintf,VPRINTF), - [CURIOUS_VFPRINTF_ID] = GET_BINDING(vfprintf,VFPRINTF), - /* + [CURIOUS_READ_ID] = GET_BINDING(read, READ), + // GET_BINDING(read,READ), expands to { "read", read_wrapper, &io_fns[CURIOUS_READ_ID].handle } + [CURIOUS_WRITE_ID] = GET_BINDING(write, WRITE), + [CURIOUS_OPEN_ID] = GET_BINDING(open, OPEN), + [CURIOUS_CLOSE_ID] = GET_BINDING(close, CLOSE), + [CURIOUS_STAT_ID] = GET_BINDING(stat, STAT), + [CURIOUS__STAT_ID] = { "__stat", (void*) wrapped_stat, &io_fns[CURIOUS_STAT_ID].handle }, + [CURIOUS_LSTAT_ID] = GET_BINDING(lstat, LSTAT), + [CURIOUS__LSTAT_ID] = { "__lstat", (void*) wrapped_lstat, &io_fns[CURIOUS_LSTAT_ID].handle }, + [CURIOUS_XSTAT_ID] = GET_BINDING(xstat, XSTAT), + [CURIOUS__XSTAT_ID] = { "__xstat", (void*) wrapped_xstat, &io_fns[CURIOUS_XSTAT_ID].handle }, + [CURIOUS_XSTAT64_ID] = GET_BINDING(xstat64, XSTAT64), + [CURIOUS__XSTAT64_ID] = { "__xstat64", (void*) wrapped_xstat64, &io_fns[CURIOUS_XSTAT64_ID].handle }, + [CURIOUS_LXSTAT_ID] = GET_BINDING(lxstat, LXSTAT), + [CURIOUS__LXSTAT_ID] = { "__lxstat", (void*) wrapped_lxstat, &io_fns[CURIOUS_LXSTAT_ID].handle }, + [CURIOUS_LXSTAT64_ID] = GET_BINDING(lxstat64, LXSTAT64), + [CURIOUS__LXSTAT64_ID] = { "__lxstat64", (void*) wrapped_lxstat64, &io_fns[CURIOUS_LXSTAT64_ID].handle }, + [CURIOUS_FSTAT_ID] = GET_BINDING(fstat, FSTAT), + [CURIOUS__FSTAT_ID] = { "__fstat", (void*) wrapped_fstat, &io_fns[CURIOUS_FSTAT_ID].handle }, + [CURIOUS_FXSTAT_ID] = GET_BINDING(fxstat, FXSTAT), + [CURIOUS__FXSTAT_ID] = { "__fxstat", (void*) wrapped_fxstat, &io_fns[CURIOUS_FXSTAT_ID].handle }, + [CURIOUS_FXSTAT64_ID] = GET_BINDING(fxstat64, FXSTAT64), + [CURIOUS__FXSTAT64_ID] = { "__fxstat64", (void*) wrapped_fxstat64, &io_fns[CURIOUS_FXSTAT64_ID].handle }, + [CURIOUS_FOPEN_ID] = GET_BINDING(fopen, FOPEN), + [CURIOUS_FOPEN64_ID] = { "fopen64", (void*) wrapped_fopen, &io_fns[CURIOUS_FOPEN64_ID].handle }, + [CURIOUS_FDOPEN_ID] = GET_BINDING(fdopen, FDOPEN), + [CURIOUS_FREOPEN_ID] = GET_BINDING(freopen, FREOPEN), + [CURIOUS_FCLOSE_ID] = GET_BINDING(fclose, FCLOSE), + [CURIOUS_PRINTF_ID] = GET_BINDING(printf, PRINTF), + [CURIOUS_FPRINTF_ID] = GET_BINDING(fprintf, FPRINTF), + [CURIOUS_VPRINTF_ID] = GET_BINDING(vprintf, VPRINTF), + [CURIOUS_VFPRINTF_ID] = GET_BINDING(vfprintf, VFPRINTF), + /* [CURIOUS_SCANF_ID] = GET_BINDING(scanf,SCANF), [CURIOUS_FSCANF_ID] = GET_BINDING(fscanf,FSCANF), [CURIOUS_VSCANF_ID] = GET_BINDING(vscanf,VSCANF), [CURIOUS_VFSCANF_ID] = GET_BINDING(vfscanf,VFSCANF), */ - [CURIOUS_FGETC_ID] = GET_BINDING(fgetc,FGETC), - [CURIOUS_FGETS_ID] = GET_BINDING(fgets,FGETS), - [CURIOUS_GETCHAR_ID] = GET_BINDING(getchar,GETCHAR), -//[CURIOUS_GETS_ID] = GET_BINDING(gets,GETS), - [CURIOUS_FREAD_ID] = GET_BINDING(fread,FREAD), - [CURIOUS_FWRITE_ID] = GET_BINDING(fwrite,FWRITE), + [CURIOUS_FGETC_ID] = GET_BINDING(fgetc, FGETC), + [CURIOUS_FGETS_ID] = GET_BINDING(fgets, FGETS), + [CURIOUS_GETCHAR_ID] = GET_BINDING(getchar, GETCHAR), + //[CURIOUS_GETS_ID] = GET_BINDING(gets,GETS), + [CURIOUS_FREAD_ID] = GET_BINDING(fread, FREAD), + [CURIOUS_FWRITE_ID] = GET_BINDING(fwrite, FWRITE), }; // Just a boolean to control whether or not wrappers do anything or not static volatile sig_atomic_t wrappers_enabled; -void curious_apply_wrappers(curious_apis_t apis) { - if (apis & CURIOUS_POSIX) { - gotcha_wrap(&bindings[CURIOUS_POSIX_START], CURIOUS_CSTDIO_START - CURIOUS_POSIX_START, "curious"); - } - if (apis & CURIOUS_CSTDIO) { - gotcha_wrap(&bindings[CURIOUS_CSTDIO_START], CURIOUS_FUNCTION_COUNT - CURIOUS_CSTDIO_START, "curious"); - } +void curious_apply_wrappers(curious_apis_t apis) +{ + if (apis & CURIOUS_POSIX) { + gotcha_wrap(&bindings[CURIOUS_POSIX_START], CURIOUS_CSTDIO_START - CURIOUS_POSIX_START, "curious"); + } + if (apis & CURIOUS_CSTDIO) { + gotcha_wrap(&bindings[CURIOUS_CSTDIO_START], CURIOUS_FUNCTION_COUNT - CURIOUS_CSTDIO_START, "curious"); + } - wrappers_enabled = 1; + wrappers_enabled = 1; } -void curious_disable_wrappers(void) { - wrappers_enabled = 0; +void curious_disable_wrappers(void) +{ + wrappers_enabled = 0; } // Keeps track of how many wrapper calls deep each thread is; @@ -200,841 +204,860 @@ static __thread volatile sig_atomic_t wrapper_call_depth; #endif #define WRAPPER_ENTER(wrapper) \ - wrapper_call_depth++; \ - /* printf("entering %s wrapper\n", #wrapper); */ + wrapper_call_depth++; \ + /* printf("entering %s wrapper\n", #wrapper); */ // Just to make sure we decremint call depth before returning -#define WRAPPER_RETURN(return_val, wrapper) \ - /* printf("exiting %s wrapper\n", #wrapper); */ \ - wrapper_call_depth--; \ - return return_val; - -ssize_t wrapped_read(int fd, void *buf, size_t count) { - WRAPPER_ENTER(read); - - // Get the handle for the original function - read_f orig_read = (read_f) gotcha_get_wrappee(io_fns[CURIOUS_READ_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +#define WRAPPER_RETURN(return_val, wrapper) \ + /* printf("exiting %s wrapper\n", #wrapper); */ \ + wrapper_call_depth--; \ + return return_val; + +ssize_t wrapped_read(int fd, void* buf, size_t count) +{ + WRAPPER_ENTER(read); + + // Get the handle for the original function + read_f orig_read = (read_f) gotcha_get_wrappee(io_fns[CURIOUS_READ_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_read_record_t io_args = { + .bytes_read = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_READ_ID, + }; + curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); + + // Call the original, saving the result... + io_args.bytes_read = orig_read(fd, buf, count); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(io_args.bytes_read, read); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_read(fd, buf, count), read); } - - curious_read_record_t io_args = { - .bytes_read = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_READ_ID, - }; - curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); - - // Call the original, saving the result... - io_args.bytes_read = orig_read(fd, buf, count); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(io_args.bytes_read, read); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_read(fd, buf, count), read); - } } -ssize_t wrapped_write(int fd, const void *buf, size_t count) { - WRAPPER_ENTER(write); - - // Get the handle for the original function - write_f orig_write = (write_f) gotcha_get_wrappee(io_fns[CURIOUS_WRITE_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +ssize_t wrapped_write(int fd, const void* buf, size_t count) +{ + WRAPPER_ENTER(write); + + // Get the handle for the original function + write_f orig_write = (write_f) gotcha_get_wrappee(io_fns[CURIOUS_WRITE_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_write_record_t io_args = { + .bytes_written = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_WRITE_ID, + }; + curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + // Call the original, saving the result... + io_args.bytes_written = orig_write(fd, buf, count); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(io_args.bytes_written, write); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_write(fd, buf, count), write); } - - curious_write_record_t io_args = { - .bytes_written = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_WRITE_ID, - }; - curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); - - // Call the original, saving the result... - io_args.bytes_written = orig_write(fd, buf, count); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(io_args.bytes_written, write); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_write(fd, buf, count), write); - } -} - -int wrapped_open(const char *pathname, int flags, mode_t mode) { - WRAPPER_ENTER(open); - - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_OPEN_ID, - }; - - if (1 == wrapper_call_depth && wrappers_enabled) { - // NOTE: calling realpath on pathname before passing it to curious_get_filesystem - // might be necessary in order to handle relative paths_ - - curious_get_filesystem(pathname, &io_args.filesystem, &io_args.mount_point); - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - } - - // Get the handle for the original function - open_f orig_open = (open_f) gotcha_get_wrappee(io_fns[CURIOUS_OPEN_ID].handle); - - // Call the original, saving the result... - int fd = orig_open(pathname, flags, mode); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_register_file_by_fd(fd); - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - } - - WRAPPER_RETURN(fd, open); -} - -int wrapped_close(int fd) { - WRAPPER_ENTER(close); - - // Get the handle for the original function - close_f orig_close = (close_f) gotcha_get_wrappee(io_fns[CURIOUS_CLOSE_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; - } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } - - curious_metadata_record_t io_args = { - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_CLOSE_ID, - }; - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_close(fd); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - - WRAPPER_RETURN(return_val, close); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_close(fd), close); - } -} - -int wrapped_stat(const char *path, struct stat *buf) { - WRAPPER_ENTER(stat); - - // Get the handle for the original function - stat_f orig_stat = (stat_f) gotcha_get_wrappee(io_fns[CURIOUS_STAT_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_STAT_ID, - }; - - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_stat(path, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, stat); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_stat(path, buf), stat); - } -} - -int wrapped_lstat(const char *path, struct stat *buf) { - WRAPPER_ENTER(lstat); - - // Get the handle for the original function - stat_f orig_lstat = (stat_f) gotcha_get_wrappee(io_fns[CURIOUS_LSTAT_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_LSTAT_ID, - }; - - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_lstat(path, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, lstat); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_lstat(path, buf), lstat); - } -} - -int wrapped_xstat(int vers, const char *path, struct stat *buf) { - WRAPPER_ENTER(xstat); - - // Get the handle for the original function - xstat_f orig_xstat = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_XSTAT_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_XSTAT_ID, - }; - - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_xstat(vers, path, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, xstat); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_xstat(vers, path, buf), xstat); - } -} - -int wrapped_xstat64(int vers, const char *path, struct stat *buf) { - WRAPPER_ENTER(xstat64); - - // Get the handle for the original function - xstat_f orig_xstat64 = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_XSTAT64_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_XSTAT64_ID, - }; - - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_xstat64(vers, path, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, xstat64); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_xstat64(vers, path, buf), xstat64); - } } -int wrapped_lxstat(int vers, const char *path, struct stat *buf) { - WRAPPER_ENTER(lxstat); - - // Get the handle for the original function - xstat_f orig_lxstat = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_LXSTAT_ID].handle); +int wrapped_open(const char* pathname, int flags, mode_t mode) +{ + WRAPPER_ENTER(open); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_LXSTAT_ID, + .call_count = 0, + .function_id = CURIOUS_OPEN_ID, }; - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_lxstat(vers, path, buf); - io_args.call_count = 1; + if (1 == wrapper_call_depth && wrappers_enabled) { + // NOTE: calling realpath on pathname before passing it to curious_get_filesystem + // might be necessary in order to handle relative paths_ - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, lxstat); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_lxstat(vers, path, buf), lxstat); - } -} - -int wrapped_lxstat64(int vers, const char *path, struct stat *buf) { - WRAPPER_ENTER(xstat); - - // Get the handle for the original function - xstat_f orig_lxstat64 = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_XSTAT_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_LXSTAT64_ID, - }; - - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); + curious_get_filesystem(pathname, &io_args.filesystem, &io_args.mount_point); + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + } - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + // Get the handle for the original function + open_f orig_open = (open_f) gotcha_get_wrappee(io_fns[CURIOUS_OPEN_ID].handle); // Call the original, saving the result... - int return_val = orig_lxstat64(vers, path, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + int fd = orig_open(pathname, flags, mode); - WRAPPER_RETURN(return_val, lxstat64); + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_register_file_by_fd(fd); + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + } - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_lxstat64(vers, path, buf), lxstat64); - } + WRAPPER_RETURN(fd, open); } -int wrapped_fstat(int fd, struct stat *buf) { - WRAPPER_ENTER(fstat); - - // Get the handle for the original function - fstat_f orig_fstat = (fstat_f) gotcha_get_wrappee(io_fns[CURIOUS_FSTAT_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +int wrapped_close(int fd) +{ + WRAPPER_ENTER(close); + + // Get the handle for the original function + close_f orig_close = (close_f) gotcha_get_wrappee(io_fns[CURIOUS_CLOSE_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_metadata_record_t io_args = { + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_CLOSE_ID, + }; + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_close(fd); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, close); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_close(fd), close); } - - curious_metadata_record_t io_args = { - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FSTAT_ID, - }; - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_fstat(fd, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, fstat); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fstat(fd, buf), fstat); - } } -int wrapped_fxstat(int vers, int fd, struct stat *buf) { - WRAPPER_ENTER(fxstat); - - // Get the handle for the original function - fxstat_f orig_fxstat = (fxstat_f) gotcha_get_wrappee(io_fns[CURIOUS_FXSTAT_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; - } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } +int wrapped_stat(const char* path, struct stat* buf) +{ + WRAPPER_ENTER(stat); - curious_metadata_record_t io_args = { - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FXSTAT_ID, - }; - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + // Get the handle for the original function + stat_f orig_stat = (stat_f) gotcha_get_wrappee(io_fns[CURIOUS_STAT_ID].handle); - // Call the original, saving the result... - int return_val = orig_fxstat(vers, fd, buf); - io_args.call_count = 1; + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_STAT_ID, + }; - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - WRAPPER_RETURN(return_val, fxstat); + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fxstat(vers, fd, buf), fxstat); - } -} + // Call the original, saving the result... + int return_val = orig_stat(path, buf); + io_args.call_count = 1; -int wrapped_fxstat64(int vers, int fd, struct stat *buf) { - WRAPPER_ENTER(fxstat64); + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - // Get the handle for the original function - fxstat_f orig_fxstat64 = (fxstat_f) gotcha_get_wrappee(io_fns[CURIOUS_FXSTAT64_ID].handle); + WRAPPER_RETURN(return_val, stat); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_stat(path, buf), stat); } - - curious_metadata_record_t io_args = { - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FXSTAT64_ID, - }; - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_fxstat64(vers, fd, buf); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, fxstat64); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fxstat64(vers, fd, buf), fxstat64); - } } -FILE * wrapped_fopen(const char *path, const char *mode) { - WRAPPER_ENTER(fopen); - - // Get the handle for the original function - fopen_f orig_fopen = (fopen_f) gotcha_get_wrappee(io_fns[CURIOUS_FOPEN_ID].handle); - - // NOTE: calling realpath on pathname before passing it to curious_get_filesystem - // might be necessary in order to handle relative paths_ +int wrapped_lstat(const char* path, struct stat* buf) +{ + WRAPPER_ENTER(lstat); - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_FOPEN_ID, - }; + // Get the handle for the original function + stat_f orig_lstat = (stat_f) gotcha_get_wrappee(io_fns[CURIOUS_LSTAT_ID].handle); - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_LSTAT_ID, + }; - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - } - - // Call the original, saving the result... - FILE *file = orig_fopen(path, mode); - - if (NULL != file) { - curious_register_file_by_fd(fileno(file)); - } + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - } + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - WRAPPER_RETURN(file, fopen); -} + // Call the original, saving the result... + int return_val = orig_lstat(path, buf); + io_args.call_count = 1; -FILE * wrapped_fdopen(int fd, const char *mode) { - WRAPPER_ENTER(fdopen); + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - // Get the handle for the original function - fdopen_f orig_fdopen = (fdopen_f) gotcha_get_wrappee(io_fns[CURIOUS_FDOPEN_ID].handle); + WRAPPER_RETURN(return_val, lstat); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fd); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_lstat(path, buf), lstat); } - - curious_metadata_record_t io_args = { - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FDOPEN_ID, - }; - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - FILE *file = orig_fdopen(fd, mode); - io_args.call_count = 1; - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(file, fdopen); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fdopen(fd, mode), fdopen); - } } -FILE * wrapped_freopen(const char *path, const char *mode, FILE *stream) { - WRAPPER_ENTER(freopen); - - // Get the handle for the original function - freopen_f orig_freopen = (freopen_f) gotcha_get_wrappee(io_fns[CURIOUS_FREOPEN_ID].handle); - - // NOTE: calling realpath on pathname before passing it to curious_get_filesystem - // might be necessary in order to handle relative paths_ - - curious_metadata_record_t io_args = { - .call_count = 0, - .function_id = CURIOUS_FREOPEN_ID, - }; +int wrapped_xstat(int vers, const char* path, struct stat* buf) +{ + WRAPPER_ENTER(xstat); - curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); + // Get the handle for the original function + xstat_f orig_xstat = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_XSTAT_ID].handle); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - } + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_XSTAT_ID, + }; - // Call the original, saving the result... - FILE *file = orig_freopen(path, mode, stream); + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - if (NULL != file) { - curious_register_file_by_fd(fileno(file)); - } - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - } + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - WRAPPER_RETURN(file, freopen); -} + // Call the original, saving the result... + int return_val = orig_xstat(vers, path, buf); + io_args.call_count = 1; -int wrapped_fclose(FILE *fp) { - WRAPPER_ENTER(fclose); + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - // Get the handle for the original function - fclose_f orig_fclose = (fclose_f) gotcha_get_wrappee(io_fns[CURIOUS_FCLOSE_ID].handle); + WRAPPER_RETURN(return_val, xstat); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(fp)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_xstat(vers, path, buf), xstat); } +} - curious_deregister_file(fileno(fp)); - - curious_metadata_record_t io_args = { - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FCLOSE_ID, - }; - curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_fclose(fp); - io_args.call_count = 1; +int wrapped_xstat64(int vers, const char* path, struct stat* buf) +{ + WRAPPER_ENTER(xstat64); - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + // Get the handle for the original function + xstat_f orig_xstat64 = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_XSTAT64_ID].handle); + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_XSTAT64_ID, + }; - WRAPPER_RETURN(return_val, fclose); + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fclose(fp), fclose); - } -} + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); -int wrapped_printf(const char *format, ...) { - WRAPPER_ENTER(printf); + // Call the original, saving the result... + int return_val = orig_xstat64(vers, path, buf); + io_args.call_count = 1; - // Get the handle for the original function - vprintf_f orig_vprintf = (vprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VPRINTF_ID].handle); + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - // Get the variable-length argument list set up - va_list args; - va_start(args, format); + WRAPPER_RETURN(return_val, xstat64); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(STDOUT_FILENO); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_xstat64(vers, path, buf), xstat64); } +} - curious_write_record_t io_args = { - .bytes_written = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_PRINTF_ID, - }; - curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); +int wrapped_lxstat(int vers, const char* path, struct stat* buf) +{ + WRAPPER_ENTER(lxstat); - // Call the original, saving the result... - int return_val = orig_vprintf(format, args); - io_args.call_count = 1; - if (return_val > 0) { - io_args.bytes_written = return_val; - } + // Get the handle for the original function + xstat_f orig_lxstat = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_LXSTAT_ID].handle); - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_LXSTAT_ID, + }; - WRAPPER_RETURN(return_val, printf); + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_vprintf(format, args), printf); - } -} + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); -int wrapped_fprintf(FILE *stream, const char *format, ...) { - WRAPPER_ENTER(fprintf); + // Call the original, saving the result... + int return_val = orig_lxstat(vers, path, buf); + io_args.call_count = 1; - // Get the handle for the original function - vfprintf_f orig_vfprintf = (vfprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VFPRINTF_ID].handle); + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - // Get the variable-length argument list set up - va_list args; - va_start(args, format); + WRAPPER_RETURN(return_val, lxstat); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(stream)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_lxstat(vers, path, buf), lxstat); } +} - curious_write_record_t io_args = { - .bytes_written = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FPRINTF_ID, - }; - curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); +int wrapped_lxstat64(int vers, const char* path, struct stat* buf) +{ + WRAPPER_ENTER(xstat); - // Call the original, saving the result... - int return_val = orig_vfprintf(stream, format, args); - io_args.call_count = 1; - if (return_val > 0) { - io_args.bytes_written = return_val; - } + // Get the handle for the original function + xstat_f orig_lxstat64 = (xstat_f) gotcha_get_wrappee(io_fns[CURIOUS_XSTAT_ID].handle); - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_LXSTAT64_ID, + }; - WRAPPER_RETURN(return_val, fprintf); + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_vfprintf(stream, format, args), fprintf); - } + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_lxstat64(vers, path, buf); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, lxstat64); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_lxstat64(vers, path, buf), lxstat64); + } } -int wrapped_vprintf(const char *format, va_list ap) { - WRAPPER_ENTER(vprintf); +int wrapped_fstat(int fd, struct stat* buf) +{ + WRAPPER_ENTER(fstat); + + // Get the handle for the original function + fstat_f orig_fstat = (fstat_f) gotcha_get_wrappee(io_fns[CURIOUS_FSTAT_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_metadata_record_t io_args = { + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FSTAT_ID, + }; + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_fstat(fd, buf); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fstat); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_fstat(fd, buf), fstat); + } +} - // Get the handle for the original function - vprintf_f orig_vprintf = (vprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VPRINTF_ID].handle); +int wrapped_fxstat(int vers, int fd, struct stat* buf) +{ + WRAPPER_ENTER(fxstat); + + // Get the handle for the original function + fxstat_f orig_fxstat = (fxstat_f) gotcha_get_wrappee(io_fns[CURIOUS_FXSTAT_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_metadata_record_t io_args = { + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FXSTAT_ID, + }; + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_fxstat(vers, fd, buf); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fxstat); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_fxstat(vers, fd, buf), fxstat); + } +} - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(STDOUT_FILENO); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +int wrapped_fxstat64(int vers, int fd, struct stat* buf) +{ + WRAPPER_ENTER(fxstat64); + + // Get the handle for the original function + fxstat_f orig_fxstat64 = (fxstat_f) gotcha_get_wrappee(io_fns[CURIOUS_FXSTAT64_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_metadata_record_t io_args = { + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FXSTAT64_ID, + }; + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_fxstat64(vers, fd, buf); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fxstat64); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; + WRAPPER_RETURN(orig_fxstat64(vers, fd, buf), fxstat64); } +} - curious_write_record_t io_args = { - .bytes_written = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_VPRINTF_ID, +FILE* wrapped_fopen(const char* path, const char* mode) +{ + WRAPPER_ENTER(fopen); + + // Get the handle for the original function + fopen_f orig_fopen = (fopen_f) gotcha_get_wrappee(io_fns[CURIOUS_FOPEN_ID].handle); + + // NOTE: calling realpath on pathname before passing it to curious_get_filesystem + // might be necessary in order to handle relative paths_ + + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_FOPEN_ID, }; - curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + } // Call the original, saving the result... - int return_val = orig_vprintf(format, ap); - io_args.call_count = 1; - if (return_val > 0) { - io_args.bytes_written = return_val; + FILE* file = orig_fopen(path, mode); + + if (NULL != file) { + curious_register_file_by_fd(fileno(file)); } - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + } - WRAPPER_RETURN(return_val, vprintf); + WRAPPER_RETURN(file, fopen); +} - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_vprintf(format, ap), vprintf); - } +FILE* wrapped_fdopen(int fd, const char* mode) +{ + WRAPPER_ENTER(fdopen); + + // Get the handle for the original function + fdopen_f orig_fdopen = (fdopen_f) gotcha_get_wrappee(io_fns[CURIOUS_FDOPEN_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fd); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_metadata_record_t io_args = { + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FDOPEN_ID, + }; + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + FILE* file = orig_fdopen(fd, mode); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(file, fdopen); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_fdopen(fd, mode), fdopen); + } } -int wrapped_vfprintf(FILE *stream, const char *format, va_list ap) { - WRAPPER_ENTER(vfprintf); +FILE* wrapped_freopen(const char* path, const char* mode, FILE* stream) +{ + WRAPPER_ENTER(freopen); - // Get the handle for the original function - vfprintf_f orig_vfprintf = (vfprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VFPRINTF_ID].handle); + // Get the handle for the original function + freopen_f orig_freopen = (freopen_f) gotcha_get_wrappee(io_fns[CURIOUS_FREOPEN_ID].handle); - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(stream)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; - } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } + // NOTE: calling realpath on pathname before passing it to curious_get_filesystem + // might be necessary in order to handle relative paths_ - curious_write_record_t io_args = { - .bytes_written = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_VFPRINTF_ID, + curious_metadata_record_t io_args = { + .call_count = 0, + .function_id = CURIOUS_FREOPEN_ID, }; - curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + curious_get_filesystem(path, &io_args.filesystem, &io_args.mount_point); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + } // Call the original, saving the result... - int return_val = orig_vfprintf(stream, format, ap); - io_args.call_count = 1; - if (return_val > 0) { - io_args.bytes_written = return_val; + FILE* file = orig_freopen(path, mode, stream); + + if (NULL != file) { + curious_register_file_by_fd(fileno(file)); + } + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); } - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + WRAPPER_RETURN(file, freopen); +} + +int wrapped_fclose(FILE* fp) +{ + WRAPPER_ENTER(fclose); + + // Get the handle for the original function + fclose_f orig_fclose = (fclose_f) gotcha_get_wrappee(io_fns[CURIOUS_FCLOSE_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(fp)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_deregister_file(fileno(fp)); + + curious_metadata_record_t io_args = { + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FCLOSE_ID, + }; + curious_call_callbacks(CURIOUS_METADATA_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_fclose(fp); + io_args.call_count = 1; + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fclose); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_fclose(fp), fclose); + } +} - WRAPPER_RETURN(return_val, vfprintf); +int wrapped_printf(const char* format, ...) +{ + WRAPPER_ENTER(printf); + + // Get the handle for the original function + vprintf_f orig_vprintf = (vprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VPRINTF_ID].handle); + + // Get the variable-length argument list set up + va_list args; + va_start(args, format); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(STDOUT_FILENO); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_write_record_t io_args = { + .bytes_written = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_PRINTF_ID, + }; + curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_vprintf(format, args); + io_args.call_count = 1; + if (return_val > 0) { + io_args.bytes_written = return_val; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, printf); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_vprintf(format, args), printf); + } +} - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_vfprintf(stream, format, ap), vfprintf); - } +int wrapped_fprintf(FILE* stream, const char* format, ...) +{ + WRAPPER_ENTER(fprintf); + + // Get the handle for the original function + vfprintf_f orig_vfprintf = (vfprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VFPRINTF_ID].handle); + + // Get the variable-length argument list set up + va_list args; + va_start(args, format); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(stream)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_write_record_t io_args = { + .bytes_written = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FPRINTF_ID, + }; + curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_vfprintf(stream, format, args); + io_args.call_count = 1; + if (return_val > 0) { + io_args.bytes_written = return_val; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fprintf); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_vfprintf(stream, format, args), fprintf); + } +} + +int wrapped_vprintf(const char* format, va_list ap) +{ + WRAPPER_ENTER(vprintf); + + // Get the handle for the original function + vprintf_f orig_vprintf = (vprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VPRINTF_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(STDOUT_FILENO); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_write_record_t io_args = { + .bytes_written = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_VPRINTF_ID, + }; + curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_vprintf(format, ap); + io_args.call_count = 1; + if (return_val > 0) { + io_args.bytes_written = return_val; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, vprintf); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_vprintf(format, ap), vprintf); + } +} + +int wrapped_vfprintf(FILE* stream, const char* format, va_list ap) +{ + WRAPPER_ENTER(vfprintf); + + // Get the handle for the original function + vfprintf_f orig_vfprintf = (vfprintf_f) gotcha_get_wrappee(io_fns[CURIOUS_VFPRINTF_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(stream)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_write_record_t io_args = { + .bytes_written = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_VFPRINTF_ID, + }; + curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_vfprintf(stream, format, ap); + io_args.call_count = 1; + if (return_val > 0) { + io_args.bytes_written = return_val; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, vfprintf); + + // and just call the original function every other time + } else { + WRAPPER_RETURN(orig_vfprintf(stream, format, ap), vfprintf); + } } /* @@ -1204,142 +1227,145 @@ int wrapped_vfscanf(FILE *stream, const char *format, va_list ap) { } */ -int wrapped_fgetc(FILE *stream) { - WRAPPER_ENTER(fgetc); - - // Get the handle for the original function - fgetc_f orig_fgetc = (fgetc_f) gotcha_get_wrappee(io_fns[CURIOUS_FGETC_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(stream)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +int wrapped_fgetc(FILE* stream) +{ + WRAPPER_ENTER(fgetc); + + // Get the handle for the original function + fgetc_f orig_fgetc = (fgetc_f) gotcha_get_wrappee(io_fns[CURIOUS_FGETC_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(stream)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_read_record_t io_args = { + .bytes_read = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FGETC_ID, + }; + curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_fgetc(stream); + io_args.call_count = 1; + if (EOF != return_val) { + io_args.bytes_read = 1; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fgetc); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } - - curious_read_record_t io_args = { - .bytes_read = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FGETC_ID, - }; - curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_fgetc(stream); - io_args.call_count = 1; - if (EOF != return_val) { - io_args.bytes_read = 1; + WRAPPER_RETURN(orig_fgetc(stream), fgetc); } - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, fgetc); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fgetc(stream), fgetc); - } } -char * wrapped_fgets(char *s, int size, FILE *stream) { - WRAPPER_ENTER(fgets); - - // Get the handle for the original function - fgets_f orig_fgets = (fgets_f) gotcha_get_wrappee(io_fns[CURIOUS_FGETS_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(stream)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +char* wrapped_fgets(char* s, int size, FILE* stream) +{ + WRAPPER_ENTER(fgets); + + // Get the handle for the original function + fgets_f orig_fgets = (fgets_f) gotcha_get_wrappee(io_fns[CURIOUS_FGETS_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(stream)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_read_record_t io_args = { + .bytes_read = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FGETS_ID, + }; + curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); + + // Call the original, saving the result... + char* return_val = orig_fgets(s, size, stream); + io_args.call_count = 1; + if (NULL != return_val) { + io_args.bytes_read = strlen(return_val); + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fgets); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } - - curious_read_record_t io_args = { - .bytes_read = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FGETS_ID, - }; - curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); - - // Call the original, saving the result... - char *return_val = orig_fgets(s, size, stream); - io_args.call_count = 1; - if (NULL != return_val) { - io_args.bytes_read = strlen(return_val); + WRAPPER_RETURN(orig_fgets(s, size, stream), fgets); } - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, fgets); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fgets(s, size, stream), fgets); - } } -int wrapped_getchar(void) { - WRAPPER_ENTER(getchar); - - // Get the handle for the original function - getchar_f orig_getchar = (getchar_f) gotcha_get_wrappee(io_fns[CURIOUS_GETCHAR_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(STDIN_FILENO); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +int wrapped_getchar(void) +{ + WRAPPER_ENTER(getchar); + + // Get the handle for the original function + getchar_f orig_getchar = (getchar_f) gotcha_get_wrappee(io_fns[CURIOUS_GETCHAR_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(STDIN_FILENO); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_read_record_t io_args = { + .bytes_read = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_GETCHAR_ID, + }; + curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); + + // Call the original, saving the result... + int return_val = orig_getchar(); + io_args.call_count = 1; + if (EOF != return_val) { + io_args.bytes_read = 1; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, getchar); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } - - curious_read_record_t io_args = { - .bytes_read = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_GETCHAR_ID, - }; - curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); - - // Call the original, saving the result... - int return_val = orig_getchar(); - io_args.call_count = 1; - if (EOF != return_val) { - io_args.bytes_read = 1; + WRAPPER_RETURN(orig_getchar(), getchar); } - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, getchar); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_getchar(), getchar); - } } /* @@ -1390,94 +1416,96 @@ char * wrapped_gets(char *s) { // or for ungetc, since it just pushes something onto the stream, // not to an actual file (or so it would seem) -size_t wrapped_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) { - WRAPPER_ENTER(fread); - - // Get the handle for the original function - fread_f orig_fread = (fread_f) gotcha_get_wrappee(io_fns[CURIOUS_FREAD_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(stream)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +size_t wrapped_fread(void* ptr, size_t size, size_t nmemb, FILE* stream) +{ + WRAPPER_ENTER(fread); + + // Get the handle for the original function + fread_f orig_fread = (fread_f) gotcha_get_wrappee(io_fns[CURIOUS_FREAD_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(stream)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_read_record_t io_args = { + .bytes_read = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FREAD_ID, + }; + curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); + + // Call the original, saving the result... + size_t return_val = orig_fread(ptr, size, nmemb, stream); + io_args.call_count = 1; + if (0 < return_val) { + io_args.bytes_read = return_val * size; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fread); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } - - curious_read_record_t io_args = { - .bytes_read = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FREAD_ID, - }; - curious_call_callbacks(CURIOUS_READ_CALLBACK, &io_args); - - // Call the original, saving the result... - size_t return_val = orig_fread(ptr, size, nmemb, stream); - io_args.call_count = 1; - if (0 < return_val) { - io_args.bytes_read = return_val * size; + WRAPPER_RETURN(orig_fread(ptr, size, nmemb, stream), fread); } - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, fread); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fread(ptr, size, nmemb, stream), fread); - } } -size_t wrapped_fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream) { - WRAPPER_ENTER(fwrite); - - // Get the handle for the original function - fwrite_f orig_fwrite = (fwrite_f) gotcha_get_wrappee(io_fns[CURIOUS_FWRITE_ID].handle); - - // Only call callbacks the first time around - if (1 == wrapper_call_depth && wrappers_enabled) { - char *filesystem; - char *mount_point; - curious_file_record_t *record = get_curious_file_record(fileno(stream)); - if (NULL == record) { - filesystem = NULL; - mount_point = NULL; +size_t wrapped_fwrite(void* ptr, size_t size, size_t nmemb, FILE* stream) +{ + WRAPPER_ENTER(fwrite); + + // Get the handle for the original function + fwrite_f orig_fwrite = (fwrite_f) gotcha_get_wrappee(io_fns[CURIOUS_FWRITE_ID].handle); + + // Only call callbacks the first time around + if (1 == wrapper_call_depth && wrappers_enabled) { + char* filesystem; + char* mount_point; + curious_file_record_t* record = get_curious_file_record(fileno(stream)); + if (NULL == record) { + filesystem = NULL; + mount_point = NULL; + } else { + filesystem = record->filesystem; + mount_point = record->mount_point; + } + + curious_write_record_t io_args = { + .bytes_written = 0, + .call_count = 0, + .filesystem = filesystem, + .mount_point = mount_point, + .function_id = CURIOUS_FWRITE_ID, + }; + curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); + + // Call the original, saving the result... + size_t return_val = orig_fwrite(ptr, size, nmemb, stream); + io_args.call_count = 1; + if (0 < return_val) { + io_args.bytes_written = return_val * size; + } + + //...for the pot callbacks to use + curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); + + WRAPPER_RETURN(return_val, fwrite); + + // and just call the original function every other time } else { - filesystem = record->filesystem; - mount_point = record->mount_point; - } - - curious_write_record_t io_args = { - .bytes_written = 0, - .call_count = 0, - .filesystem = filesystem, - .mount_point = mount_point, - .function_id = CURIOUS_FWRITE_ID, - }; - curious_call_callbacks(CURIOUS_WRITE_CALLBACK, &io_args); - - // Call the original, saving the result... - size_t return_val = orig_fwrite(ptr, size, nmemb, stream); - io_args.call_count = 1; - if (0 < return_val) { - io_args.bytes_written = return_val * size; + WRAPPER_RETURN(orig_fwrite(ptr, size, nmemb, stream), fwrite); } - - //...for the pot callbacks to use - curious_call_callbacks(CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, &io_args); - - WRAPPER_RETURN(return_val, fwrite); - - // and just call the original function every other time - } else { - WRAPPER_RETURN(orig_fwrite(ptr, size, nmemb, stream), fwrite); - } } diff --git a/src/services/io/CurIOus/src/wrappers.h b/src/services/io/CurIOus/src/wrappers.h index 9f84e772c..b56889380 100644 --- a/src/services/io/CurIOus/src/wrappers.h +++ b/src/services/io/CurIOus/src/wrappers.h @@ -1,33 +1,34 @@ #ifndef IO_WRAPPERS - #define IO_WRAPPERS +#define IO_WRAPPERS - #include - #include - #include - #include - #include +#include +#include +#include +#include +#include - #ifdef __cplusplus - extern "C" { - #endif +#ifdef __cplusplus +extern "C" +{ +#endif - /********* +/********* * Types * *********/ - - // This just keeps all the data about the original function in one place - typedef struct io_function_data { - curious_api_t api; + +// This just keeps all the data about the original function in one place +typedef struct io_function_data { + curious_api_t api; gotcha_wrappee_handle_t handle; - } io_function_data_t; +} io_function_data_t; - void curious_apply_wrappers(curious_apis_t apis); - void curious_disable_wrappers(void); +void curious_apply_wrappers(curious_apis_t apis); +void curious_disable_wrappers(void); - // See io_arhs.h under include/curious for structs to serve as io_args - // to callbacks for corresponding functions - - #ifdef __cplusplus - } // extern "C" - #endif +// See io_arhs.h under include/curious for structs to serve as io_args +// to callbacks for corresponding functions + +#ifdef __cplusplus +} // extern "C" +#endif #endif diff --git a/src/services/io/IOService.cpp b/src/services/io/IOService.cpp index 0a0fc8e1f..8cc8da174 100644 --- a/src/services/io/IOService.cpp +++ b/src/services/io/IOService.cpp @@ -18,193 +18,195 @@ using namespace cali; namespace { - struct user_data_t { +struct user_data_t { curious_t curious_ctx; - Channel channel; - user_data_t(curious_t ctx, Channel& chn) - : curious_ctx(ctx), channel(chn) { } - }; - - std::unordered_map> curious_insts; - - int num_io_callbacks; - int num_failed_io_callbacks; - - Attribute io_region_attr; - Attribute io_filesystem_attr; - Attribute io_mount_point_attr; - Attribute io_bytes_read_attr; - Attribute io_bytes_written_attr; - - // The least significant bit of he callback type is for whether - // it comes before or after an I/O call, so we can discard it - // to index the categories of I/O calls - #define GET_CATEGORY(callback_type) ((callback_type) >> 1) - - // A lookup table for the Variant corresponding to each callback category - Variant categories[] = { - Variant(CALI_TYPE_STRING, "read", 5), - Variant(CALI_TYPE_STRING, "write", 6), - Variant(CALI_TYPE_STRING, "metadata", 9), - }; - - // We need to record different info for different I/O regions/records: - - // For read regions... - void handle_unique_record_data(Caliper &c, Channel &channel, curious_read_record_t *record) { + Channel channel; + + user_data_t(curious_t ctx, Channel& chn) : curious_ctx(ctx), channel(chn) {} +}; + +std::unordered_map> curious_insts; + +int num_io_callbacks; +int num_failed_io_callbacks; + +Attribute io_region_attr; +Attribute io_filesystem_attr; +Attribute io_mount_point_attr; +Attribute io_bytes_read_attr; +Attribute io_bytes_written_attr; + +// The least significant bit of he callback type is for whether +// it comes before or after an I/O call, so we can discard it +// to index the categories of I/O calls +#define GET_CATEGORY(callback_type) ((callback_type) >> 1) + +// A lookup table for the Variant corresponding to each callback category +Variant categories[] = { + Variant(CALI_TYPE_STRING, "read", 5), + Variant(CALI_TYPE_STRING, "write", 6), + Variant(CALI_TYPE_STRING, "metadata", 9), +}; + +// We need to record different info for different I/O regions/records: + +// For read regions... +void handle_unique_record_data(Caliper& c, Channel& channel, curious_read_record_t* record) +{ if (record->bytes_read > 0) { - // ...we care about how much data was read - Entry data(io_bytes_read_attr, cali_make_variant_from_uint(record->bytes_read)); - c.push_snapshot(&channel, SnapshotView(1, &data)); + // ...we care about how much data was read + Entry data(io_bytes_read_attr, cali_make_variant_from_uint(record->bytes_read)); + c.push_snapshot(&channel, SnapshotView(1, &data)); } - } +} - // For write regions... - void handle_unique_record_data(Caliper &c, Channel &channel, curious_write_record_t *record) { +// For write regions... +void handle_unique_record_data(Caliper& c, Channel& channel, curious_write_record_t* record) +{ if (record->bytes_written > 0) { - // ...we care about how much data was written - Entry data(io_bytes_written_attr, cali_make_variant_from_uint(record->bytes_written)); - c.push_snapshot(&channel, SnapshotView(1, &data)); + // ...we care about how much data was written + Entry data(io_bytes_written_attr, cali_make_variant_from_uint(record->bytes_written)); + c.push_snapshot(&channel, SnapshotView(1, &data)); } - } +} - // We don't need anything extra from metadata regions - void handle_unique_record_data(Caliper &c, Channel &channel, curious_metadata_record_t *record) {} +// We don't need anything extra from metadata regions +void handle_unique_record_data(Caliper& c, Channel& channel, curious_metadata_record_t* record) +{} - // Just preforms some sanity checking before making a Variant from a C string - Variant make_variant(char *str) { +// Just preforms some sanity checking before making a Variant from a C string +Variant make_variant(char* str) +{ Variant var; if (nullptr == str) { - var = Variant(CALI_TYPE_STRING, "unknown", 8); + var = Variant(CALI_TYPE_STRING, "unknown", 8); } else { - var = Variant(CALI_TYPE_STRING, str, strlen(str) + 1); + var = Variant(CALI_TYPE_STRING, str, strlen(str) + 1); } return var; - } +} - template - void handle_io(curious_callback_type_t type, void *usr_data, Record *record) { +template +void handle_io(curious_callback_type_t type, void* usr_data, Record* record) +{ ++num_io_callbacks; // get a Caliper instance - Caliper c = Caliper::sigsafe_instance(); - auto data = static_cast(usr_data); + Caliper c = Caliper::sigsafe_instance(); + auto data = static_cast(usr_data); // If we got a Caliper instance successfully... if (c && data->channel.is_active()) { - // ...mark the end... - if (type & CURIOUS_POST_CALLBACK) { - handle_unique_record_data(c, data->channel, record); + // ...mark the end... + if (type & CURIOUS_POST_CALLBACK) { + handle_unique_record_data(c, data->channel, record); - c.end(io_region_attr); - c.end(io_filesystem_attr); - c.end(io_mount_point_attr); + c.end(io_region_attr); + c.end(io_filesystem_attr); + c.end(io_mount_point_attr); - // ...or begining of the I/O region, as appropriate - } else { - Variant filesystem_var = make_variant(record->filesystem); - Variant mount_point_var = make_variant(record->mount_point); + // ...or begining of the I/O region, as appropriate + } else { + Variant filesystem_var = make_variant(record->filesystem); + Variant mount_point_var = make_variant(record->mount_point); - c.begin(io_mount_point_attr, mount_point_var); - c.begin(io_filesystem_attr, filesystem_var); - c.begin(io_region_attr, categories[GET_CATEGORY(type)]); - } + c.begin(io_mount_point_attr, mount_point_var); + c.begin(io_filesystem_attr, filesystem_var); + c.begin(io_region_attr, categories[GET_CATEGORY(type)]); + } - // ...otherwise not our failure (probably already inside Caliper) + // ...otherwise not our failure (probably already inside Caliper) } else { - ++num_failed_io_callbacks; + ++num_failed_io_callbacks; } - } - +} - void init_curious_in_channel(Caliper* c, Channel* channel) { +void init_curious_in_channel(Caliper* c, Channel* channel) +{ channel->events().subscribe_attribute(c, channel, io_region_attr); curious_t curious_inst = curious_init(CURIOUS_ALL_APIS); - auto data = std::unique_ptr(new user_data_t { curious_inst, *channel }); + auto data = std::unique_ptr(new user_data_t { curious_inst, *channel }); curious_register_callback( - curious_inst, - CURIOUS_READ_CALLBACK, - (curious_callback_f) handle_io, - data.get() + curious_inst, + CURIOUS_READ_CALLBACK, + (curious_callback_f) handle_io, + data.get() ); curious_register_callback( - curious_inst, - CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, - (curious_callback_f) handle_io, - data.get() + curious_inst, + CURIOUS_READ_CALLBACK | CURIOUS_POST_CALLBACK, + (curious_callback_f) handle_io, + data.get() ); curious_register_callback( - curious_inst, - CURIOUS_WRITE_CALLBACK, - (curious_callback_f) handle_io, - data.get() + curious_inst, + CURIOUS_WRITE_CALLBACK, + (curious_callback_f) handle_io, + data.get() ); curious_register_callback( - curious_inst, - CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, - (curious_callback_f) handle_io, - data.get() + curious_inst, + CURIOUS_WRITE_CALLBACK | CURIOUS_POST_CALLBACK, + (curious_callback_f) handle_io, + data.get() ); curious_register_callback( - curious_inst, - CURIOUS_METADATA_CALLBACK, - (curious_callback_f) handle_io, - data.get() + curious_inst, + CURIOUS_METADATA_CALLBACK, + (curious_callback_f) handle_io, + data.get() ); curious_register_callback( - curious_inst, - CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, - (curious_callback_f) handle_io, - data.get() + curious_inst, + CURIOUS_METADATA_CALLBACK | CURIOUS_POST_CALLBACK, + (curious_callback_f) handle_io, + data.get() ); curious_insts.insert(std::make_pair(channel->id(), std::move(data))); - } +} - void finalize_curious_in_channel(Caliper* c, Channel* channel) { +void finalize_curious_in_channel(Caliper* c, Channel* channel) +{ auto it = curious_insts.find(channel->id()); if (it != curious_insts.end()) { - curious_finalize(it->second->curious_ctx); - curious_insts.erase(it); + curious_finalize(it->second->curious_ctx); + curious_insts.erase(it); } if (Log::verbosity() >= 2) - Log(2).stream() << channel->name() << ": Processed " - << num_io_callbacks << " I/O callbacks, " - << num_failed_io_callbacks << " failed." << std::endl; - } + Log(2).stream() << channel->name() << ": Processed " << num_io_callbacks << " I/O callbacks, " + << num_failed_io_callbacks << " failed." << std::endl; +} - void init_curious_service(Caliper* c, Channel* channel) { +void init_curious_service(Caliper* c, Channel* channel) +{ Attribute subscribe_attr = c->get_attribute("subscription_event"); - Variant v_true(true); + Variant v_true(true); // create I/O attributes io_region_attr = - c->create_attribute("io.region", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD, - 1, &subscribe_attr, &v_true); + c->create_attribute("io.region", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, 1, &subscribe_attr, &v_true); io_filesystem_attr = - c->create_attribute("io.filesystem", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + c->create_attribute("io.filesystem", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); io_mount_point_attr = - c->create_attribute("io.mount.point", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - - io_bytes_read_attr = - c->create_attribute("io.bytes.read", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); - io_bytes_written_attr = - c->create_attribute("io.bytes.written", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); + c->create_attribute("io.mount.point", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + + io_bytes_read_attr = c->create_attribute( + "io.bytes.read", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); + io_bytes_written_attr = c->create_attribute( + "io.bytes.written", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); // register Caliper post_init_evt and finish_evt callbacks channel->events().post_init_evt.connect(init_curious_in_channel); @@ -213,8 +215,8 @@ namespace channel->events().pre_finish_evt.connect(finalize_curious_in_channel); Log(1).stream() << channel->name() << ": Registered io service" << std::endl; - } -} // namespace [anonymous] +} +} // namespace namespace cali { diff --git a/src/services/kokkos/KokkosLookup.cpp b/src/services/kokkos/KokkosLookup.cpp index 23f6b7a4f..5e34b1c08 100644 --- a/src/services/kokkos/KokkosLookup.cpp +++ b/src/services/kokkos/KokkosLookup.cpp @@ -93,15 +93,15 @@ class KokkosLookup { static const ConfigSet::Entry s_configdata[]; - unsigned m_num_spaces = 0; - unsigned m_num_copies = 0; + unsigned m_num_spaces = 0; + unsigned m_num_copies = 0; - Attribute m_space_attr; - Attribute m_size_attr; - Attribute m_dst_attr; - Attribute m_src_attr; + Attribute m_space_attr; + Attribute m_size_attr; + Attribute m_dst_attr; + Attribute m_src_attr; - Channel m_channel; + Channel m_channel; #if 0 struct KokkosAttributes { @@ -244,19 +244,19 @@ class KokkosLookup #endif // some final log output; print warning if we didn't find an address attribute - void finish_log(Caliper* c, Channel* chn) { + void finish_log(Caliper* c, Channel* chn) + { // Log(1).stream() << chn->name() << ": Kokkoslookup: Performed " // << m_num_lookups << " address lookups, " // << m_num_failed << " failed." // << std::endl; - Log(1).stream() << chn->name() << ": KokkosLookup: Tracked " - << m_num_spaces << " spaces, " - << m_num_copies << " copies." - << std::endl; + Log(1).stream() << chn->name() << ": KokkosLookup: Tracked " << m_num_spaces << " spaces, " << m_num_copies + << " copies." << std::endl; } - void kokkos_allocate(const cali::kokkos::SpaceHandle handle, const char* name, const void* const ptr, size_t size) { + void kokkos_allocate(const cali::kokkos::SpaceHandle handle, const char* name, const void* const ptr, size_t size) + { Caliper c; Variant v_space(handle.name); @@ -264,75 +264,93 @@ class KokkosLookup ++m_num_spaces; } - void kokkos_deallocate(const void* const ptr) { + void kokkos_deallocate(const void* const ptr) + { Caliper c; c.memory_region_end(&m_channel, ptr); } - void kokkos_deepcopy(const void* dst, const void* src, uint64_t size) { + void kokkos_deepcopy(const void* dst, const void* src, uint64_t size) + { Caliper c; - - Entry data[] = { - { m_dst_attr, Variant(CALI_TYPE_ADDR, &dst, sizeof(void*)) }, - { m_src_attr, Variant(CALI_TYPE_ADDR, &src, sizeof(void*)) }, - { m_size_attr, Variant(cali_make_variant_from_uint(size)) } - }; + + Entry data[] = { { m_dst_attr, Variant(CALI_TYPE_ADDR, &dst, sizeof(void*)) }, + { m_src_attr, Variant(CALI_TYPE_ADDR, &src, sizeof(void*)) }, + { m_size_attr, Variant(cali_make_variant_from_uint(size)) } }; c.push_snapshot(&m_channel, SnapshotView(3, data)); ++m_num_copies; } - void make_attributes(Caliper* c) { - m_space_attr = - c->create_attribute("kokkos.space", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + void make_attributes(Caliper* c) + { + m_space_attr = c->create_attribute("kokkos.space", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); Attribute class_mem = c->get_attribute("class.memoryaddress"); Variant v_true(true); - m_size_attr = - c->create_attribute("kokkos.size", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); - - m_src_attr = - c->create_attribute("kokkos.src.addr", CALI_TYPE_ADDR, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - 1, &class_mem, &v_true); - m_dst_attr = - c->create_attribute("kokkos.dst.addr", CALI_TYPE_ADDR, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - 1, &class_mem, &v_true); + m_size_attr = c->create_attribute("kokkos.size", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + + m_src_attr = c->create_attribute( + "kokkos.src.addr", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, + 1, + &class_mem, + &v_true + ); + m_dst_attr = c->create_attribute( + "kokkos.dst.addr", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, + 1, + &class_mem, + &v_true + ); } - KokkosLookup(Caliper* c, Channel* chn) - : m_channel(*chn) - { - ConfigSet config = - chn->config().init("kokkoslookup", s_configdata); + KokkosLookup(Caliper* c, Channel* chn) : m_channel(*chn) + { + ConfigSet config = chn->config().init("kokkoslookup", s_configdata); - make_attributes(c); - } + make_attributes(c); + } public: - static void kokkoslookup_register(Caliper* c, Channel* chn) { + static void kokkoslookup_register(Caliper* c, Channel* chn) + { auto* instance = new KokkosLookup(c, chn); - kokkosp_callbacks.kokkosp_allocate_callback.connect([instance](const cali::kokkos::SpaceHandle handle, const char* name, const void* const ptr, const uint64_t size){ + kokkosp_callbacks.kokkosp_allocate_callback.connect([instance]( + const cali::kokkos::SpaceHandle handle, + const char* name, + const void* const ptr, + const uint64_t size + ) { instance->kokkos_allocate(handle, name, ptr, size); // tracked_pointers.insert(NamedPointer{reinterpret_cast(ptr), std::string(name), size, handle}); }); - kokkosp_callbacks.kokkosp_deallocate_callback.connect([instance](const cali::kokkos::SpaceHandle, const char*, const void* const ptr, const uint64_t){ - instance->kokkos_deallocate(ptr); - // tracked_pointers.insert(NamedPointer{reinterpret_cast(ptr), std::string(name), size, handle}); + kokkosp_callbacks.kokkosp_deallocate_callback.connect( + [instance](const cali::kokkos::SpaceHandle, const char*, const void* const ptr, const uint64_t) { + instance->kokkos_deallocate(ptr); + // tracked_pointers.insert(NamedPointer{reinterpret_cast(ptr), std::string(name), size, handle}); + } + ); + + kokkosp_callbacks.kokkosp_begin_deep_copy_callback.connect([instance]( + const cali::kokkos::SpaceHandle, + const char*, + const void* dst_ptr, + const cali::kokkos::SpaceHandle, + const char*, + const void* src_ptr, + const uint64_t size + ) { + instance->kokkos_deepcopy(dst_ptr, src_ptr, size); }); - - kokkosp_callbacks.kokkosp_begin_deep_copy_callback.connect([instance](const cali::kokkos::SpaceHandle, const char*, const void* dst_ptr, - const cali::kokkos::SpaceHandle, const char*, const void* src_ptr, - const uint64_t size){ - instance->kokkos_deepcopy(dst_ptr, src_ptr, size); - }); #if 0 chn->events().pre_flush_evt.connect( [instance](Caliper* c, Channel* chn, SnapshotView info){ @@ -343,26 +361,27 @@ class KokkosLookup instance->process_snapshot(c, rec); }); #endif - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finish_log(c, chn); - delete instance; - }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finish_log(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered kokkoslookup service" << std::endl; } }; const ConfigSet::Entry KokkosLookup::s_configdata[] = { - { "attributes", CALI_TYPE_STRING, "", - "List of address attributes for which to perform kokkos lookup", - "List of address attributes for which to perform kokkos lookup", + { + "attributes", + CALI_TYPE_STRING, + "", + "List of address attributes for which to perform kokkos lookup", + "List of address attributes for which to perform kokkos lookup", }, ConfigSet::Terminator }; -} // namespace [anonymous] - +} // namespace namespace cali { diff --git a/src/services/kokkos/KokkosProfilingSymbols.cpp b/src/services/kokkos/KokkosProfilingSymbols.cpp index 51b594107..6125c42db 100644 --- a/src/services/kokkos/KokkosProfilingSymbols.cpp +++ b/src/services/kokkos/KokkosProfilingSymbols.cpp @@ -5,103 +5,136 @@ #include cali::kokkos::callbacks kokkosp_callbacks; using cali::kokkos::SpaceHandle; -namespace kokkos { - cali::ConfigManager mgr; -} -extern "C" void kokkosp_print_help(char* progName){ - std::cerr << "Caliper: available configs: \n"; - for(auto conf: kokkos::mgr.available_config_specs() ) { - std::cerr << kokkos::mgr.get_documentation_for_spec(conf.c_str()) << std::endl; - } -} -extern "C" void kokkosp_parse_args(int argc, char *argv_raw[]) { - if (argc > 2) { - std::cerr << "Error: the Kokkos Caliper connector takes only one argument" - << std::endl; - } - if (argc == 2) { - kokkos::mgr.add(argv_raw[1]); - if (kokkos::mgr.error()) { - std::cerr << "Kokkos Caliper connector error: " << kokkos::mgr.error_msg() - << std::endl; + +namespace kokkos +{ +cali::ConfigManager mgr; +} + +extern "C" void kokkosp_print_help(char* progName) +{ + std::cerr << "Caliper: available configs: \n"; + for (auto conf : kokkos::mgr.available_config_specs()) { + std::cerr << kokkos::mgr.get_documentation_for_spec(conf.c_str()) << std::endl; } - kokkos::mgr.start(); - } } -extern "C" void kokkosp_init_library(const int loadSeq, - const uint64_t interfaceVer, - const uint32_t devInfoCount, - void *deviceInfo) { - cali_init(); - kokkosp_callbacks.kokkosp_init_callback(loadSeq, interfaceVer, devInfoCount, - deviceInfo); +extern "C" void kokkosp_parse_args(int argc, char* argv_raw[]) +{ + if (argc > 2) { + std::cerr << "Error: the Kokkos Caliper connector takes only one argument" << std::endl; + } + if (argc == 2) { + kokkos::mgr.add(argv_raw[1]); + if (kokkos::mgr.error()) { + std::cerr << "Kokkos Caliper connector error: " << kokkos::mgr.error_msg() << std::endl; + } + kokkos::mgr.start(); + } } -extern "C" void kokkosp_finalize_library() { - kokkos::mgr.flush(); - kokkosp_callbacks.kokkosp_finalize_callback(); +extern "C" void kokkosp_init_library( + const int loadSeq, + const uint64_t interfaceVer, + const uint32_t devInfoCount, + void* deviceInfo +) +{ + cali_init(); + kokkosp_callbacks.kokkosp_init_callback(loadSeq, interfaceVer, devInfoCount, deviceInfo); } -extern "C" void kokkosp_begin_parallel_for(const char *name, - const uint32_t devID, - uint64_t *kID) { - kokkosp_callbacks.kokkosp_begin_parallel_for_callback(name, devID, kID); +extern "C" void kokkosp_finalize_library() +{ + kokkos::mgr.flush(); + kokkosp_callbacks.kokkosp_finalize_callback(); } -extern "C" void kokkosp_begin_parallel_reduce(const char *name, - const uint32_t devID, - uint64_t *kID) { - kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback(name, devID, kID); + +extern "C" void kokkosp_begin_parallel_for(const char* name, const uint32_t devID, uint64_t* kID) +{ + kokkosp_callbacks.kokkosp_begin_parallel_for_callback(name, devID, kID); } -extern "C" void kokkosp_begin_parallel_scan(const char *name, - const uint32_t devID, - uint64_t *kID) { - kokkosp_callbacks.kokkosp_begin_parallel_scan_callback(name, devID, kID); + +extern "C" void kokkosp_begin_parallel_reduce(const char* name, const uint32_t devID, uint64_t* kID) +{ + kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback(name, devID, kID); } -extern "C" void kokkosp_begin_fence(const char *name, const uint32_t devID, - uint64_t *kID) { - kokkosp_callbacks.kokkosp_begin_fence_callback(name, devID, kID); + +extern "C" void kokkosp_begin_parallel_scan(const char* name, const uint32_t devID, uint64_t* kID) +{ + kokkosp_callbacks.kokkosp_begin_parallel_scan_callback(name, devID, kID); } -extern "C" void kokkosp_end_parallel_for(const uint64_t kID) { - kokkosp_callbacks.kokkosp_end_parallel_for_callback(kID); +extern "C" void kokkosp_begin_fence(const char* name, const uint32_t devID, uint64_t* kID) +{ + kokkosp_callbacks.kokkosp_begin_fence_callback(name, devID, kID); } -extern "C" void kokkosp_end_parallel_reduce(const uint64_t kID) { - kokkosp_callbacks.kokkosp_end_parallel_reduce_callback(kID); + +extern "C" void kokkosp_end_parallel_for(const uint64_t kID) +{ + kokkosp_callbacks.kokkosp_end_parallel_for_callback(kID); } -extern "C" void kokkosp_end_parallel_scan(const uint64_t kID) { - kokkosp_callbacks.kokkosp_end_parallel_scan_callback(kID); + +extern "C" void kokkosp_end_parallel_reduce(const uint64_t kID) +{ + kokkosp_callbacks.kokkosp_end_parallel_reduce_callback(kID); +} + +extern "C" void kokkosp_end_parallel_scan(const uint64_t kID) +{ + kokkosp_callbacks.kokkosp_end_parallel_scan_callback(kID); } -extern "C" void kokkosp_end_fence(const uint64_t kID) { - kokkosp_callbacks.kokkosp_end_fence_callback(kID); + +extern "C" void kokkosp_end_fence(const uint64_t kID) +{ + kokkosp_callbacks.kokkosp_end_fence_callback(kID); } -extern "C" void kokkosp_push_profile_region(char *regionName) { - kokkosp_callbacks.kokkosp_push_region_callback(regionName); +extern "C" void kokkosp_push_profile_region(char* regionName) +{ + kokkosp_callbacks.kokkosp_push_region_callback(regionName); } -extern "C" void kokkosp_pop_profile_region() { - kokkosp_callbacks.kokkosp_pop_region_callback(); + +extern "C" void kokkosp_pop_profile_region() +{ + kokkosp_callbacks.kokkosp_pop_region_callback(); } -extern "C" void kokkosp_allocate_data(const SpaceHandle space, - const char *label, const void *const ptr, - const uint64_t size) { - kokkosp_callbacks.kokkosp_allocate_callback(space, label, ptr, size); + +extern "C" void kokkosp_allocate_data( + const SpaceHandle space, + const char* label, + const void* const ptr, + const uint64_t size +) +{ + kokkosp_callbacks.kokkosp_allocate_callback(space, label, ptr, size); } -extern "C" void kokkosp_deallocate_data(const SpaceHandle space, - const char *label, - const void *const ptr, - const uint64_t size) { - kokkosp_callbacks.kokkosp_deallocate_callback(space, label, ptr, size); + +extern "C" void kokkosp_deallocate_data( + const SpaceHandle space, + const char* label, + const void* const ptr, + const uint64_t size +) +{ + kokkosp_callbacks.kokkosp_deallocate_callback(space, label, ptr, size); } -extern "C" void -kokkosp_begin_deep_copy(const SpaceHandle dst_handle, const char *dst_name, - const void *dst_ptr, const SpaceHandle src_space, - const char *src_name, const void *src_ptr, - const uint64_t size) { - kokkosp_callbacks.kokkosp_begin_deep_copy_callback( - dst_handle, dst_name, dst_ptr, src_space, src_name, src_ptr, size); +extern "C" void kokkosp_begin_deep_copy( + const SpaceHandle dst_handle, + const char* dst_name, + const void* dst_ptr, + const SpaceHandle src_space, + const char* src_name, + const void* src_ptr, + const uint64_t size +) +{ + kokkosp_callbacks + .kokkosp_begin_deep_copy_callback(dst_handle, dst_name, dst_ptr, src_space, src_name, src_ptr, size); } -extern "C" void kokkosp_end_deep_copy() { - kokkosp_callbacks.kokkosp_end_deep_copy_callback(); + +extern "C" void kokkosp_end_deep_copy() +{ + kokkosp_callbacks.kokkosp_end_deep_copy_callback(); } diff --git a/src/services/kokkos/KokkosProfilingSymbols.hpp b/src/services/kokkos/KokkosProfilingSymbols.hpp index b69e5f226..4c0454ed8 100644 --- a/src/services/kokkos/KokkosProfilingSymbols.hpp +++ b/src/services/kokkos/KokkosProfilingSymbols.hpp @@ -1,9 +1,6 @@ #ifndef CALIPER_SERVICES_KOKKOS_PROFILING_SYMBOLS_HPP #define CALIPER_SERVICES_KOKKOS_PROFILING_SYMBOLS_HPP - #include "types.hpp" - #endif - diff --git a/src/services/kokkos/KokkosTime.cpp b/src/services/kokkos/KokkosTime.cpp index ef96b78d9..bb5c1462f 100644 --- a/src/services/kokkos/KokkosTime.cpp +++ b/src/services/kokkos/KokkosTime.cpp @@ -49,78 +49,73 @@ using namespace cali; -namespace { - - class KokkosTime { - cali::Annotation kernel_name_annot; - cali::Annotation kernel_type_annot; - - KokkosTime(Caliper *c, Channel *chn) - : kernel_name_annot("region"), - kernel_type_annot("kernel_type", CALI_ATTR_SKIP_EVENTS) { - } - - public: - - void pushRegion(const char *name, const char *type) { - kernel_type_annot.begin(type); - kernel_name_annot.begin(name); - } - - void popRegion() { - kernel_name_annot.end(); - kernel_type_annot.end(); - } - - static void kokkostime_register(Caliper *c, Channel *chn) { - - auto *instance = new KokkosTime(c, chn); - chn->events().post_init_evt.connect( - [instance](Caliper *c, Channel *chn) { - kokkosp_callbacks.kokkosp_begin_parallel_for_callback.connect([&](const char* name, const uint32_t, uint64_t*){ - instance->pushRegion(name, "kokkos.parallel_for"); - }); - kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback.connect([&](const char* name, const uint32_t, uint64_t*){ - instance->pushRegion(name, "kokkos.parallel_reduce"); - }); - kokkosp_callbacks.kokkosp_begin_parallel_scan_callback.connect([&](const char* name, const uint32_t, uint64_t*){ - instance->pushRegion(name, "kokkos.parallel_scan"); - }); - kokkosp_callbacks.kokkosp_begin_fence_callback.connect([&](const char* name, const uint32_t, uint64_t*){ - instance->pushRegion(name, "kokkos.fence"); - }); - kokkosp_callbacks.kokkosp_end_parallel_for_callback.connect([&](const uint64_t){ - instance->popRegion(); - }); - kokkosp_callbacks.kokkosp_end_parallel_reduce_callback.connect([&](const uint64_t){ - instance->popRegion(); - }); - kokkosp_callbacks.kokkosp_end_parallel_scan_callback.connect([&](const uint64_t){ - instance->popRegion(); - }); - kokkosp_callbacks.kokkosp_end_fence_callback.connect([&](const uint64_t){ - instance->popRegion(); - }); - kokkosp_callbacks.kokkosp_push_region_callback.connect([&](const char* regionName){ - instance->pushRegion(regionName,"kokkos.user_region"); - }); - kokkosp_callbacks.kokkosp_pop_region_callback.connect([&](){ - instance->popRegion(); - }); - }); - chn->events().finish_evt.connect( - [instance](Caliper*, Channel*){ - delete instance; - }); - - Log(1).stream() << chn->name() << ": Registered kokkostime service" << std::endl; - } - }; -} // namespace [anonymous] - - -namespace cali { - - CaliperService kokkostime_service{"kokkostime", ::KokkosTime::kokkostime_register}; +namespace +{ + +class KokkosTime +{ + cali::Annotation kernel_name_annot; + cali::Annotation kernel_type_annot; + + KokkosTime(Caliper* c, Channel* chn) + : kernel_name_annot("region"), kernel_type_annot("kernel_type", CALI_ATTR_SKIP_EVENTS) + {} + +public: + + void pushRegion(const char* name, const char* type) + { + kernel_type_annot.begin(type); + kernel_name_annot.begin(name); + } + + void popRegion() + { + kernel_name_annot.end(); + kernel_type_annot.end(); + } + + static void kokkostime_register(Caliper* c, Channel* chn) + { + + auto* instance = new KokkosTime(c, chn); + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* chn) { + kokkosp_callbacks.kokkosp_begin_parallel_for_callback.connect( + [&](const char* name, const uint32_t, uint64_t*) { instance->pushRegion(name, "kokkos.parallel_for"); } + ); + kokkosp_callbacks.kokkosp_begin_parallel_reduce_callback.connect( + [&](const char* name, const uint32_t, uint64_t*) { + instance->pushRegion(name, "kokkos.parallel_reduce"); + } + ); + kokkosp_callbacks.kokkosp_begin_parallel_scan_callback.connect( + [&](const char* name, const uint32_t, uint64_t*) { instance->pushRegion(name, "kokkos.parallel_scan"); } + ); + kokkosp_callbacks.kokkosp_begin_fence_callback.connect([&](const char* name, const uint32_t, uint64_t*) { + instance->pushRegion(name, "kokkos.fence"); + }); + kokkosp_callbacks.kokkosp_end_parallel_for_callback.connect([&](const uint64_t) { instance->popRegion(); }); + kokkosp_callbacks.kokkosp_end_parallel_reduce_callback.connect([&](const uint64_t) { + instance->popRegion(); + }); + kokkosp_callbacks.kokkosp_end_parallel_scan_callback.connect([&](const uint64_t) { instance->popRegion(); } + ); + kokkosp_callbacks.kokkosp_end_fence_callback.connect([&](const uint64_t) { instance->popRegion(); }); + kokkosp_callbacks.kokkosp_push_region_callback.connect([&](const char* regionName) { + instance->pushRegion(regionName, "kokkos.user_region"); + }); + kokkosp_callbacks.kokkosp_pop_region_callback.connect([&]() { instance->popRegion(); }); + }); + chn->events().finish_evt.connect([instance](Caliper*, Channel*) { delete instance; }); + + Log(1).stream() << chn->name() << ": Registered kokkostime service" << std::endl; + } +}; +} // namespace + +namespace cali +{ + +CaliperService kokkostime_service { "kokkostime", ::KokkosTime::kokkostime_register }; } diff --git a/src/services/kokkos/types.hpp b/src/services/kokkos/types.hpp index 65a040c41..f190220fe 100644 --- a/src/services/kokkos/types.hpp +++ b/src/services/kokkos/types.hpp @@ -4,65 +4,64 @@ #include #include +namespace cali +{ -namespace cali { +namespace kokkos +{ +struct SpaceHandle { + char name[64]; +}; -namespace kokkos { - struct SpaceHandle { - char name[64]; - }; +using init_callback = util::callback; +using finalize_callback = util::callback; - using init_callback = util::callback; - using finalize_callback = util::callback; +using begin_kernel_callback = util::callback; +using end_kernel_callback = util::callback; - using begin_kernel_callback = util::callback; - using end_kernel_callback = util::callback; +using push_region_callback = util::callback; +using pop_region_callback = util::callback; - using push_region_callback = util::callback; - using pop_region_callback = util::callback; +using allocation_callback = util::callback; +using deallocation_callback = util::callback; - using allocation_callback = util::callback; - using deallocation_callback = util::callback; +using begin_deep_copy_callback = util::callback< + void(const SpaceHandle, const char*, const void*, const SpaceHandle, const char*, const void*, const uint64_t)>; +using end_deep_copy_callback = util::callback; - using begin_deep_copy_callback = util::callback; - using end_deep_copy_callback = util::callback; - - using begin_fence_callback = util::callback; - using end_fence_callback = util::callback; - struct callbacks { +using begin_fence_callback = util::callback; +using end_fence_callback = util::callback; - init_callback kokkosp_init_callback; - finalize_callback kokkosp_finalize_callback; +struct callbacks { - begin_kernel_callback kokkosp_begin_parallel_for_callback; - end_kernel_callback kokkosp_end_parallel_for_callback; + init_callback kokkosp_init_callback; + finalize_callback kokkosp_finalize_callback; - begin_kernel_callback kokkosp_begin_parallel_reduce_callback; - end_kernel_callback kokkosp_end_parallel_reduce_callback; + begin_kernel_callback kokkosp_begin_parallel_for_callback; + end_kernel_callback kokkosp_end_parallel_for_callback; - begin_kernel_callback kokkosp_begin_parallel_scan_callback; - end_kernel_callback kokkosp_end_parallel_scan_callback; + begin_kernel_callback kokkosp_begin_parallel_reduce_callback; + end_kernel_callback kokkosp_end_parallel_reduce_callback; - push_region_callback kokkosp_push_region_callback; - pop_region_callback kokkosp_pop_region_callback; + begin_kernel_callback kokkosp_begin_parallel_scan_callback; + end_kernel_callback kokkosp_end_parallel_scan_callback; - allocation_callback kokkosp_allocate_callback; - deallocation_callback kokkosp_deallocate_callback; + push_region_callback kokkosp_push_region_callback; + pop_region_callback kokkosp_pop_region_callback; - begin_deep_copy_callback kokkosp_begin_deep_copy_callback; - end_deep_copy_callback kokkosp_end_deep_copy_callback; + allocation_callback kokkosp_allocate_callback; + deallocation_callback kokkosp_deallocate_callback; - begin_fence_callback kokkosp_begin_fence_callback; - end_fence_callback kokkosp_end_fence_callback; - }; + begin_deep_copy_callback kokkosp_begin_deep_copy_callback; + end_deep_copy_callback kokkosp_end_deep_copy_callback; + + begin_fence_callback kokkosp_begin_fence_callback; + end_fence_callback kokkosp_end_fence_callback; +}; } // end namespace kokkos -} // end namespace caliper +} // namespace cali extern cali::kokkos::callbacks kokkosp_callbacks; diff --git a/src/services/ldms/LdmsForwarder.cpp b/src/services/ldms/LdmsForwarder.cpp index 49bf8f423..ef7ed3b0f 100644 --- a/src/services/ldms/LdmsForwarder.cpp +++ b/src/services/ldms/LdmsForwarder.cpp @@ -29,131 +29,125 @@ #include #include - #define SLURM_NOTIFY_TIMEOUT 5 -ldms_t ldms_g; +ldms_t ldms_g; pthread_mutex_t ln_lock; -int conn_status, to; -ldms_t ldms_cali; -sem_t conn_sem; -sem_t recv_sem; +int conn_status, to; +ldms_t ldms_cali; +sem_t conn_sem; +sem_t recv_sem; using namespace cali; namespace { -static void event_cb(ldms_t x, ldms_xprt_event_t e, void *cb_arg) +static void event_cb(ldms_t x, ldms_xprt_event_t e, void* cb_arg) { - switch (e->type) { - case LDMS_XPRT_EVENT_CONNECTED: - sem_post(&conn_sem); - conn_status = 0; - break; - case LDMS_XPRT_EVENT_REJECTED: - ldms_xprt_put(x); - conn_status = ECONNREFUSED; - break; - case LDMS_XPRT_EVENT_DISCONNECTED: - ldms_xprt_put(x); - conn_status = ENOTCONN; - break; - case LDMS_XPRT_EVENT_ERROR: - conn_status = ECONNREFUSED; - break; - case LDMS_XPRT_EVENT_RECV: - sem_post(&recv_sem); - break; - case LDMS_XPRT_EVENT_SEND_COMPLETE: - break; - default: - printf("Received invalid event type %d\n", e->type); - } + switch (e->type) { + case LDMS_XPRT_EVENT_CONNECTED: + sem_post(&conn_sem); + conn_status = 0; + break; + case LDMS_XPRT_EVENT_REJECTED: + ldms_xprt_put(x); + conn_status = ECONNREFUSED; + break; + case LDMS_XPRT_EVENT_DISCONNECTED: + ldms_xprt_put(x); + conn_status = ENOTCONN; + break; + case LDMS_XPRT_EVENT_ERROR: + conn_status = ECONNREFUSED; + break; + case LDMS_XPRT_EVENT_RECV: + sem_post(&recv_sem); + break; + case LDMS_XPRT_EVENT_SEND_COMPLETE: + break; + default: + printf("Received invalid event type %d\n", e->type); + } } -ldms_t setup_connection(const char *xprt, const char *host, - const char *port, const char *auth) +ldms_t setup_connection(const char* xprt, const char* host, const char* port, const char* auth) { - char hostname[PATH_MAX]; - const char *timeout = "5"; - int rc; - struct timespec ts; - - if (!host) { - if (0 == gethostname(hostname, sizeof(hostname))) - host = hostname; - } - if (!timeout) { - ts.tv_sec = time(NULL) + 5; - ts.tv_nsec = 0; - } else { - int to = atoi(timeout); - if (to <= 0) - to = 5; - ts.tv_sec = time(NULL) + to; - ts.tv_nsec = 0; - } + char hostname[PATH_MAX]; + const char* timeout = "5"; + int rc; + struct timespec ts; + + if (!host) { + if (0 == gethostname(hostname, sizeof(hostname))) + host = hostname; + } + if (!timeout) { + ts.tv_sec = time(NULL) + 5; + ts.tv_nsec = 0; + } else { + int to = atoi(timeout); + if (to <= 0) + to = 5; + ts.tv_sec = time(NULL) + to; + ts.tv_nsec = 0; + } - ldms_g = ldms_xprt_new_with_auth(xprt, NULL, auth, NULL); - if (!ldms_g) { - printf("Error %d creating the '%s' transport\n", - errno, xprt); - return NULL; - } + ldms_g = ldms_xprt_new_with_auth(xprt, NULL, auth, NULL); + if (!ldms_g) { + printf("Error %d creating the '%s' transport\n", errno, xprt); + return NULL; + } - sem_init(&recv_sem, 1, 0); - sem_init(&conn_sem, 1, 0); + sem_init(&recv_sem, 1, 0); + sem_init(&conn_sem, 1, 0); - rc = ldms_xprt_connect_by_name(ldms_g, host, port, event_cb, NULL); - if (rc) { - printf("Error %d connecting to %s:%s\n", - rc, host, port); - return NULL; - } - sem_timedwait(&conn_sem, &ts); - if (conn_status) - return NULL; - return ldms_g; + rc = ldms_xprt_connect_by_name(ldms_g, host, port, event_cb, NULL); + if (rc) { + printf("Error %d connecting to %s:%s\n", rc, host, port); + return NULL; + } + sem_timedwait(&conn_sem, &ts); + if (conn_status) + return NULL; + return ldms_g; } void caliper_ldms_connector_initialize() { - const char* env_ldms_stream = getenv("CALIPER_LDMS_STREAM"); - const char* env_ldms_xprt = getenv("CALIPER_LDMS_XPRT"); - const char* env_ldms_host = getenv("CALIPER_LDMS_HOST"); - const char* env_ldms_port = getenv("CALIPER_LDMS_PORT"); - const char* env_ldms_auth = getenv("CALIPER_LDMS_AUTH"); + const char* env_ldms_stream = getenv("CALIPER_LDMS_STREAM"); + const char* env_ldms_xprt = getenv("CALIPER_LDMS_XPRT"); + const char* env_ldms_host = getenv("CALIPER_LDMS_HOST"); + const char* env_ldms_port = getenv("CALIPER_LDMS_PORT"); + const char* env_ldms_auth = getenv("CALIPER_LDMS_AUTH"); /* Check/set LDMS transport type */ - if (!env_ldms_xprt || !env_ldms_host || !env_ldms_port || !env_ldms_auth){ - Log(1).stream() << "Either the transport, host, port or authentication is not given. Setting to default.\n"; - - if (env_ldms_xprt == NULL) - env_ldms_xprt = "sock"; + if (!env_ldms_xprt || !env_ldms_host || !env_ldms_port || !env_ldms_auth) { + Log(1).stream() << "Either the transport, host, port or authentication is not given. Setting to default.\n"; - if (env_ldms_host == NULL) - env_ldms_host = "localhost"; + if (env_ldms_xprt == NULL) + env_ldms_xprt = "sock"; - if (env_ldms_port == NULL) - env_ldms_port = "412"; + if (env_ldms_host == NULL) + env_ldms_host = "localhost"; - if (env_ldms_auth == NULL) - env_ldms_auth = "munge"; + if (env_ldms_port == NULL) + env_ldms_port = "412"; + if (env_ldms_auth == NULL) + env_ldms_auth = "munge"; } pthread_mutex_lock(&ln_lock); ldms_cali = setup_connection(env_ldms_xprt, env_ldms_host, env_ldms_port, env_ldms_auth); - if (conn_status != 0) { - Log(1).stream() << "Error setting up connection to LDMS streams daemon:" << conn_status << " -- exiting\n"; - pthread_mutex_unlock(&ln_lock); - return; - } - else if (ldms_cali->disconnected){ - Log(1).stream() << "Disconnected from LDMS streams daemon -- exiting\n"; - pthread_mutex_unlock(&ln_lock); - return; - } + if (conn_status != 0) { + Log(1).stream() << "Error setting up connection to LDMS streams daemon:" << conn_status << " -- exiting\n"; + pthread_mutex_unlock(&ln_lock); + return; + } else if (ldms_cali->disconnected) { + Log(1).stream() << "Disconnected from LDMS streams daemon -- exiting\n"; + pthread_mutex_unlock(&ln_lock); + return; + } pthread_mutex_unlock(&ln_lock); return; } @@ -163,98 +157,114 @@ void write_ldms_record(int mpi_rank, RegionProfile& profile) caliper_ldms_connector_initialize(); std::map region_times; - double total_time = 0; + double total_time = 0; - int buffer_size = 4096; - char* buffer = (char*) malloc (sizeof(char) * buffer_size); + int buffer_size = 4096; + char* buffer = (char*) malloc(sizeof(char) * buffer_size); - const char* env_ldms_jobid_str = getenv("SLURM_JOB_ID"); - const char* env_ldms_procid = getenv("SLURM_PROCID"); - const char* env_ldms_slurm_nodelist = getenv("SLURM_JOB_NODELIST"); - const char* env_ldms_caliper_verbose_str = getenv("CALIPER_LDMS_VERBOSE"); + const char* env_ldms_jobid_str = getenv("SLURM_JOB_ID"); + const char* env_ldms_procid = getenv("SLURM_PROCID"); + const char* env_ldms_slurm_nodelist = getenv("SLURM_JOB_NODELIST"); + const char* env_ldms_caliper_verbose_str = getenv("CALIPER_LDMS_VERBOSE"); - int env_ldms_jobid = env_ldms_jobid_str == NULL ? 0 : atoi( env_ldms_jobid_str ); - int env_ldms_caliper_verbose = env_ldms_caliper_verbose_str == NULL ? 0 : atoi( env_ldms_caliper_verbose_str ); + int env_ldms_jobid = env_ldms_jobid_str == NULL ? 0 : atoi(env_ldms_jobid_str); + int env_ldms_caliper_verbose = env_ldms_caliper_verbose_str == NULL ? 0 : atoi(env_ldms_caliper_verbose_str); - std::tie(region_times, std::ignore, total_time) = - profile.inclusive_region_times(); + std::tie(region_times, std::ignore, total_time) = profile.inclusive_region_times(); - double unix_ts = 1e-6 * std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()).count(); + double unix_ts = + 1e-6 + * std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) + .count(); - for (const auto &p : region_times) { + for (const auto& p : region_times) { // ignore regions with < 5% of the epoch's total time if (p.second < 0.05 * total_time) continue; - // std::string path_msg = "" + p.first; + // std::string path_msg = "" + p.first; const char* path = p.first.c_str(); if (mpi_rank >= 0) { - snprintf(buffer, buffer_size, "{ \"timestamp\": %f, \"jobid\" : %d, \"rank\" : %d, \"procid\" : %s, \"nodelist\" : %s, \"caliper-perf-data\", \"duration\": %f, \"path\": \"%s\"} \n", unix_ts, env_ldms_jobid, mpi_rank, env_ldms_procid, env_ldms_slurm_nodelist, p.second, path); - } else { - snprintf(buffer, buffer_size, "{ \"timestamp\": %f, \"jobid\" : %d, \"rank\": %d, \"procid\" : %s, \"nodelist\" : %s, \"caliper-perf-data\", \"duration\": %f, \"path\": \"%s\"} \n", unix_ts, env_ldms_jobid, 0, env_ldms_procid, env_ldms_slurm_nodelist, p.second, path); - } + snprintf( + buffer, + buffer_size, + "{ \"timestamp\": %f, \"jobid\" : %d, \"rank\" : %d, \"procid\" : %s, \"nodelist\" : %s, " + "\"caliper-perf-data\", \"duration\": %f, \"path\": \"%s\"} \n", + unix_ts, + env_ldms_jobid, + mpi_rank, + env_ldms_procid, + env_ldms_slurm_nodelist, + p.second, + path + ); + } else { + snprintf( + buffer, + buffer_size, + "{ \"timestamp\": %f, \"jobid\" : %d, \"rank\": %d, \"procid\" : %s, \"nodelist\" : %s, " + "\"caliper-perf-data\", \"duration\": %f, \"path\": \"%s\"} \n", + unix_ts, + env_ldms_jobid, + 0, + env_ldms_procid, + env_ldms_slurm_nodelist, + p.second, + path + ); + } - if (env_ldms_caliper_verbose > 0) - puts(buffer); + if (env_ldms_caliper_verbose > 0) + puts(buffer); - int rc = ldmsd_stream_publish( ldms_cali, "caliper-perf-data", LDMSD_STREAM_JSON, buffer, strlen(buffer) + 1); + int rc = ldmsd_stream_publish(ldms_cali, "caliper-perf-data", LDMSD_STREAM_JSON, buffer, strlen(buffer) + 1); - if (rc) - Log(0).stream() << "Error " << rc << " publishing data.\n"; - else if (env_ldms_caliper_verbose > 0) - Log(2).stream() << "Caliper Message published successfully\n"; - } + if (rc) + Log(0).stream() << "Error " << rc << " publishing data.\n"; + else if (env_ldms_caliper_verbose > 0) + Log(2).stream() << "Caliper Message published successfully\n"; + } } class LdmsForwarder { RegionProfile profile; - void snapshot(Caliper* c, Channel*) { - Entry e = c->get(c->get_attribute("mpi.rank")); - int rank = e.empty() ? -1 : e.value().to_int(); + void snapshot(Caliper* c, Channel*) + { + Entry e = c->get(c->get_attribute("mpi.rank")); + int rank = e.empty() ? -1 : e.value().to_int(); write_ldms_record(rank, profile); profile.clear(); // reset profile - skip to create a cumulative profile } - void post_init(Caliper* c, Channel* channel) { - profile.start(); - } + void post_init(Caliper* c, Channel* channel) { profile.start(); } - LdmsForwarder() - { } + LdmsForwarder() {} public: static const char* s_spec; - static void create(Caliper* c, Channel* channel) { - ConfigSet cfg = - services::init_config_from_spec(channel->config(), s_spec); + static void create(Caliper* c, Channel* channel) + { + ConfigSet cfg = services::init_config_from_spec(channel->config(), s_spec); LdmsForwarder* instance = new LdmsForwarder(); - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->post_init(c, channel); - }); - channel->events().snapshot.connect( - [instance](Caliper* c, Channel* channel, SnapshotView, SnapshotBuilder&){ - instance->snapshot(c, channel); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - delete instance; - }); + channel->events().post_init_evt.connect([instance](Caliper* c, Channel* channel) { + instance->post_init(c, channel); + }); + channel->events().snapshot.connect([instance](Caliper* c, Channel* channel, SnapshotView, SnapshotBuilder&) { + instance->snapshot(c, channel); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { delete instance; }); Log(1).stream() << channel->name() << "Initialized LDMS forwarder\n"; } - - }; const char* LdmsForwarder::s_spec = R"json( @@ -267,7 +277,7 @@ const char* LdmsForwarder::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/libpfm/Libpfm.cpp b/src/services/libpfm/Libpfm.cpp index c8f4d7b96..344c08a65 100644 --- a/src/services/libpfm/Libpfm.cpp +++ b/src/services/libpfm/Libpfm.cpp @@ -36,21 +36,21 @@ extern "C" { - #include "perf_util.h" +#include "perf_util.h" } #ifndef F_SETOWN_EX - #define F_SETOWN_EX 15 - #define F_GETOWN_EX 16 +#define F_SETOWN_EX 15 +#define F_GETOWN_EX 16 - #define F_OWNER_TID 0 - #define F_OWNER_PID 1 - #define F_OWNER_PGRP 2 +#define F_OWNER_TID 0 +#define F_OWNER_PID 1 +#define F_OWNER_PGRP 2 - struct f_owner_ex { - int type; - pid_t pid; - }; +struct f_owner_ex { + int type; + pid_t pid; +}; #endif using namespace cali; @@ -59,33 +59,32 @@ using namespace std; #define MAX_ATTRIBUTES 12 #define MAX_EVENTS 32 -namespace { +namespace +{ class LibpfmService { - Attribute libpfm_attributes[MAX_ATTRIBUTES] = { Attribute() }; - Attribute libpfm_event_name_attr; - cali_id_t libpfm_event_name_attr_id = {CALI_INV_ID}; - std::vector libpfm_event_counter_attrs; - size_t libpfm_attribute_types[MAX_ATTRIBUTES]; + Attribute libpfm_attributes[MAX_ATTRIBUTES] = { Attribute() }; + Attribute libpfm_event_name_attr; + cali_id_t libpfm_event_name_attr_id = { CALI_INV_ID }; + std::vector libpfm_event_counter_attrs; + size_t libpfm_attribute_types[MAX_ATTRIBUTES]; std::map libpfm_attribute_type_to_attr; - uint64_t perf_event_sample_t::* sample_attribute_pointers[MAX_ATTRIBUTES]; + uint64_t perf_event_sample_t::*sample_attribute_pointers[MAX_ATTRIBUTES]; std::vector event_name_nodes; - std::map sample_attribute_map = { - {"ip", PERF_SAMPLE_IP}, - {"id", PERF_SAMPLE_ID}, - {"stream_id", PERF_SAMPLE_STREAM_ID}, - {"time", PERF_SAMPLE_TIME}, - {"tid", PERF_SAMPLE_TID}, - {"period", PERF_SAMPLE_PERIOD}, - {"cpu", PERF_SAMPLE_CPU}, - {"addr", PERF_SAMPLE_ADDR}, - {"weight", PERF_SAMPLE_WEIGHT}, - {"transaction", PERF_SAMPLE_TRANSACTION}, - {"data_src", PERF_SAMPLE_DATA_SRC} - }; + std::map sample_attribute_map = { { "ip", PERF_SAMPLE_IP }, + { "id", PERF_SAMPLE_ID }, + { "stream_id", PERF_SAMPLE_STREAM_ID }, + { "time", PERF_SAMPLE_TIME }, + { "tid", PERF_SAMPLE_TID }, + { "period", PERF_SAMPLE_PERIOD }, + { "cpu", PERF_SAMPLE_CPU }, + { "addr", PERF_SAMPLE_ADDR }, + { "weight", PERF_SAMPLE_WEIGHT }, + { "transaction", PERF_SAMPLE_TRANSACTION }, + { "data_src", PERF_SAMPLE_DATA_SRC } }; static const ConfigSet::Entry s_configdata[]; @@ -93,59 +92,56 @@ class LibpfmService * Service configuration variables */ - int num_attributes = 0; - bool record_counters; - bool enable_sampling; - std::string events_string; + int num_attributes = 0; + bool record_counters; + bool enable_sampling; + std::string events_string; std::vector event_list; - std::vector sampling_period_list; - std::vector precise_ip_list; - std::vector config1_list; + std::vector sampling_period_list; + std::vector precise_ip_list; + std::vector config1_list; - std::vector sample_attributes_strvec; - uint64_t sample_attributes = 0; + std::vector sample_attributes_strvec; + uint64_t sample_attributes = 0; - uint64_t signals_received = 0; - uint64_t samples_produced = 0; - uint64_t bad_samples = 0; - uint64_t null_events = 0; + uint64_t signals_received = 0; + uint64_t samples_produced = 0; + uint64_t bad_samples = 0; + uint64_t null_events = 0; uint64_t null_cali_instances = 0; - unsigned event_read_fail = 0; - unsigned event_reset_fail = 0; + unsigned event_read_fail = 0; + unsigned event_reset_fail = 0; /* * libpfm sampling variables */ struct ThreadState { - ThreadState() - : tid(0), fds(nullptr), num_events(0) - { } + ThreadState() : tid(0), fds(nullptr), num_events(0) {} ~ThreadState() - { - if (sI) - sI->end_thread_sampling(); - } + { + if (sI) + sI->end_thread_sampling(); + } - pid_t tid; - perf_event_desc_t *fds; - int num_events; + pid_t tid; + perf_event_desc_t* fds; + int num_events; }; static thread_local ThreadState sT; - static Channel sC; + static Channel sC; static LibpfmService* sI; const int signum = SIGIO; const int buffer_pages = 1; - static pid_t gettid(void) { - return (pid_t) syscall(__NR_gettid); - } + static pid_t gettid(void) { return (pid_t) syscall(__NR_gettid); } - inline void sample_handler(int event_index, const perf_event_sample_t& sample) { + inline void sample_handler(int event_index, const perf_event_sample_t& sample) + { Caliper c = Caliper::sigsafe_instance(); if (!c) { @@ -153,21 +149,21 @@ class LibpfmService return; } - Entry data[MAX_ATTRIBUTES+1]; + Entry data[MAX_ATTRIBUTES + 1]; for (int i = 0; i < num_attributes; ++i) - data[i] = Entry(libpfm_attributes[i], - cali_make_variant_from_uint(sample.*sample_attribute_pointers[i])); + data[i] = Entry(libpfm_attributes[i], cali_make_variant_from_uint(sample.*sample_attribute_pointers[i])); data[num_attributes] = Entry(event_name_nodes[event_index]); - c.push_snapshot(&sC, SnapshotView(num_attributes+1, data)); + c.push_snapshot(&sC, SnapshotView(num_attributes + 1, data)); sI->samples_produced++; } - static void sigio_handler(int sig, siginfo_t *info, void *extra) { - perf_event_desc_t *fdx = 0; + static void sigio_handler(int sig, siginfo_t* info, void* extra) + { + perf_event_desc_t* fdx = 0; struct perf_event_header ehdr; int fd = info->si_fd; @@ -179,7 +175,7 @@ class LibpfmService sI->signals_received++; int i = 0; - for (i = 0; i MAX_EVENTS) - Log(0).stream() << "libpfm: WARNING: too many events specified for libpfm service! Maximum is " << MAX_EVENTS << std::endl; + Log(0).stream() << "libpfm: WARNING: too many events specified for libpfm service! Maximum is " + << MAX_EVENTS << std::endl; - for(int i=0; i < check_num_events; i++) { + for (int i = 0; i < check_num_events; i++) { if (enable_sampling) { // Store Caliper nodes for each event name - event_name_nodes.push_back( - c->make_tree_entry(libpfm_event_name_attr, - Variant(CALI_TYPE_STRING, check_fds[i].name, strlen(check_fds[i].name)))); + event_name_nodes.push_back(c->make_tree_entry( + libpfm_event_name_attr, + Variant(CALI_TYPE_STRING, check_fds[i].name, strlen(check_fds[i].name)) + )); } } } - void setup_thread_events(Caliper *c) { + void setup_thread_events(Caliper* c) + { struct f_owner_ex fown_ex; - int ret, fd, flags, i; - size_t pgsz = sysconf(_SC_PAGESIZE); + int ret, fd, flags, i; + size_t pgsz = sysconf(_SC_PAGESIZE); // Set thread state sT.tid = gettid(); // Get perf_event from string - sT.fds = NULL; + sT.fds = NULL; sT.num_events = 0; perf_setup_list_events(events_string.c_str(), &sT.fds, &sT.num_events); - for(i=0; i < sT.num_events; i++) { + for (i = 0; i < sT.num_events; i++) { // Set up perf_event - sT.fds[i].hw.disabled = 1; + sT.fds[i].hw.disabled = 1; sT.fds[i].hw.read_format = record_counters ? PERF_FORMAT_SCALE : 0; if (enable_sampling) { sT.fds[i].hw.wakeup_events = 1; - sT.fds[i].hw.sample_type = sample_attributes; + sT.fds[i].hw.sample_type = sample_attributes; sT.fds[i].hw.sample_period = sampling_period_list[i]; - sT.fds[i].hw.precise_ip = precise_ip_list[i]; - sT.fds[i].hw.config1 = config1_list[i]; + sT.fds[i].hw.precise_ip = precise_ip_list[i]; + sT.fds[i].hw.config1 = config1_list[i]; } sT.fds[i].fd = fd = perf_event_open(&sT.fds[i].hw, sT.tid, -1, -1, 0); @@ -277,8 +277,8 @@ class LibpfmService Log(0).stream() << "libpfm: fcntl SETFL failed" << std::endl; fown_ex.type = F_OWNER_TID; - fown_ex.pid = sT.tid; - ret = fcntl(fd, F_SETOWN_EX, (unsigned long) &fown_ex); + fown_ex.pid = sT.tid; + ret = fcntl(fd, F_SETOWN_EX, (unsigned long) &fown_ex); if (ret) Log(0).stream() << "libpfm: fcntl SETOWN failed" << std::endl; @@ -293,16 +293,17 @@ class LibpfmService } } - int setup_process_signals() { + int setup_process_signals() + { struct sigaction sa; - sigset_t set, oldsig, newsig; + sigset_t set, oldsig, newsig; memset(&sa, 0, sizeof(sa)); sigemptyset(&set); sa.sa_sigaction = sigio_handler; - sa.sa_mask = set; - sa.sa_flags = SA_SIGINFO; + sa.sa_mask = set; + sa.sa_flags = SA_SIGINFO; if (sigaction(signum, &sa, NULL) != 0) Log(0).stream() << "libpfm: sigaction failed" << std::endl; @@ -333,10 +334,11 @@ class LibpfmService return ret; } - int begin_thread_sampling() { + int begin_thread_sampling() + { int ret = 0; - for (int i=0; iconfig().init("libpfm", s_configdata); enable_sampling = config.get("enable_sampling").to_bool(); @@ -409,33 +413,37 @@ class LibpfmService // Register IP attribute for symbol lookup Attribute symbol_class_attr = c->get_attribute("class.symboladdress"); - Variant v_true(true); - - new_attribute = c->create_attribute(attribute_name, - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE - | CALI_ATTR_SCOPE_THREAD - | CALI_ATTR_SKIP_EVENTS, - 1, &symbol_class_attr, &v_true); + Variant v_true(true); + + new_attribute = c->create_attribute( + attribute_name, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS, + 1, + &symbol_class_attr, + &v_true + ); } else if (attribute_bits == PERF_SAMPLE_ADDR) { // Register ADDR attribute for memory address lookup Attribute memory_class_attr = c->get_attribute("class.memoryaddress"); - Variant v_true(true); - - new_attribute = c->create_attribute(attribute_name, - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE - | CALI_ATTR_SCOPE_THREAD - | CALI_ATTR_SKIP_EVENTS, - 1, &memory_class_attr, &v_true); + Variant v_true(true); + + new_attribute = c->create_attribute( + attribute_name, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS, + 1, + &memory_class_attr, + &v_true + ); } else { - new_attribute = c->create_attribute(attribute_name, - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE - | CALI_ATTR_SCOPE_THREAD - | CALI_ATTR_SKIP_EVENTS); + new_attribute = c->create_attribute( + attribute_name, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS + ); } // Add to attribute ids @@ -454,8 +462,7 @@ class LibpfmService precise_ip_strvec = config.get("precise_ip").to_stringlist(); config1_strvec = config.get("config1").to_stringlist(); - if (events_listed != sampling_period_strvec.size() - || events_listed != precise_ip_strvec.size() + if (events_listed != sampling_period_strvec.size() || events_listed != precise_ip_strvec.size() || events_listed != config1_strvec.size()) { Log(0).stream() << "libpfm: invalid arguments specified!" << std::endl; @@ -466,7 +473,7 @@ class LibpfmService } } - for (size_t i=0; icreate_attribute(std::string("libpfm.counter.") + event_list[i], - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE - | CALI_ATTR_SCOPE_THREAD - | CALI_ATTR_SKIP_EVENTS - | CALI_ATTR_AGGREGATABLE); + Attribute event_counter_attr = c->create_attribute( + std::string("libpfm.counter.") + event_list[i], + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); libpfm_event_counter_attrs.push_back(event_counter_attr); } } @@ -496,63 +501,65 @@ class LibpfmService return true; } - void setup_sample_pointers() { + void setup_sample_pointers() + { for (int a = 0; a < num_attributes; a++) { size_t attribute_type = libpfm_attribute_types[a]; switch (attribute_type) { - case (PERF_SAMPLE_IP): - sample_attribute_pointers[a] = &perf_event_sample_t::ip; - break; - case (PERF_SAMPLE_ID): - sample_attribute_pointers[a] = &perf_event_sample_t::id; - break; - case (PERF_SAMPLE_STREAM_ID): - sample_attribute_pointers[a] = &perf_event_sample_t::stream_id; - break; - case (PERF_SAMPLE_TIME): - sample_attribute_pointers[a] = &perf_event_sample_t::time; - break; - case (PERF_SAMPLE_TID): - sample_attribute_pointers[a] = &perf_event_sample_t::tid; - break; - case (PERF_SAMPLE_PERIOD): - sample_attribute_pointers[a] = &perf_event_sample_t::period; - break; - case (PERF_SAMPLE_CPU): - sample_attribute_pointers[a] = &perf_event_sample_t::cpu; - break; - case (PERF_SAMPLE_ADDR): - sample_attribute_pointers[a] = &perf_event_sample_t::addr; - break; - case (PERF_SAMPLE_WEIGHT): - sample_attribute_pointers[a] = &perf_event_sample_t::weight; - break; - case (PERF_SAMPLE_TRANSACTION): - sample_attribute_pointers[a] = &perf_event_sample_t::transaction; - break; - case (PERF_SAMPLE_DATA_SRC): - sample_attribute_pointers[a] = &perf_event_sample_t::data_src; - break; - default: - Log(0).stream() << "libpfm: attribute unrecognized!" << std::endl; - return; + case (PERF_SAMPLE_IP): + sample_attribute_pointers[a] = &perf_event_sample_t::ip; + break; + case (PERF_SAMPLE_ID): + sample_attribute_pointers[a] = &perf_event_sample_t::id; + break; + case (PERF_SAMPLE_STREAM_ID): + sample_attribute_pointers[a] = &perf_event_sample_t::stream_id; + break; + case (PERF_SAMPLE_TIME): + sample_attribute_pointers[a] = &perf_event_sample_t::time; + break; + case (PERF_SAMPLE_TID): + sample_attribute_pointers[a] = &perf_event_sample_t::tid; + break; + case (PERF_SAMPLE_PERIOD): + sample_attribute_pointers[a] = &perf_event_sample_t::period; + break; + case (PERF_SAMPLE_CPU): + sample_attribute_pointers[a] = &perf_event_sample_t::cpu; + break; + case (PERF_SAMPLE_ADDR): + sample_attribute_pointers[a] = &perf_event_sample_t::addr; + break; + case (PERF_SAMPLE_WEIGHT): + sample_attribute_pointers[a] = &perf_event_sample_t::weight; + break; + case (PERF_SAMPLE_TRANSACTION): + sample_attribute_pointers[a] = &perf_event_sample_t::transaction; + break; + case (PERF_SAMPLE_DATA_SRC): + sample_attribute_pointers[a] = &perf_event_sample_t::data_src; + break; + default: + Log(0).stream() << "libpfm: attribute unrecognized!" << std::endl; + return; } } } struct read_format { - uint64_t value; /* The value of the event */ - uint64_t time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */ - uint64_t time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */ + uint64_t value; /* The value of the event */ + uint64_t time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */ + uint64_t time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */ //uint64_t id; /* if PERF_FORMAT_ID */ }; - void snapshot_cb(SnapshotBuilder& snapshot) { - Entry data[MAX_EVENTS]; + void snapshot_cb(SnapshotBuilder& snapshot) + { + Entry data[MAX_EVENTS]; size_t count = 0; - for (int i=0; iname() << ": libpfm: thread sampling stats: " << "\tsignals received: " << sI->signals_received - << "\tsamples produced: " << sI->samples_produced - << "\tbad samples: " << sI->bad_samples + << "\tsamples produced: " << sI->samples_produced << "\tbad samples: " << sI->bad_samples << "\tunknown events: " << sI->null_events << "\tnull Caliper instances: " << sI->null_cali_instances << std::endl; if (record_counters && (event_read_fail > 0 || event_reset_fail > 0)) - Log(1).stream() << chn->name() << ": libpfm: " - << event_read_fail << " counter reads failed, " + Log(1).stream() << chn->name() << ": libpfm: " << event_read_fail << " counter reads failed, " << event_reset_fail << " counter resets failed." << std::endl; } @@ -620,41 +628,52 @@ class LibpfmService struct DataSrcAttrs data_src_attrs; - void postprocess_snapshot_cb(Caliper* c, Channel* chn, std::vector& rec) { + void postprocess_snapshot_cb(Caliper* c, Channel* chn, std::vector& rec) + { // Decode data_src encoding if (sample_attributes & PERF_SAMPLE_DATA_SRC) { cali_id_t sample_src_attr_id = libpfm_attribute_type_to_attr[PERF_SAMPLE_DATA_SRC].id(); - auto it = std::find_if(rec.begin(), rec.end(), [&sample_src_attr_id](const Entry& e){ - return e.attribute() == sample_src_attr_id; - }); + auto it = std::find_if(rec.begin(), rec.end(), [&sample_src_attr_id](const Entry& e) { + return e.attribute() == sample_src_attr_id; + }); if (it != rec.end()) { uint64_t data_src = it->value().to_uint(); std::string mem_lvl = datasource_mem_lvl(data_src); - std::string hit = datasource_mem_hit(data_src); - std::string op = datasource_mem_op(data_src); - std::string snoop = datasource_mem_snoop(data_src); - std::string tlb = datasource_mem_tlb(data_src); + std::string hit = datasource_mem_hit(data_src); + std::string op = datasource_mem_op(data_src); + std::string snoop = datasource_mem_snoop(data_src); + std::string tlb = datasource_mem_tlb(data_src); Node* node = nullptr; - node = c->make_tree_entry(data_src_attrs.mem_lvl_attr, - Variant(CALI_TYPE_STRING, mem_lvl.c_str(), mem_lvl.size()), - node); - node = c->make_tree_entry(data_src_attrs.mem_hit_attr, - Variant(CALI_TYPE_STRING, hit.c_str(), hit.size()), - node); - node = c->make_tree_entry(data_src_attrs.mem_op_attr, - Variant(CALI_TYPE_STRING, op.c_str(), op.size()), - node); - node = c->make_tree_entry(data_src_attrs.mem_snoop_attr, - Variant(CALI_TYPE_STRING, snoop.c_str(), snoop.size()), - node); - node = c->make_tree_entry(data_src_attrs.mem_tlb_attr, - Variant(CALI_TYPE_STRING, tlb.c_str(), tlb.size()), - node); + node = c->make_tree_entry( + data_src_attrs.mem_lvl_attr, + Variant(CALI_TYPE_STRING, mem_lvl.c_str(), mem_lvl.size()), + node + ); + node = c->make_tree_entry( + data_src_attrs.mem_hit_attr, + Variant(CALI_TYPE_STRING, hit.c_str(), hit.size()), + node + ); + node = c->make_tree_entry( + data_src_attrs.mem_op_attr, + Variant(CALI_TYPE_STRING, op.c_str(), op.size()), + node + ); + node = c->make_tree_entry( + data_src_attrs.mem_snoop_attr, + Variant(CALI_TYPE_STRING, snoop.c_str(), snoop.size()), + node + ); + node = c->make_tree_entry( + data_src_attrs.mem_tlb_attr, + Variant(CALI_TYPE_STRING, tlb.c_str(), tlb.size()), + node + ); rec.push_back(Entry(node)); } @@ -662,39 +681,38 @@ class LibpfmService } LibpfmService(Caliper* c, Channel* chn) - { - libpfm_event_name_attr = - c->create_attribute("libpfm.event_sample_name", - CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - - libpfm_event_name_attr_id = libpfm_event_name_attr.id(); - - data_src_attrs.mem_lvl_attr = - c->create_attribute("libpfm.memory_level", - CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - data_src_attrs.mem_hit_attr = - c->create_attribute("libpfm.hit_type", - CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - data_src_attrs.mem_op_attr = - c->create_attribute("libpfm.operation", - CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - data_src_attrs.mem_snoop_attr = - c->create_attribute("libpfm.snoop", - CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - data_src_attrs.mem_tlb_attr = - c->create_attribute("libpfm.tlb", - CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); - } + { + libpfm_event_name_attr = c->create_attribute( + "libpfm.event_sample_name", + CALI_TYPE_STRING, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS + ); + + libpfm_event_name_attr_id = libpfm_event_name_attr.id(); + + data_src_attrs.mem_lvl_attr = c->create_attribute( + "libpfm.memory_level", + CALI_TYPE_STRING, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS + ); + data_src_attrs.mem_hit_attr = + c->create_attribute("libpfm.hit_type", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + data_src_attrs.mem_op_attr = + c->create_attribute("libpfm.operation", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + data_src_attrs.mem_snoop_attr = + c->create_attribute("libpfm.snoop", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + data_src_attrs.mem_tlb_attr = + c->create_attribute("libpfm.tlb", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + } public: // Initialization handler - static void libpfm_service_register(Caliper* c, Channel* chn) { + static void libpfm_service_register(Caliper* c, Channel* chn) + { if (sC) { Log(0).stream() << chn->name() << ": libpfm: Cannot enable libpfm service twice!" - << " It is already enabled in channel " - << sC.name() << std::endl; + << " It is already enabled in channel " << sC.name() << std::endl; return; } @@ -710,32 +728,23 @@ class LibpfmService sI->setup_process_signals(); - chn->events().create_thread_evt.connect( - [](Caliper* c, Channel* chn){ - sI->create_thread_cb(c, chn); - }); - chn->events().post_init_evt.connect( - [](Caliper* c, Channel* chn){ - sI->post_init_cb(c, chn); - }); - chn->events().finish_evt.connect( - [](Caliper* c, Channel* chn){ - sI->finish_cb(c, chn); - delete sI; - sI = nullptr; - sC = Channel(); - }); + chn->events().create_thread_evt.connect([](Caliper* c, Channel* chn) { sI->create_thread_cb(c, chn); }); + chn->events().post_init_evt.connect([](Caliper* c, Channel* chn) { sI->post_init_cb(c, chn); }); + chn->events().finish_evt.connect([](Caliper* c, Channel* chn) { + sI->finish_cb(c, chn); + delete sI; + sI = nullptr; + sC = Channel(); + }); if (sI->enable_sampling) - chn->events().postprocess_snapshot.connect( - [](Caliper* c, Channel* chn, std::vector& rec){ - sI->postprocess_snapshot_cb(c, chn, rec); - }); + chn->events().postprocess_snapshot.connect([](Caliper* c, Channel* chn, std::vector& rec) { + sI->postprocess_snapshot_cb(c, chn, rec); + }); if (sI->record_counters) - chn->events().snapshot.connect( - [](Caliper*, Channel*, SnapshotView, SnapshotBuilder& rec){ - sI->snapshot_cb(rec); - }); + chn->events().snapshot.connect([](Caliper*, Channel*, SnapshotView, SnapshotBuilder& rec) { + sI->snapshot_cb(rec); + }); Log(1).stream() << chn->name() << ": Registered libpfm service" << endl; } @@ -747,40 +756,34 @@ Channel LibpfmService::sC; LibpfmService* LibpfmService::sI = nullptr; const ConfigSet::Entry LibpfmService::s_configdata[] = { - {"events", CALI_TYPE_STRING, "cycles", - "Event list", - "Comma-separated list of events to sample" - }, - {"record_counters", CALI_TYPE_BOOL, "true", - "Record counter values (true|false)", - "Whether to record event counter values at each snapshot (true|false)" - }, - {"enable_sampling", CALI_TYPE_BOOL, "true", - "Enable sampling", - "Whether to trigger and record samples" - }, - {"sample_attributes", CALI_TYPE_STRING, "ip,time,tid,cpu", - "Sample attributes", - "Comma-separated list of attributes to record for each sample" - }, - {"sample_period", CALI_TYPE_UINT, "20000000", - "Event sampling periods", - "Comma-separated list of event periods" - }, - {"precise_ip", CALI_TYPE_STRING, "0", - "Precise IP values for events", - "Comma-separated list of precise IP values for respective events" - }, - {"config1", CALI_TYPE_STRING, "0", - "Extra event configurations", - "Comma-separated list of extra event configuration values for supported events" - }, + { "events", CALI_TYPE_STRING, "cycles", "Event list", "Comma-separated list of events to sample" }, + { "record_counters", + CALI_TYPE_BOOL, + "true", + "Record counter values (true|false)", + "Whether to record event counter values at each snapshot (true|false)" }, + { "enable_sampling", CALI_TYPE_BOOL, "true", "Enable sampling", "Whether to trigger and record samples" }, + { "sample_attributes", + CALI_TYPE_STRING, + "ip,time,tid,cpu", + "Sample attributes", + "Comma-separated list of attributes to record for each sample" }, + { "sample_period", CALI_TYPE_UINT, "20000000", "Event sampling periods", "Comma-separated list of event periods" }, + { "precise_ip", + CALI_TYPE_STRING, + "0", + "Precise IP values for events", + "Comma-separated list of precise IP values for respective events" }, + { "config1", + CALI_TYPE_STRING, + "0", + "Extra event configurations", + "Comma-separated list of extra event configuration values for supported events" }, ConfigSet::Terminator }; } // namespace - namespace cali { diff --git a/src/services/libpfm/perf_postprocessing.cpp b/src/services/libpfm/perf_postprocessing.cpp index 71ab0e781..7a673843e 100644 --- a/src/services/libpfm/perf_postprocessing.cpp +++ b/src/services/libpfm/perf_postprocessing.cpp @@ -3,94 +3,95 @@ #include const std::string s_invalid = "Invalid"; -const std::string s_na = "Not Available"; +const std::string s_na = "Not Available"; -const std::string s_mem_lvl_l1 = "L1"; -const std::string s_mem_lvl_lfb = "LFB"; -const std::string s_mem_lvl_l2 = "L2"; -const std::string s_mem_lvl_l3 = "L3"; -const std::string s_mem_lvl_loc_ram = "Local RAM"; +const std::string s_mem_lvl_l1 = "L1"; +const std::string s_mem_lvl_lfb = "LFB"; +const std::string s_mem_lvl_l2 = "L2"; +const std::string s_mem_lvl_l3 = "L3"; +const std::string s_mem_lvl_loc_ram = "Local RAM"; const std::string s_mem_lvl_rem_ram1 = "Remote RAM 1 Hop"; const std::string s_mem_lvl_rem_ram2 = "Remote RAM 2 Hops"; -const std::string s_mem_lvl_cce1 = "Remote Cache 1 Hops"; -const std::string s_mem_lvl_cce2 = "Remote Cache 2 Hops"; -const std::string s_mem_lvl_io = "I/O Memory"; +const std::string s_mem_lvl_cce1 = "Remote Cache 1 Hops"; +const std::string s_mem_lvl_cce2 = "Remote Cache 2 Hops"; +const std::string s_mem_lvl_io = "I/O Memory"; const std::string s_mem_lvl_uncached = "Uncached Memory"; -const std::string datasource_mem_lvl(uint64_t data_src) { +const std::string datasource_mem_lvl(uint64_t data_src) +{ uint64_t lvl_bits = data_src >> PERF_MEM_LVL_SHIFT; - if(lvl_bits & PERF_MEM_LVL_NA) + if (lvl_bits & PERF_MEM_LVL_NA) return s_na; - else if(lvl_bits & PERF_MEM_LVL_L1) + else if (lvl_bits & PERF_MEM_LVL_L1) return s_mem_lvl_l1; - else if(lvl_bits & PERF_MEM_LVL_LFB) + else if (lvl_bits & PERF_MEM_LVL_LFB) return s_mem_lvl_lfb; - else if(lvl_bits & PERF_MEM_LVL_L2) + else if (lvl_bits & PERF_MEM_LVL_L2) return s_mem_lvl_l2; - else if(lvl_bits & PERF_MEM_LVL_L3) + else if (lvl_bits & PERF_MEM_LVL_L3) return s_mem_lvl_l3; - else if(lvl_bits & PERF_MEM_LVL_LOC_RAM) + else if (lvl_bits & PERF_MEM_LVL_LOC_RAM) return s_mem_lvl_loc_ram; - else if(lvl_bits & PERF_MEM_LVL_REM_RAM1) + else if (lvl_bits & PERF_MEM_LVL_REM_RAM1) return s_mem_lvl_rem_ram1; - else if(lvl_bits & PERF_MEM_LVL_REM_RAM2) + else if (lvl_bits & PERF_MEM_LVL_REM_RAM2) return s_mem_lvl_rem_ram2; - else if(lvl_bits & PERF_MEM_LVL_REM_CCE1) + else if (lvl_bits & PERF_MEM_LVL_REM_CCE1) return s_mem_lvl_cce1; - else if(lvl_bits & PERF_MEM_LVL_REM_CCE2) + else if (lvl_bits & PERF_MEM_LVL_REM_CCE2) return s_mem_lvl_cce2; - else if(lvl_bits & PERF_MEM_LVL_IO) + else if (lvl_bits & PERF_MEM_LVL_IO) return s_mem_lvl_io; - else if(lvl_bits & PERF_MEM_LVL_UNC) + else if (lvl_bits & PERF_MEM_LVL_UNC) return s_mem_lvl_uncached; return s_invalid; } -const std::string s_mem_lvl_hit = "Hit"; +const std::string s_mem_lvl_hit = "Hit"; const std::string s_mem_lvl_miss = "Miss"; const std::string datasource_mem_hit(uint64_t datasource) { uint64_t lvl_bits = datasource >> PERF_MEM_LVL_SHIFT; - if(lvl_bits & PERF_MEM_LVL_NA) + if (lvl_bits & PERF_MEM_LVL_NA) return s_na; - else if(lvl_bits & PERF_MEM_LVL_HIT) + else if (lvl_bits & PERF_MEM_LVL_HIT) return s_mem_lvl_hit; - else if(lvl_bits & PERF_MEM_LVL_MISS) + else if (lvl_bits & PERF_MEM_LVL_MISS) return s_mem_lvl_miss; return s_invalid; } -const std::string s_mem_op_na = "Load"; -const std::string s_mem_op_load = "Load"; -const std::string s_mem_op_store = "Store"; +const std::string s_mem_op_na = "Load"; +const std::string s_mem_op_load = "Load"; +const std::string s_mem_op_store = "Store"; const std::string s_mem_op_pfetch = "Prefetch"; -const std::string s_mem_op_exec = "Exec"; +const std::string s_mem_op_exec = "Exec"; const std::string datasource_mem_op(uint64_t datasource) { uint64_t op_bits = datasource >> PERF_MEM_OP_SHIFT; - if(op_bits & PERF_MEM_OP_NA) + if (op_bits & PERF_MEM_OP_NA) return s_na; - else if(op_bits & PERF_MEM_OP_LOAD) + else if (op_bits & PERF_MEM_OP_LOAD) return s_mem_op_load; - else if(op_bits & PERF_MEM_OP_STORE) + else if (op_bits & PERF_MEM_OP_STORE) return s_mem_op_store; - else if(op_bits & PERF_MEM_OP_PFETCH) + else if (op_bits & PERF_MEM_OP_PFETCH) return s_mem_op_pfetch; - else if(op_bits & PERF_MEM_OP_EXEC) + else if (op_bits & PERF_MEM_OP_EXEC) return s_mem_op_exec; return s_invalid; } const std::string s_mem_snoop_none = "None"; -const std::string s_mem_snoop_hit = "Hit"; +const std::string s_mem_snoop_hit = "Hit"; const std::string s_mem_snoop_miss = "Miss"; const std::string s_mem_snoop_hitm = "Hit Modified"; @@ -98,44 +99,44 @@ const std::string datasource_mem_snoop(uint64_t datasource) { uint64_t snoop_bits = datasource >> PERF_MEM_SNOOP_SHIFT; - if(snoop_bits & PERF_MEM_SNOOP_NA) + if (snoop_bits & PERF_MEM_SNOOP_NA) return s_na; - else if(snoop_bits & PERF_MEM_SNOOP_NONE) + else if (snoop_bits & PERF_MEM_SNOOP_NONE) return s_mem_snoop_none; - else if(snoop_bits & PERF_MEM_SNOOP_HIT) + else if (snoop_bits & PERF_MEM_SNOOP_HIT) return s_mem_snoop_hit; - else if(snoop_bits & PERF_MEM_SNOOP_MISS) + else if (snoop_bits & PERF_MEM_SNOOP_MISS) return s_mem_snoop_miss; - else if(snoop_bits & PERF_MEM_SNOOP_HITM) + else if (snoop_bits & PERF_MEM_SNOOP_HITM) return s_mem_snoop_hitm; return s_invalid; } -const std::string s_mem_tlb_hit = "Hit"; +const std::string s_mem_tlb_hit = "Hit"; const std::string s_mem_tlb_miss = "Miss"; -const std::string s_mem_tlb_l1 = "L1"; -const std::string s_mem_tlb_l2 = "L2"; -const std::string s_mem_tlb_wk = "Hardware Walker"; -const std::string s_mem_tlb_os = "OS Fault Handler"; +const std::string s_mem_tlb_l1 = "L1"; +const std::string s_mem_tlb_l2 = "L2"; +const std::string s_mem_tlb_wk = "Hardware Walker"; +const std::string s_mem_tlb_os = "OS Fault Handler"; const std::string datasource_mem_tlb(uint64_t datasource) { uint64_t tlb_bits = datasource >> PERF_MEM_TLB_SHIFT; - if(tlb_bits & PERF_MEM_TLB_NA) + if (tlb_bits & PERF_MEM_TLB_NA) return s_na; - else if(tlb_bits & PERF_MEM_TLB_HIT) + else if (tlb_bits & PERF_MEM_TLB_HIT) return s_mem_tlb_hit; - else if(tlb_bits & PERF_MEM_TLB_MISS) + else if (tlb_bits & PERF_MEM_TLB_MISS) return s_mem_tlb_miss; - else if(tlb_bits & PERF_MEM_TLB_L1) + else if (tlb_bits & PERF_MEM_TLB_L1) return s_mem_tlb_l1; - else if(tlb_bits & PERF_MEM_TLB_L2) + else if (tlb_bits & PERF_MEM_TLB_L2) return s_mem_tlb_l2; - else if(tlb_bits & PERF_MEM_TLB_WK) + else if (tlb_bits & PERF_MEM_TLB_WK) return s_mem_tlb_wk; - else if(tlb_bits & PERF_MEM_TLB_OS) + else if (tlb_bits & PERF_MEM_TLB_OS) return s_mem_tlb_os; return s_invalid; diff --git a/src/services/libpfm/perf_util.c b/src/services/libpfm/perf_util.c index ce3a5add3..1794a93ec 100644 --- a/src/services/libpfm/perf_util.c +++ b/src/services/libpfm/perf_util.c @@ -39,484 +39,484 @@ * max_fds and num_fds must both point to a zero value on the first call * The return value is success (0) vs. failure (non-zero) */ -int -perf_setup_argv_events(const char **argv, perf_event_desc_t **fds, int *num_fds) +int perf_setup_argv_events(const char** argv, perf_event_desc_t** fds, int* num_fds) { - perf_event_desc_t *fd; - pfm_perf_encode_arg_t arg; - int new_max, ret, num, max_fds; - int group_leader; - - if (!(argv && fds && num_fds)) - return -1; - - fd = *fds; - if (fd) { - max_fds = fd[0].max_fds; - if (max_fds < 2) - return -1; - num = *num_fds; - } else { - max_fds = num = 0; /* bootstrap */ - } - group_leader = num; - - while(*argv) { - if (num == max_fds) { - if (max_fds == 0) - new_max = 2; - else - new_max = max_fds << 1; - - if (new_max < max_fds) { - warn("too many entries"); - goto error; - } - fd = realloc(fd, new_max * sizeof(*fd)); - if (!fd) { - warn("cannot allocate memory"); - goto error; - } - /* reset newly allocated chunk */ - memset(fd + max_fds, 0, (new_max - max_fds) * sizeof(*fd)); - max_fds = new_max; - - /* update max size */ - fd[0].max_fds = max_fds; - } - /* ABI compatibility, set before calling libpfm */ - fd[num].hw.size = sizeof(fd[num].hw); - - memset(&arg, 0, sizeof(arg)); - arg.attr = &fd[num].hw; - arg.fstr = &fd[num].fstr; /* fd[].fstr is NULL */ - - ret = pfm_get_os_event_encoding(*argv, PFM_PLM0|PFM_PLM3, PFM_OS_PERF_EVENT_EXT, &arg); - if (ret != PFM_SUCCESS) { - //warnx("event %s: %s", *argv, pfm_strerror(ret)); - goto error; - } - - fd[num].name = strdup(*argv); - fd[num].group_leader = group_leader; - fd[num].idx = arg.idx; - fd[num].cpu = arg.cpu; - - num++; - argv++; - } - *num_fds = num; - *fds = fd; - return 0; + perf_event_desc_t* fd; + pfm_perf_encode_arg_t arg; + int new_max, ret, num, max_fds; + int group_leader; + + if (!(argv && fds && num_fds)) + return -1; + + fd = *fds; + if (fd) { + max_fds = fd[0].max_fds; + if (max_fds < 2) + return -1; + num = *num_fds; + } else { + max_fds = num = 0; /* bootstrap */ + } + group_leader = num; + + while (*argv) { + if (num == max_fds) { + if (max_fds == 0) + new_max = 2; + else + new_max = max_fds << 1; + + if (new_max < max_fds) { + warn("too many entries"); + goto error; + } + fd = realloc(fd, new_max * sizeof(*fd)); + if (!fd) { + warn("cannot allocate memory"); + goto error; + } + /* reset newly allocated chunk */ + memset(fd + max_fds, 0, (new_max - max_fds) * sizeof(*fd)); + max_fds = new_max; + + /* update max size */ + fd[0].max_fds = max_fds; + } + /* ABI compatibility, set before calling libpfm */ + fd[num].hw.size = sizeof(fd[num].hw); + + memset(&arg, 0, sizeof(arg)); + arg.attr = &fd[num].hw; + arg.fstr = &fd[num].fstr; /* fd[].fstr is NULL */ + + ret = pfm_get_os_event_encoding(*argv, PFM_PLM0 | PFM_PLM3, PFM_OS_PERF_EVENT_EXT, &arg); + if (ret != PFM_SUCCESS) { + //warnx("event %s: %s", *argv, pfm_strerror(ret)); + goto error; + } + + fd[num].name = strdup(*argv); + fd[num].group_leader = group_leader; + fd[num].idx = arg.idx; + fd[num].cpu = arg.cpu; + + num++; + argv++; + } + *num_fds = num; + *fds = fd; + return 0; error: - perf_free_fds(fd, num); - return -1; + perf_free_fds(fd, num); + return -1; } -int -perf_setup_list_events(const char *ev, perf_event_desc_t **fd, int *num_fds) +int perf_setup_list_events(const char* ev, perf_event_desc_t** fd, int* num_fds) { - const char **argv; - char *p, *q, *events; - int i, ret, num = 0; - - if (!(ev && fd && num_fds)) - return -1; - - events = strdup(ev); - if (!events) - return -1; - - q = events; - while((p = strchr(q, ','))) { - num++; - q = p + 1; - } - num++; - num++; /* terminator */ - - argv = malloc(num * sizeof(char *)); - if (!argv) { - free(events); - return -1; - } - - i = 0; q = events; - while((p = strchr(q, ','))) { - *p = '\0'; - argv[i++] = q; - q = p + 1; - } - argv[i++] = q; - argv[i] = NULL; - - ret = perf_setup_argv_events(argv, fd, num_fds); - free(argv); - free(events); /* strdup in perf_setup_argv_events() */ - return ret; + const char** argv; + char * p, *q, *events; + int i, ret, num = 0; + + if (!(ev && fd && num_fds)) + return -1; + + events = strdup(ev); + if (!events) + return -1; + + q = events; + while ((p = strchr(q, ','))) { + num++; + q = p + 1; + } + num++; + num++; /* terminator */ + + argv = malloc(num * sizeof(char*)); + if (!argv) { + free(events); + return -1; + } + + i = 0; + q = events; + while ((p = strchr(q, ','))) { + *p = '\0'; + argv[i++] = q; + q = p + 1; + } + argv[i++] = q; + argv[i] = NULL; + + ret = perf_setup_argv_events(argv, fd, num_fds); + free(argv); + free(events); /* strdup in perf_setup_argv_events() */ + return ret; } -void -perf_free_fds(perf_event_desc_t *fds, int num_fds) +void perf_free_fds(perf_event_desc_t* fds, int num_fds) { - int i; + int i; - for (i = 0 ; i < num_fds; i++) { - free(fds[i].name); - free(fds[i].fstr); - } - free(fds); + for (i = 0; i < num_fds; i++) { + free(fds[i].name); + free(fds[i].fstr); + } + free(fds); } -int -perf_get_group_nevents(perf_event_desc_t *fds, int num, int idx) +int perf_get_group_nevents(perf_event_desc_t* fds, int num, int idx) { - int leader; - int i; + int leader; + int i; - if (idx < 0 || idx >= num) - return 0; + if (idx < 0 || idx >= num) + return 0; - leader = fds[idx].group_leader; + leader = fds[idx].group_leader; - for (i = leader + 1; i < num; i++) { - if (fds[i].group_leader != leader) { - /* This is a new group leader, so the previous + for (i = leader + 1; i < num; i++) { + if (fds[i].group_leader != leader) { + /* This is a new group leader, so the previous * event was the final event of the preceding * group. */ - return i - leader; - } - } - return i - leader; + return i - leader; + } + } + return i - leader; } -int -perf_read_buffer(perf_event_desc_t *hw, void *buf, size_t sz) +int perf_read_buffer(perf_event_desc_t* hw, void* buf, size_t sz) { - struct perf_event_mmap_page *hdr = hw->buf; - size_t pgmsk = hw->pgmsk; - void *data; - unsigned long tail; - size_t avail_sz, m, c; - - /* + struct perf_event_mmap_page* hdr = hw->buf; + size_t pgmsk = hw->pgmsk; + void* data; + unsigned long tail; + size_t avail_sz, m, c; + + /* * data points to beginning of buffer payload */ - data = ((void *)hdr)+sysconf(_SC_PAGESIZE); + data = ((void*) hdr) + sysconf(_SC_PAGESIZE); - /* + /* * position of tail within the buffer payload */ - tail = hdr->data_tail & pgmsk; + tail = hdr->data_tail & pgmsk; - /* + /* * size of what is available * * data_head, data_tail never wrap around */ - avail_sz = hdr->data_head - hdr->data_tail; - if (sz > avail_sz) - return -1; + avail_sz = hdr->data_head - hdr->data_tail; + if (sz > avail_sz) + return -1; - /* + /* * sz <= avail_sz, we can satisfy the request */ - /* + /* * c = size till end of buffer * * buffer payload size is necessarily * a power of two, so we can do: */ - c = pgmsk + 1 - tail; + c = pgmsk + 1 - tail; - /* + /* * min with requested size */ - m = c < sz ? c : sz; + m = c < sz ? c : sz; - /* copy beginning */ - memcpy(buf, data+tail, m); + /* copy beginning */ + memcpy(buf, data + tail, m); - /* + /* * copy wrapped around leftover */ - if (sz > m) - memcpy(buf+m, data, sz - m); + if (sz > m) + memcpy(buf + m, data, sz - m); - //printf("\nhead=%lx tail=%lx new_tail=%lx sz=%zu\n", hdr->data_head, hdr->data_tail, hdr->data_tail+sz, sz); - hdr->data_tail += sz; + //printf("\nhead=%lx tail=%lx new_tail=%lx sz=%zu\n", hdr->data_head, hdr->data_tail, hdr->data_tail+sz, sz); + hdr->data_tail += sz; - return 0; + return 0; } -void -perf_skip_buffer(perf_event_desc_t *hw, size_t sz) +void perf_skip_buffer(perf_event_desc_t* hw, size_t sz) { - struct perf_event_mmap_page *hdr = hw->buf; + struct perf_event_mmap_page* hdr = hw->buf; - if ((hdr->data_tail + sz) > hdr->data_head) - sz = hdr->data_head - hdr->data_tail; + if ((hdr->data_tail + sz) > hdr->data_head) + sz = hdr->data_head - hdr->data_tail; - hdr->data_tail += sz; + hdr->data_tail += sz; } -static size_t -__perf_handle_raw(perf_event_desc_t *hw) +static size_t __perf_handle_raw(perf_event_desc_t* hw) { - size_t sz = 0; - uint32_t raw_sz, i; - char *buf; - int ret; - - ret = perf_read_buffer_32(hw, &raw_sz); - if (ret) { - warnx("cannot read raw size"); - return -1; - } - - sz += sizeof(raw_sz); + size_t sz = 0; + uint32_t raw_sz, i; + char* buf; + int ret; + + ret = perf_read_buffer_32(hw, &raw_sz); + if (ret) { + warnx("cannot read raw size"); + return -1; + } - printf("\n\tRAWSZ:%u\n", raw_sz); + sz += sizeof(raw_sz); - buf = malloc(raw_sz); - if (!buf) { - warn("cannot allocate raw buffer"); - return -1; - } + printf("\n\tRAWSZ:%u\n", raw_sz); + buf = malloc(raw_sz); + if (!buf) { + warn("cannot allocate raw buffer"); + return -1; + } - ret = perf_read_buffer(hw, buf, raw_sz); - if (ret) { - warnx("cannot read raw data"); - free(buf); - return -1; - } + ret = perf_read_buffer(hw, buf, raw_sz); + if (ret) { + warnx("cannot read raw data"); + free(buf); + return -1; + } - if (raw_sz) - putchar('\t'); + if (raw_sz) + putchar('\t'); - for(i=0; i < raw_sz; i++) { - printf("0x%02x ", buf[i] & 0xff ); - if (((i+1) % 16) == 0) - printf("\n\t"); - } - if (raw_sz) - putchar('\n'); + for (i = 0; i < raw_sz; i++) { + printf("0x%02x ", buf[i] & 0xff); + if (((i + 1) % 16) == 0) + printf("\n\t"); + } + if (raw_sz) + putchar('\n'); - free(buf); + free(buf); - return sz + raw_sz; + return sz + raw_sz; } -static int -perf_display_branch_stack(perf_event_desc_t *desc, FILE *fp) +static int perf_display_branch_stack(perf_event_desc_t* desc, FILE* fp) { - struct perf_branch_entry b; - uint64_t nr, n; - int ret; + struct perf_branch_entry b; + uint64_t nr, n; + int ret; - ret = perf_read_buffer(desc, &n, sizeof(n)); - if (ret) - errx(1, "cannot read branch stack nr"); + ret = perf_read_buffer(desc, &n, sizeof(n)); + if (ret) + errx(1, "cannot read branch stack nr"); - fprintf(fp, "\n\tBRANCH_STACK:%"PRIu64"\n", n); - nr = n; - /* + fprintf(fp, "\n\tBRANCH_STACK:%" PRIu64 "\n", n); + nr = n; + /* * from most recent to least recent take branch */ - while (nr--) { - ret = perf_read_buffer(desc, &b, sizeof(b)); - if (ret) - errx(1, "cannot read branch stack entry"); - - fprintf(fp, "\tFROM:0x%016"PRIx64" TO:0x%016"PRIx64" MISPRED:%c\n", - b.from, - b.to, - !(b.mispred || b.predicted) ? '-': - (b.mispred ? 'Y' :'N')); - } - return (int)(n * sizeof(b) + sizeof(n)); + while (nr--) { + ret = perf_read_buffer(desc, &b, sizeof(b)); + if (ret) + errx(1, "cannot read branch stack entry"); + + fprintf( + fp, + "\tFROM:0x%016" PRIx64 " TO:0x%016" PRIx64 " MISPRED:%c\n", + b.from, + b.to, + !(b.mispred || b.predicted) ? '-' : (b.mispred ? 'Y' : 'N') + ); + } + return (int) (n * sizeof(b) + sizeof(n)); } -static int -perf_display_regs_user(perf_event_desc_t *hw, FILE *fp) +static int perf_display_regs_user(perf_event_desc_t* hw, FILE* fp) { - errx(1, "display regs_user not implemented yet\n"); - return 0; + errx(1, "display regs_user not implemented yet\n"); + return 0; } -static int -perf_display_regs_intr(perf_event_desc_t *hw, FILE *fp) +static int perf_display_regs_intr(perf_event_desc_t* hw, FILE* fp) { - errx(1, "display regs_intr not implemented yet\n"); - return 0; + errx(1, "display regs_intr not implemented yet\n"); + return 0; } -static int -perf_display_stack_user(perf_event_desc_t *hw, FILE *fp) +static int perf_display_stack_user(perf_event_desc_t* hw, FILE* fp) { - uint64_t nr; - char buf[512]; - size_t sz; - int ret; - - ret = perf_read_buffer(hw, &nr, sizeof(nr)); - if (ret) - errx(1, "cannot user stack size"); - - fprintf(fp, "USER_STACK: SZ:%"PRIu64"\n", nr); - - /* consume content */ - while (nr) { - sz = nr; - if (sz > sizeof(buf)) - sz = sizeof(buf); - - ret = perf_read_buffer(hw, buf, sz); - if (ret) - errx(1, "cannot user stack content"); - nr -= sz; - } - - return 0; + uint64_t nr; + char buf[512]; + size_t sz; + int ret; + + ret = perf_read_buffer(hw, &nr, sizeof(nr)); + if (ret) + errx(1, "cannot user stack size"); + + fprintf(fp, "USER_STACK: SZ:%" PRIu64 "\n", nr); + + /* consume content */ + while (nr) { + sz = nr; + if (sz > sizeof(buf)) + sz = sizeof(buf); + + ret = perf_read_buffer(hw, buf, sz); + if (ret) + errx(1, "cannot user stack content"); + nr -= sz; + } + + return 0; } -int -perf_display_sample(perf_event_desc_t *fds, int num_fds, int idx, struct perf_event_header *ehdr, FILE *fp) +int perf_display_sample(perf_event_desc_t* fds, int num_fds, int idx, struct perf_event_header* ehdr, FILE* fp) { - perf_event_desc_t *hw; - struct { uint32_t pid, tid; } pid; - struct { uint64_t value, id; } grp; - uint64_t time_enabled, time_running; - size_t sz; - uint64_t type, fmt; - uint64_t val64; - const char *str; - int ret, e; - - if (!fds || !fp || !ehdr || num_fds < 0 || idx < 0 || idx >= num_fds) - return -1; - - sz = ehdr->size - sizeof(*ehdr); - - hw = fds+idx; - - type = hw->hw.sample_type; - fmt = hw->hw.read_format; - - if (type & PERF_SAMPLE_IDENTIFIER) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx("cannot read IP"); - return -1; - } - fprintf(fp, "ID:%"PRIu64" ", val64); - sz -= sizeof(val64); - } - /* + perf_event_desc_t* hw; + + struct { + uint32_t pid, tid; + } pid; + + struct { + uint64_t value, id; + } grp; + + uint64_t time_enabled, time_running; + size_t sz; + uint64_t type, fmt; + uint64_t val64; + const char* str; + int ret, e; + + if (!fds || !fp || !ehdr || num_fds < 0 || idx < 0 || idx >= num_fds) + return -1; + + sz = ehdr->size - sizeof(*ehdr); + + hw = fds + idx; + + type = hw->hw.sample_type; + fmt = hw->hw.read_format; + + if (type & PERF_SAMPLE_IDENTIFIER) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read IP"); + return -1; + } + fprintf(fp, "ID:%" PRIu64 " ", val64); + sz -= sizeof(val64); + } + /* * the sample_type information is laid down * based on the PERF_RECORD_SAMPLE format specified * in the libpfm.h header file. * That order is different from the enum perf_event_sample_format */ - if (type & PERF_SAMPLE_IP) { - const char *xtra = " "; - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx("cannot read IP"); - return -1; - } - - /* + if (type & PERF_SAMPLE_IP) { + const char* xtra = " "; + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read IP"); + return -1; + } + + /* * MISC_EXACT_IP indicates that kernel is returning * th IIP of an instruction which caused the event, i.e., * no skid */ - if (hw->hw.precise_ip && (ehdr->misc & PERF_RECORD_MISC_EXACT_IP)) - xtra = " (exact) "; - - fprintf(fp, "IIP:%#016"PRIx64"%s", val64, xtra); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_TID) { - ret = perf_read_buffer(hw, &pid, sizeof(pid)); - if (ret) { - warnx( "cannot read PID"); - return -1; - } - - fprintf(fp, "PID:%d TID:%d ", pid.pid, pid.tid); - sz -= sizeof(pid); - } - - if (type & PERF_SAMPLE_TIME) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read time"); - return -1; - } - - fprintf(fp, "TIME:%'"PRIu64" ", val64); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_ADDR) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read addr"); - return -1; - } - - fprintf(fp, "ADDR:%#016"PRIx64" ", val64); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_ID) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read id"); - return -1; - } - - fprintf(fp, "ID:%"PRIu64" ", val64); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_STREAM_ID) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read stream_id"); - return -1; - } - fprintf(fp, "STREAM_ID:%"PRIu64" ", val64); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_CPU) { - struct { uint32_t cpu, reserved; } cpu; - ret = perf_read_buffer(hw, &cpu, sizeof(cpu)); - if (ret) { - warnx( "cannot read cpu"); - return -1; - } - fprintf(fp, "CPU:%u ", cpu.cpu); - sz -= sizeof(cpu); - } - - if (type & PERF_SAMPLE_PERIOD) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read period"); - return -1; - } - fprintf(fp, "PERIOD:%'"PRIu64" ", val64); - sz -= sizeof(val64); - } - - /* struct read_format { + if (hw->hw.precise_ip && (ehdr->misc & PERF_RECORD_MISC_EXACT_IP)) + xtra = " (exact) "; + + fprintf(fp, "IIP:%#016" PRIx64 "%s", val64, xtra); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_TID) { + ret = perf_read_buffer(hw, &pid, sizeof(pid)); + if (ret) { + warnx("cannot read PID"); + return -1; + } + + fprintf(fp, "PID:%d TID:%d ", pid.pid, pid.tid); + sz -= sizeof(pid); + } + + if (type & PERF_SAMPLE_TIME) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read time"); + return -1; + } + + fprintf(fp, "TIME:%'" PRIu64 " ", val64); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_ADDR) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read addr"); + return -1; + } + + fprintf(fp, "ADDR:%#016" PRIx64 " ", val64); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_ID) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read id"); + return -1; + } + + fprintf(fp, "ID:%" PRIu64 " ", val64); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_STREAM_ID) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read stream_id"); + return -1; + } + fprintf(fp, "STREAM_ID:%" PRIu64 " ", val64); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_CPU) { + struct { + uint32_t cpu, reserved; + } cpu; + + ret = perf_read_buffer(hw, &cpu, sizeof(cpu)); + if (ret) { + warnx("cannot read cpu"); + return -1; + } + fprintf(fp, "CPU:%u ", cpu.cpu); + sz -= sizeof(cpu); + } + + if (type & PERF_SAMPLE_PERIOD) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read period"); + return -1; + } + fprintf(fp, "PERIOD:%'" PRIu64 " ", val64); + sz -= sizeof(val64); + } + + /* struct read_format { * { u64 value; * { u64 time_enabled; } && PERF_FORMAT_ENABLED * { u64 time_running; } && PERF_FORMAT_RUNNING @@ -532,272 +532,280 @@ perf_display_sample(perf_event_desc_t *fds, int num_fds, int idx, struct perf_ev * } && PERF_FORMAT_GROUP * }; */ - if (type & PERF_SAMPLE_READ) { - uint64_t values[3]; - uint64_t nr; - - if (fmt & PERF_FORMAT_GROUP) { - ret = perf_read_buffer_64(hw, &nr); - if (ret) { - warnx( "cannot read nr"); - return -1; - } - - sz -= sizeof(nr); - - time_enabled = time_running = 1; - - if (fmt & PERF_FORMAT_TOTAL_TIME_ENABLED) { - ret = perf_read_buffer_64(hw, &time_enabled); - if (ret) { - warnx( "cannot read timing info"); - return -1; - } - sz -= sizeof(time_enabled); - } - - if (fmt & PERF_FORMAT_TOTAL_TIME_RUNNING) { - ret = perf_read_buffer_64(hw, &time_running); - if (ret) { - warnx( "cannot read timing info"); - return -1; - } - sz -= sizeof(time_running); - } - - fprintf(fp, "ENA=%'"PRIu64" RUN=%'"PRIu64" NR=%"PRIu64"\n", time_enabled, time_running, nr); - - values[1] = time_enabled; - values[2] = time_running; - while(nr--) { - grp.id = -1; - ret = perf_read_buffer_64(hw, &grp.value); - if (ret) { - warnx( "cannot read group value"); - return -1; - } - sz -= sizeof(grp.value); - - if (fmt & PERF_FORMAT_ID) { - ret = perf_read_buffer_64(hw, &grp.id); - if (ret) { - warnx( "cannot read leader id"); - return -1; - } - sz -= sizeof(grp.id); - } - - e = perf_id2event(fds, num_fds, grp.id); - if (e == -1) - str = "unknown sample event"; - else - str = fds[e].name; - - values[0] = grp.value; - grp.value = perf_scale(values); - - fprintf(fp, "\t%'"PRIu64" %s (%"PRIu64"%s)\n", - grp.value, str, - grp.id, - time_running != time_enabled ? ", scaled":""); - - } - } else { - time_enabled = time_running = 0; - /* + if (type & PERF_SAMPLE_READ) { + uint64_t values[3]; + uint64_t nr; + + if (fmt & PERF_FORMAT_GROUP) { + ret = perf_read_buffer_64(hw, &nr); + if (ret) { + warnx("cannot read nr"); + return -1; + } + + sz -= sizeof(nr); + + time_enabled = time_running = 1; + + if (fmt & PERF_FORMAT_TOTAL_TIME_ENABLED) { + ret = perf_read_buffer_64(hw, &time_enabled); + if (ret) { + warnx("cannot read timing info"); + return -1; + } + sz -= sizeof(time_enabled); + } + + if (fmt & PERF_FORMAT_TOTAL_TIME_RUNNING) { + ret = perf_read_buffer_64(hw, &time_running); + if (ret) { + warnx("cannot read timing info"); + return -1; + } + sz -= sizeof(time_running); + } + + fprintf(fp, "ENA=%'" PRIu64 " RUN=%'" PRIu64 " NR=%" PRIu64 "\n", time_enabled, time_running, nr); + + values[1] = time_enabled; + values[2] = time_running; + while (nr--) { + grp.id = -1; + ret = perf_read_buffer_64(hw, &grp.value); + if (ret) { + warnx("cannot read group value"); + return -1; + } + sz -= sizeof(grp.value); + + if (fmt & PERF_FORMAT_ID) { + ret = perf_read_buffer_64(hw, &grp.id); + if (ret) { + warnx("cannot read leader id"); + return -1; + } + sz -= sizeof(grp.id); + } + + e = perf_id2event(fds, num_fds, grp.id); + if (e == -1) + str = "unknown sample event"; + else + str = fds[e].name; + + values[0] = grp.value; + grp.value = perf_scale(values); + + fprintf( + fp, + "\t%'" PRIu64 " %s (%" PRIu64 "%s)\n", + grp.value, + str, + grp.id, + time_running != time_enabled ? ", scaled" : "" + ); + } + } else { + time_enabled = time_running = 0; + /* * this program does not use FORMAT_GROUP when there is only one event */ - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read value"); - return -1; - } - sz -= sizeof(val64); - - if (fmt & PERF_FORMAT_TOTAL_TIME_ENABLED) { - ret = perf_read_buffer_64(hw, &time_enabled); - if (ret) { - warnx( "cannot read timing info"); - return -1; - } - sz -= sizeof(time_enabled); - } - - if (fmt & PERF_FORMAT_TOTAL_TIME_RUNNING) { - ret = perf_read_buffer_64(hw, &time_running); - if (ret) { - warnx( "cannot read timing info"); - return -1; - } - sz -= sizeof(time_running); - } - if (fmt & PERF_FORMAT_ID) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read leader id"); - return -1; - } - sz -= sizeof(val64); - } - - fprintf(fp, "ENA=%'"PRIu64" RUN=%'"PRIu64"\n", time_enabled, time_running); - - values[0] = val64; - values[1] = time_enabled; - values[2] = time_running; - val64 = perf_scale(values); - - fprintf(fp, "\t%'"PRIu64" %s %s\n", - val64, fds[0].name, - time_running != time_enabled ? ", scaled":""); - } - } - - if (type & PERF_SAMPLE_CALLCHAIN) { - uint64_t nr, ip; - - ret = perf_read_buffer_64(hw, &nr); - if (ret) { - warnx( "cannot read callchain nr"); - return -1; - } - sz -= sizeof(nr); - - while(nr--) { - ret = perf_read_buffer_64(hw, &ip); - if (ret) { - warnx( "cannot read ip"); - return -1; - } - - sz -= sizeof(ip); - - fprintf(fp, "\t0x%"PRIx64"\n", ip); - } - } - - if (type & PERF_SAMPLE_RAW) { - ret = __perf_handle_raw(hw); - if (ret == -1) - return -1; - sz -= ret; - } - - if (type & PERF_SAMPLE_BRANCH_STACK) { - ret = perf_display_branch_stack(hw, fp); - sz -= ret; - } - - if (type & PERF_SAMPLE_REGS_USER) { - ret = perf_display_regs_user(hw, fp); - sz -= ret; - } - - if (type & PERF_SAMPLE_STACK_USER) { - ret = perf_display_stack_user(hw, fp); - sz -= ret; - } - - if (type & PERF_SAMPLE_WEIGHT) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read weight"); - return -1; - } - fprintf(fp, "WEIGHT:%'"PRIu64" ", val64); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_DATA_SRC) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read data src"); - return -1; - } - fprintf(fp, "DATA_SRC:%'"PRIu64" ", val64); - sz -= sizeof(val64); - } - if (type & PERF_SAMPLE_TRANSACTION) { - ret = perf_read_buffer_64(hw, &val64); - if (ret) { - warnx( "cannot read txn"); - return -1; - } - fprintf(fp, "TXN:%'"PRIu64" ", val64); - sz -= sizeof(val64); - } - - if (type & PERF_SAMPLE_REGS_INTR) { - ret = perf_display_regs_intr(hw, fp); - sz -= ret; - } - - /* + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read value"); + return -1; + } + sz -= sizeof(val64); + + if (fmt & PERF_FORMAT_TOTAL_TIME_ENABLED) { + ret = perf_read_buffer_64(hw, &time_enabled); + if (ret) { + warnx("cannot read timing info"); + return -1; + } + sz -= sizeof(time_enabled); + } + + if (fmt & PERF_FORMAT_TOTAL_TIME_RUNNING) { + ret = perf_read_buffer_64(hw, &time_running); + if (ret) { + warnx("cannot read timing info"); + return -1; + } + sz -= sizeof(time_running); + } + if (fmt & PERF_FORMAT_ID) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read leader id"); + return -1; + } + sz -= sizeof(val64); + } + + fprintf(fp, "ENA=%'" PRIu64 " RUN=%'" PRIu64 "\n", time_enabled, time_running); + + values[0] = val64; + values[1] = time_enabled; + values[2] = time_running; + val64 = perf_scale(values); + + fprintf(fp, "\t%'" PRIu64 " %s %s\n", val64, fds[0].name, time_running != time_enabled ? ", scaled" : ""); + } + } + + if (type & PERF_SAMPLE_CALLCHAIN) { + uint64_t nr, ip; + + ret = perf_read_buffer_64(hw, &nr); + if (ret) { + warnx("cannot read callchain nr"); + return -1; + } + sz -= sizeof(nr); + + while (nr--) { + ret = perf_read_buffer_64(hw, &ip); + if (ret) { + warnx("cannot read ip"); + return -1; + } + + sz -= sizeof(ip); + + fprintf(fp, "\t0x%" PRIx64 "\n", ip); + } + } + + if (type & PERF_SAMPLE_RAW) { + ret = __perf_handle_raw(hw); + if (ret == -1) + return -1; + sz -= ret; + } + + if (type & PERF_SAMPLE_BRANCH_STACK) { + ret = perf_display_branch_stack(hw, fp); + sz -= ret; + } + + if (type & PERF_SAMPLE_REGS_USER) { + ret = perf_display_regs_user(hw, fp); + sz -= ret; + } + + if (type & PERF_SAMPLE_STACK_USER) { + ret = perf_display_stack_user(hw, fp); + sz -= ret; + } + + if (type & PERF_SAMPLE_WEIGHT) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read weight"); + return -1; + } + fprintf(fp, "WEIGHT:%'" PRIu64 " ", val64); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_DATA_SRC) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read data src"); + return -1; + } + fprintf(fp, "DATA_SRC:%'" PRIu64 " ", val64); + sz -= sizeof(val64); + } + if (type & PERF_SAMPLE_TRANSACTION) { + ret = perf_read_buffer_64(hw, &val64); + if (ret) { + warnx("cannot read txn"); + return -1; + } + fprintf(fp, "TXN:%'" PRIu64 " ", val64); + sz -= sizeof(val64); + } + + if (type & PERF_SAMPLE_REGS_INTR) { + ret = perf_display_regs_intr(hw, fp); + sz -= ret; + } + + /* * if we have some data left, it is because there is more * than what we know about. In fact, it is more complicated * because we may have the right size but wrong layout. But * that's the best we can do. */ - if (sz) { - warnx("did not correctly parse sample leftover=%zu", sz); - perf_skip_buffer(hw, sz); - } + if (sz) { + warnx("did not correctly parse sample leftover=%zu", sz); + perf_skip_buffer(hw, sz); + } - fputc('\n',fp); - return 0; + fputc('\n', fp); + return 0; } -uint64_t -display_lost(perf_event_desc_t *hw, perf_event_desc_t *fds, int num_fds, FILE *fp) +uint64_t display_lost(perf_event_desc_t* hw, perf_event_desc_t* fds, int num_fds, FILE* fp) { - struct { uint64_t id, lost; } lost; - const char *str; - int e, ret; - - ret = perf_read_buffer(hw, &lost, sizeof(lost)); - if (ret) { - warnx("cannot read lost info"); - return 0; - } - - e = perf_id2event(fds, num_fds, lost.id); - if (e == -1) - str = "unknown lost event"; - else - str = fds[e].name; - - fprintf(fp, "<<>>\n", - lost.lost, - str); - - return lost.lost; -} + struct { + uint64_t id, lost; + } lost; + + const char* str; + int e, ret; + + ret = perf_read_buffer(hw, &lost, sizeof(lost)); + if (ret) { + warnx("cannot read lost info"); + return 0; + } + + e = perf_id2event(fds, num_fds, lost.id); + if (e == -1) + str = "unknown lost event"; + else + str = fds[e].name; + fprintf(fp, "<<>>\n", lost.lost, str); -int -perf_read_sample(perf_event_desc_t *fds, - int num_fds, - int idx, - struct perf_event_header *ehdr, - perf_event_sample_t *s, - FILE *ferr) + return lost.lost; +} + +int perf_read_sample( + perf_event_desc_t* fds, + int num_fds, + int idx, + struct perf_event_header* ehdr, + perf_event_sample_t* s, + FILE* ferr +) { - perf_event_desc_t *hw; - struct { uint32_t pid, tid; } pid; - struct { uint64_t value, id; } grp; - uint64_t time_enabled, time_running; - size_t sz; - uint64_t type, fmt; - uint64_t val64; - int ret, e; + perf_event_desc_t* hw; + + struct { + uint32_t pid, tid; + } pid; + + struct { + uint64_t value, id; + } grp; + + uint64_t time_enabled, time_running; + size_t sz; + uint64_t type, fmt; + uint64_t val64; + int ret, e; const char* str; - if (!fds || !s || !ehdr || num_fds < 0 || idx < 0 || idx >= num_fds) + if (!fds || !s || !ehdr || num_fds < 0 || idx < 0 || idx >= num_fds) return -1; sz = ehdr->size - sizeof(*ehdr); - hw = fds+idx; + hw = fds + idx; type = hw->hw.sample_type; fmt = hw->hw.read_format; @@ -879,7 +887,10 @@ perf_read_sample(perf_event_desc_t *fds, } if (type & PERF_SAMPLE_CPU) { - struct { uint32_t cpu, reserved; } cpu; + struct { + uint32_t cpu, reserved; + } cpu; + ret = perf_read_buffer(hw, &cpu, sizeof(cpu)); if (ret) { fprintf(ferr, "libpfm: cannot read cpu"); @@ -953,9 +964,9 @@ perf_read_sample(perf_event_desc_t *fds, values[1] = time_enabled; values[2] = time_running; - while(nr--) { + while (nr--) { grp.id = -1; - ret = perf_read_buffer_64(hw, &grp.value); + ret = perf_read_buffer_64(hw, &grp.value); if (ret) { fprintf(ferr, "libpfm: cannot read group value"); return -1; @@ -984,7 +995,6 @@ perf_read_sample(perf_event_desc_t *fds, // grp.value, str, // grp.id, // time_running != time_enabled ? ", scaled":""); - } } else { time_enabled = time_running = 0; @@ -1029,7 +1039,7 @@ perf_read_sample(perf_event_desc_t *fds, values[0] = val64; values[1] = time_enabled; values[2] = time_running; - val64 = perf_scale(values); + val64 = perf_scale(values); // fprintf(fp, "\t%'"PRIu64" %s %s\n", // val64, fds[0].name, @@ -1047,7 +1057,7 @@ perf_read_sample(perf_event_desc_t *fds, } sz -= sizeof(nr); - while(nr--) { + while (nr--) { ret = perf_read_buffer_64(hw, &ip); if (ret) { fprintf(ferr, "libpfm: cannot read ip"); @@ -1130,35 +1140,42 @@ perf_read_sample(perf_event_desc_t *fds, return 0; } -void -display_exit(perf_event_desc_t *hw, FILE *fp) +void display_exit(perf_event_desc_t* hw, FILE* fp) { - struct { pid_t pid, ppid, tid, ptid; } grp; - int ret; + struct { + pid_t pid, ppid, tid, ptid; + } grp; + + int ret; - ret = perf_read_buffer(hw, &grp, sizeof(grp)); - if (ret) { - warnx("cannot read exit info"); - return; - } + ret = perf_read_buffer(hw, &grp, sizeof(grp)); + if (ret) { + warnx("cannot read exit info"); + return; + } - fprintf(fp,"[%d] exited\n", grp.pid); + fprintf(fp, "[%d] exited\n", grp.pid); } -void -display_freq(int mode, perf_event_desc_t *hw, FILE *fp) +void display_freq(int mode, perf_event_desc_t* hw, FILE* fp) { - struct { uint64_t time, id, stream_id; } thr; - int ret; - - ret = perf_read_buffer(hw, &thr, sizeof(thr)); - if (ret) { - warnx("cannot read throttling info"); - return; - } - - fprintf(fp, "%s value=%"PRIu64" event ID=%"PRIu64"\n", - mode ? "Throttled" : "Unthrottled", - thr.id, - thr.stream_id); + struct { + uint64_t time, id, stream_id; + } thr; + + int ret; + + ret = perf_read_buffer(hw, &thr, sizeof(thr)); + if (ret) { + warnx("cannot read throttling info"); + return; + } + + fprintf( + fp, + "%s value=%" PRIu64 " event ID=%" PRIu64 "\n", + mode ? "Throttled" : "Unthrottled", + thr.id, + thr.stream_id + ); } diff --git a/src/services/libpfm/perf_util.h b/src/services/libpfm/perf_util.h index 67852f78b..e13e75cdf 100644 --- a/src/services/libpfm/perf_util.h +++ b/src/services/libpfm/perf_util.h @@ -30,70 +30,66 @@ #include typedef struct { - struct perf_event_attr hw; - uint64_t values[3]; - uint64_t prev_values[3]; - char *name; - uint64_t id; /* event id kernel */ - void *buf; - size_t pgmsk; - int group_leader; - int fd; - int max_fds; - int idx; /* opaque libpfm event identifier */ - int cpu; /* cpu to program */ - char *fstr; /* fstr from library, must be freed */ + struct perf_event_attr hw; + uint64_t values[3]; + uint64_t prev_values[3]; + char* name; + uint64_t id; /* event id kernel */ + void* buf; + size_t pgmsk; + int group_leader; + int fd; + int max_fds; + int idx; /* opaque libpfm event identifier */ + int cpu; /* cpu to program */ + char* fstr; /* fstr from library, must be freed */ } perf_event_desc_t; -typedef struct perf_event_sample -{ - //struct perf_event_header header; - uint64_t sample_id; /* if PERF_SAMPLE_IDENTIFIER */ - uint64_t ip; /* if PERF_SAMPLE_IP */ - uint64_t pid, tid; /* if PERF_SAMPLE_TID */ - uint64_t time; /* if PERF_SAMPLE_TIME */ - uint64_t addr; /* if PERF_SAMPLE_ADDR */ - uint64_t id; /* if PERF_SAMPLE_ID */ - uint64_t stream_id; /* if PERF_SAMPLE_STREAM_ID */ - uint64_t cpu, res; /* if PERF_SAMPLE_CPU */ - uint64_t period; /* if PERF_SAMPLE_PERIOD */ - //struct read_format v; /* if PERF_SAMPLE_READ */ - //uint64_t nr; /* if PERF_SAMPLE_CALLCHAIN */ - //uint64_t *ips; /* if PERF_SAMPLE_CALLCHAIN */ - //uint32_t raw_size; /* if PERF_SAMPLE_RAW */ - //char *raw_data; /* if PERF_SAMPLE_RAW */ - //uint64_t bnr; /* if PERF_SAMPLE_BRANCH_STACK */ - //struct perf_branch_entry *lbr; /* if PERF_SAMPLE_BRANCH_STACK */ - //uint64_t abi; /* if PERF_SAMPLE_REGS_USER */ - //uint64_t *regs; /* if PERF_SAMPLE_REGS_USER */ - //uint64_t stack_size; /* if PERF_SAMPLE_STACK_USER */ - //char *stack_data; /* if PERF_SAMPLE_STACK_USER */ - //uint64_t dyn_size; /* if PERF_SAMPLE_STACK_USER */ - uint64_t weight; /* if PERF_SAMPLE_WEIGHT */ - uint64_t data_src; /* if PERF_SAMPLE_DATA_SRC */ - uint64_t transaction; /* if PERF_SAMPLE_TRANSACTION */ +typedef struct perf_event_sample { + //struct perf_event_header header; + uint64_t sample_id; /* if PERF_SAMPLE_IDENTIFIER */ + uint64_t ip; /* if PERF_SAMPLE_IP */ + uint64_t pid, tid; /* if PERF_SAMPLE_TID */ + uint64_t time; /* if PERF_SAMPLE_TIME */ + uint64_t addr; /* if PERF_SAMPLE_ADDR */ + uint64_t id; /* if PERF_SAMPLE_ID */ + uint64_t stream_id; /* if PERF_SAMPLE_STREAM_ID */ + uint64_t cpu, res; /* if PERF_SAMPLE_CPU */ + uint64_t period; /* if PERF_SAMPLE_PERIOD */ + //struct read_format v; /* if PERF_SAMPLE_READ */ + //uint64_t nr; /* if PERF_SAMPLE_CALLCHAIN */ + //uint64_t *ips; /* if PERF_SAMPLE_CALLCHAIN */ + //uint32_t raw_size; /* if PERF_SAMPLE_RAW */ + //char *raw_data; /* if PERF_SAMPLE_RAW */ + //uint64_t bnr; /* if PERF_SAMPLE_BRANCH_STACK */ + //struct perf_branch_entry *lbr; /* if PERF_SAMPLE_BRANCH_STACK */ + //uint64_t abi; /* if PERF_SAMPLE_REGS_USER */ + //uint64_t *regs; /* if PERF_SAMPLE_REGS_USER */ + //uint64_t stack_size; /* if PERF_SAMPLE_STACK_USER */ + //char *stack_data; /* if PERF_SAMPLE_STACK_USER */ + //uint64_t dyn_size; /* if PERF_SAMPLE_STACK_USER */ + uint64_t weight; /* if PERF_SAMPLE_WEIGHT */ + uint64_t data_src; /* if PERF_SAMPLE_DATA_SRC */ + uint64_t transaction; /* if PERF_SAMPLE_TRANSACTION */ } perf_event_sample_t; - /* handy shortcut */ -#define PERF_FORMAT_SCALE (PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING) +#define PERF_FORMAT_SCALE (PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING) -extern int perf_setup_argv_events(const char **argv, perf_event_desc_t **fd, int *num_fds); -extern int perf_setup_list_events(const char *events, perf_event_desc_t **fd, int *num_fds); -extern int perf_read_buffer(perf_event_desc_t *hw, void *buf, size_t sz); -extern void perf_free_fds(perf_event_desc_t *fds, int num_fds); -extern void perf_skip_buffer(perf_event_desc_t *hw, size_t sz); +extern int perf_setup_argv_events(const char** argv, perf_event_desc_t** fd, int* num_fds); +extern int perf_setup_list_events(const char* events, perf_event_desc_t** fd, int* num_fds); +extern int perf_read_buffer(perf_event_desc_t* hw, void* buf, size_t sz); +extern void perf_free_fds(perf_event_desc_t* fds, int num_fds); +extern void perf_skip_buffer(perf_event_desc_t* hw, size_t sz); -static inline int -perf_read_buffer_32(perf_event_desc_t *hw, void *buf) +static inline int perf_read_buffer_32(perf_event_desc_t* hw, void* buf) { - return perf_read_buffer(hw, buf, sizeof(uint32_t)); + return perf_read_buffer(hw, buf, sizeof(uint32_t)); } -static inline int -perf_read_buffer_64(perf_event_desc_t *hw, void *buf) +static inline int perf_read_buffer_64(perf_event_desc_t* hw, void* buf) { - return perf_read_buffer(hw, buf, sizeof(uint64_t)); + return perf_read_buffer(hw, buf, sizeof(uint64_t)); } /* @@ -101,99 +97,94 @@ perf_read_buffer_64(perf_event_desc_t *hw, void *buf) * values[1] = TIME_ENABLED * values[2] = TIME_RUNNING */ -static inline uint64_t -perf_scale(uint64_t *values) +static inline uint64_t perf_scale(uint64_t* values) { - uint64_t res = 0; + uint64_t res = 0; - if (!values[2] && !values[1] && values[0]) - warnx("WARNING: time_running = 0 = time_enabled, raw count not zero\n"); + if (!values[2] && !values[1] && values[0]) + warnx("WARNING: time_running = 0 = time_enabled, raw count not zero\n"); - if (values[2] > values[1]) - warnx("WARNING: time_running > time_enabled\n"); + if (values[2] > values[1]) + warnx("WARNING: time_running > time_enabled\n"); - if (values[2]) - res = (uint64_t)((double)values[0] * values[1]/values[2]); - return res; + if (values[2]) + res = (uint64_t) ((double) values[0] * values[1] / values[2]); + return res; } -static inline uint64_t -perf_scale_delta(uint64_t *values, uint64_t *prev_values) +static inline uint64_t perf_scale_delta(uint64_t* values, uint64_t* prev_values) { - double pval[3], val[3]; - uint64_t res = 0; - - if (!values[2] && !values[1] && values[0]) - warnx("WARNING: time_running = 0 = time_enabled, raw count not zero\n"); - - if (values[2] > values[1]) - warnx("WARNING: time_running > time_enabled\n"); - - if (values[2] - prev_values[2]) { - /* covnert everything to double to avoid overflows! */ - pval[0] = prev_values[0]; - pval[1] = prev_values[1]; - pval[2] = prev_values[2]; - val[0] = values[0]; - val[1] = values[1]; - val[2] = values[2]; - res = (uint64_t)(((val[0] - pval[0]) * (val[1] - pval[1])/ (val[2] - pval[2]))); - } - return res; + double pval[3], val[3]; + uint64_t res = 0; + + if (!values[2] && !values[1] && values[0]) + warnx("WARNING: time_running = 0 = time_enabled, raw count not zero\n"); + + if (values[2] > values[1]) + warnx("WARNING: time_running > time_enabled\n"); + + if (values[2] - prev_values[2]) { + /* covnert everything to double to avoid overflows! */ + pval[0] = prev_values[0]; + pval[1] = prev_values[1]; + pval[2] = prev_values[2]; + val[0] = values[0]; + val[1] = values[1]; + val[2] = values[2]; + res = (uint64_t) (((val[0] - pval[0]) * (val[1] - pval[1]) / (val[2] - pval[2]))); + } + return res; } - /* * TIME_RUNNING/TIME_ENABLED */ -static inline double -perf_scale_ratio(uint64_t *values) +static inline double perf_scale_ratio(uint64_t* values) { - if (!values[1]) - return 0.0; + if (!values[1]) + return 0.0; - return values[2]*1.0/values[1]; + return values[2] * 1.0 / values[1]; } -static inline int -perf_fd2event(perf_event_desc_t *fds, int num_events, int fd) +static inline int perf_fd2event(perf_event_desc_t* fds, int num_events, int fd) { - int i; + int i; - for(i=0; i < num_events; i++) - if (fds[i].fd == fd) - return i; - return -1; + for (i = 0; i < num_events; i++) + if (fds[i].fd == fd) + return i; + return -1; } /* * id = PERF_FORMAT_ID */ -static inline int -perf_id2event(perf_event_desc_t *fds, int num_events, uint64_t id) +static inline int perf_id2event(perf_event_desc_t* fds, int num_events, uint64_t id) { - int j; - for(j=0; j < num_events; j++) - if (fds[j].id == id) - return j; - return -1; + int j; + for (j = 0; j < num_events; j++) + if (fds[j].id == id) + return j; + return -1; } -static inline int -perf_is_group_leader(perf_event_desc_t *fds, int idx) +static inline int perf_is_group_leader(perf_event_desc_t* fds, int idx) { - return fds[idx].group_leader == idx; + return fds[idx].group_leader == idx; } -extern int perf_get_group_nevents(perf_event_desc_t *fds, int num, int leader); -extern int perf_display_sample(perf_event_desc_t *fds, int num_fds, int idx, struct perf_event_header *ehdr, FILE *fp); -extern int perf_read_sample(perf_event_desc_t *fds, - int num_fds, - int idx, - struct perf_event_header *ehdr, - perf_event_sample_t *s, - FILE *ferr); -extern uint64_t display_lost(perf_event_desc_t *hw, perf_event_desc_t *fds, int num_fds, FILE *fp); -extern void display_exit(perf_event_desc_t *hw, FILE *fp); -extern void display_freq(int mode, perf_event_desc_t *hw, FILE *fp); +extern int perf_get_group_nevents(perf_event_desc_t* fds, int num, int leader); +extern int perf_display_sample(perf_event_desc_t* fds, int num_fds, int idx, struct perf_event_header* ehdr, FILE* fp); +extern int perf_read_sample( + perf_event_desc_t* fds, + int num_fds, + int idx, + struct perf_event_header* ehdr, + perf_event_sample_t* s, + FILE* ferr +); +extern uint64_t display_lost(perf_event_desc_t* hw, perf_event_desc_t* fds, int num_fds, FILE* fp); +extern void display_exit(perf_event_desc_t* hw, FILE* fp); +extern void display_freq(int mode, perf_event_desc_t* hw, FILE* fp); #endif diff --git a/src/services/memusage/MemStatService.cpp b/src/services/memusage/MemStatService.cpp index dd61a0979..5917df010 100644 --- a/src/services/memusage/MemStatService.cpp +++ b/src/services/memusage/MemStatService.cpp @@ -23,7 +23,7 @@ namespace inline std::array parse_statm(const char* buf, ssize_t max) { std::array numbers = { 0, 0, 0, 0, 0, 0 }; - int nn = 0; + int nn = 0; for (ssize_t n = 0; n < max && nn < 6; ++n) { char c = buf[n]; @@ -42,12 +42,13 @@ class MemstatService Attribute m_vmrss_attr; Attribute m_vmdata_attr; - int m_fd; + int m_fd; - unsigned m_failed; + unsigned m_failed; - void snapshot_cb(Caliper*, SnapshotBuilder& rec) { - char buf[80]; + void snapshot_cb(Caliper*, SnapshotBuilder& rec) + { + char buf[80]; ssize_t ret = pread(m_fd, buf, sizeof(buf), 0); if (ret < 0) { @@ -58,41 +59,40 @@ class MemstatService auto val = parse_statm(buf, ret); rec.append(m_vmsize_attr, cali_make_variant_from_uint(val[0])); - rec.append(m_vmrss_attr, cali_make_variant_from_uint(val[1])); + rec.append(m_vmrss_attr, cali_make_variant_from_uint(val[1])); rec.append(m_vmdata_attr, cali_make_variant_from_uint(val[5])); } - void finish_cb(Caliper*, Channel* channel) { + void finish_cb(Caliper*, Channel* channel) + { if (m_failed > 0) - Log(0).stream() << channel->name() - << ": memstat: failed to read /proc/self/statm " - << m_failed << " times\n"; + Log(0).stream() << channel->name() << ": memstat: failed to read /proc/self/statm " << m_failed + << " times\n"; } - MemstatService(Caliper* c, int fd) - : m_fd { fd }, - m_failed { 0 } + MemstatService(Caliper* c, int fd) : m_fd { fd }, m_failed { 0 } { - m_vmsize_attr = - c->create_attribute("memstat.vmsize", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); - m_vmrss_attr = - c->create_attribute("memstat.vmrss", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); - m_vmdata_attr = - c->create_attribute("memstat.data", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); + m_vmsize_attr = c->create_attribute( + "memstat.vmsize", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); + m_vmrss_attr = c->create_attribute( + "memstat.vmrss", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); + m_vmdata_attr = c->create_attribute( + "memstat.data", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); } public: - static void memstat_register(Caliper* c, Channel* channel) { + static void memstat_register(Caliper* c, Channel* channel) + { int fd = open("/proc/self/statm", O_RDONLY | O_NONBLOCK); if (fd < 0) { @@ -102,16 +102,14 @@ class MemstatService MemstatService* instance = new MemstatService(c, fd); - channel->events().snapshot.connect( - [instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec){ - instance->snapshot_cb(c, rec); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - close(instance->m_fd); - delete instance; - }); + channel->events().snapshot.connect([instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec) { + instance->snapshot_cb(c, rec); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + close(instance->m_fd); + delete instance; + }); Log(1).stream() << channel->name() << ": registered memstat service\n"; } @@ -124,7 +122,7 @@ const char* memstat_spec = R"json( } )json"; -} +} // namespace namespace cali { diff --git a/src/services/monitor/LoopMonitor.cpp b/src/services/monitor/LoopMonitor.cpp index 29104ba80..6d92aeba2 100644 --- a/src/services/monitor/LoopMonitor.cpp +++ b/src/services/monitor/LoopMonitor.cpp @@ -22,21 +22,21 @@ namespace cali extern Attribute class_iteration_attr; extern Attribute loop_attr; -} +} // namespace cali namespace { class LoopMonitor { - int loop_level; - int target_level; - int start_iteration; - int num_iterations; - int num_snapshots; + int loop_level; + int target_level; + int start_iteration; + int num_iterations; + int num_snapshots; - int iteration_interval; - double time_interval; + int iteration_interval; + double time_interval; Attribute num_iterations_attr; Attribute start_iteration_attr; @@ -45,7 +45,8 @@ class LoopMonitor std::chrono::high_resolution_clock::time_point last_snapshot_time; - bool is_target_loop(const Variant& value) { + bool is_target_loop(const Variant& value) + { if (target_loops.empty()) return true; @@ -56,22 +57,22 @@ class LoopMonitor return false; } - void snapshot(Caliper* c, Channel* channel) { - Entry data[] = { - { num_iterations_attr, Variant(num_iterations) }, - { start_iteration_attr, Variant(start_iteration) } - }; - size_t n = start_iteration >= 0 ? 2 : 1; + void snapshot(Caliper* c, Channel* channel) + { + Entry data[] = { { num_iterations_attr, Variant(num_iterations) }, + { start_iteration_attr, Variant(start_iteration) } }; + size_t n = start_iteration >= 0 ? 2 : 1; c->push_snapshot(channel, SnapshotView(n, data)); start_iteration = -1; - num_iterations = 0; + num_iterations = 0; ++num_snapshots; last_snapshot_time = std::chrono::high_resolution_clock::now(); } - void begin_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& value) { + void begin_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& value) + { if (attr == loop_attr) { if (target_level < 0 && is_target_loop(value)) { target_level = loop_level + 1; @@ -85,7 +86,8 @@ class LoopMonitor } } - void end_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& value) { + void end_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant& value) + { if (attr == loop_attr) { if (loop_level == target_level) { snapshot(c, channel); @@ -108,9 +110,9 @@ class LoopMonitor } } - void finish_cb(Caliper* c, Channel* channel) { - Log(1).stream() << channel->name() - << ": loop_monitor: Triggered " << num_snapshots << " snapshots." + void finish_cb(Caliper* c, Channel* channel) + { + Log(1).stream() << channel->name() << ": loop_monitor: Triggered " << num_snapshots << " snapshots." << std::endl; } @@ -125,15 +127,13 @@ class LoopMonitor { Variant v_true(true); - num_iterations_attr = - c->create_attribute("loop.iterations", CALI_TYPE_INT, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); + num_iterations_attr = c->create_attribute( + "loop.iterations", + CALI_TYPE_INT, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); start_iteration_attr = - c->create_attribute("loop.start_iteration", CALI_TYPE_INT, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + c->create_attribute("loop.start_iteration", CALI_TYPE_INT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE); ConfigSet config = services::init_config_from_spec(channel->config(), s_spec); @@ -146,26 +146,26 @@ class LoopMonitor static const char* s_spec; - static void create(Caliper* c, Channel* channel) { + static void create(Caliper* c, Channel* channel) + { LoopMonitor* instance = new LoopMonitor(c, channel); channel->events().pre_begin_evt.connect( [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& val) { instance->begin_cb(c, channel, attr, val); - }); + } + ); channel->events().pre_end_evt.connect( [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& val) { instance->end_cb(c, channel, attr, val); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - delete instance; - }); - - Log(1).stream() << channel->name() - << ": Registered loop_monitor service" - << std::endl; + } + ); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + delete instance; + }); + + Log(1).stream() << channel->name() << ": Registered loop_monitor service" << std::endl; } }; @@ -191,7 +191,7 @@ const char* LoopMonitor::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/monitor/RegionMonitor.cpp b/src/services/monitor/RegionMonitor.cpp index dbf101080..bc3608cdd 100644 --- a/src/services/monitor/RegionMonitor.cpp +++ b/src/services/monitor/RegionMonitor.cpp @@ -25,16 +25,17 @@ class RegionMonitor double child_time; }; - std::unordered_map m_tracking_regions; + std::unordered_map m_tracking_regions; std::vector m_time_stack; - double m_min_interval; - bool m_measuring; - int m_skip; + double m_min_interval; + bool m_measuring; + int m_skip; unsigned m_num_measured; - void post_begin_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant&) { + void post_begin_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant&) + { if (!attr.is_nested()) return; if (m_measuring) { @@ -55,12 +56,13 @@ class RegionMonitor SnapshotBuilder tmp; c->pull_snapshot(channel, SnapshotView(), tmp); m_measuring = true; - m_skip = 1; + m_skip = 1; } } } - void pre_end_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant&) { + void pre_end_cb(Caliper* c, Channel* channel, const Attribute& attr, const Variant&) + { if (!attr.is_nested()) return; @@ -109,47 +111,42 @@ class RegionMonitor } } - void finish_cb(Caliper*, Channel* channel) { - Log(1).stream() << channel->name() - << ": " << m_tracking_regions.size() - << " regions marked, " - << m_num_measured << " instances measured." - << std::endl; + void finish_cb(Caliper*, Channel* channel) + { + Log(1).stream() << channel->name() << ": " << m_tracking_regions.size() << " regions marked, " << m_num_measured + << " instances measured." << std::endl; } - RegionMonitor(Caliper*, Channel* channel) - : m_measuring(false), - m_skip(0), - m_num_measured(0) - { - ConfigSet config = services::init_config_from_spec(channel->config(), s_spec); - m_min_interval = config.get("time_interval").to_double(); - } + RegionMonitor(Caliper*, Channel* channel) : m_measuring(false), m_skip(0), m_num_measured(0) + { + ConfigSet config = services::init_config_from_spec(channel->config(), s_spec); + m_min_interval = config.get("time_interval").to_double(); + } public: static const char* s_spec; - static void create(Caliper* c, Channel* channel) { + static void create(Caliper* c, Channel* channel) + { RegionMonitor* instance = new RegionMonitor(c, channel); channel->events().post_begin_evt.connect( [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& val) { instance->post_begin_cb(c, channel, attr, val); - }); + } + ); channel->events().pre_end_evt.connect( [instance](Caliper* c, Channel* channel, const Attribute& attr, const Variant& val) { instance->pre_end_cb(c, channel, attr, val); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - delete instance; - }); - - Log(1).stream() << channel->name() - << ": Registered region_monitor service" - << std::endl; + } + ); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + delete instance; + }); + + Log(1).stream() << channel->name() << ": Registered region_monitor service" << std::endl; } }; @@ -166,7 +163,8 @@ const char* RegionMonitor::s_spec = R"json( } )json"; -} +} // namespace + namespace cali { diff --git a/src/services/monitor/ThreadMonitor.cpp b/src/services/monitor/ThreadMonitor.cpp index f41cf1c9f..4342b5d20 100644 --- a/src/services/monitor/ThreadMonitor.cpp +++ b/src/services/monitor/ThreadMonitor.cpp @@ -24,28 +24,32 @@ class ThreadMonitor pthread_t monitor_thread; bool thread_running; - Channel channel; + Channel channel; - time_t sleep_sec; - long sleep_nsec; + time_t sleep_sec; + long sleep_nsec; Attribute monitor_attr; - int num_events; + int num_events; - void snapshot() { + void snapshot() + { Caliper c; Entry data(monitor_attr, cali_make_variant_from_int(num_events++)); c.push_snapshot(&channel, SnapshotView(1, &data)); } // the main monitor loop - void monitor() { - struct timespec req { sleep_sec, sleep_nsec }; + void monitor() + { + struct timespec req { + sleep_sec, sleep_nsec + }; while (true) { struct timespec rem = req; - int ret = 0; + int ret = 0; do { ret = nanosleep(&rem, &rem); @@ -63,16 +67,17 @@ class ThreadMonitor } } - static void* thread_fn(void* p) { + static void* thread_fn(void* p) + { ThreadMonitor* instance = static_cast(p); instance->monitor(); return nullptr; } - bool start() { + bool start() + { if (pthread_create(&monitor_thread, nullptr, thread_fn, this) != 0) { - Log(0).stream() << channel.name() << ": monitor(): pthread_create() failed" - << std::endl; + Log(0).stream() << channel.name() << ": monitor(): pthread_create() failed" << std::endl; return false; } @@ -81,7 +86,8 @@ class ThreadMonitor return true; } - void cancel() { + void cancel() + { Log(2).stream() << channel.name() << ": monitor: cancelling monitoring thread" << std::endl; if (pthread_cancel(monitor_thread) != 0) @@ -92,64 +98,56 @@ class ThreadMonitor Log(1).stream() << channel.name() << ": monitor: monitoring thread finished" << std::endl; } - void post_init_cb() { + void post_init_cb() + { if (!start()) return; } - void finish_cb() { + void finish_cb() + { if (thread_running) cancel(); - Log(1).stream() << channel.name() - << ": monitor: triggered " << num_events << " monitoring events" - << std::endl; + Log(1).stream() << channel.name() << ": monitor: triggered " << num_events << " monitoring events" << std::endl; } ThreadMonitor(Caliper* c, Channel* chn) : thread_running(false), channel(*chn), sleep_sec(2), sleep_nsec(0), num_events(0) - { - monitor_attr = - c->create_attribute("monitor.event", CALI_TYPE_INT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS); - - sleep_sec = - channel.config().init("monitor", s_configdata).get("interval").to_uint(); - } + { + monitor_attr = c->create_attribute( + "monitor.event", + CALI_TYPE_INT, + CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ); + + sleep_sec = channel.config().init("monitor", s_configdata).get("interval").to_uint(); + } public: - static void create(Caliper* c, Channel* channel) { + static void create(Caliper* c, Channel* channel) + { ThreadMonitor* instance = new ThreadMonitor(c, channel); - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->post_init_cb(); - }); - channel->events().finish_evt.connect( - [instance](Caliper*, Channel*){ - instance->finish_cb(); - delete instance; - }); - - Log(1).stream() << channel->name() - << ": Registered thread_monitor service" - << std::endl; - } + channel->events().post_init_evt.connect([instance](Caliper* c, Channel* channel) { instance->post_init_cb(); }); + channel->events().finish_evt.connect([instance](Caliper*, Channel*) { + instance->finish_cb(); + delete instance; + }); + Log(1).stream() << channel->name() << ": Registered thread_monitor service" << std::endl; + } }; -const ConfigSet::Entry ThreadMonitor::s_configdata[] = { - { "interval", CALI_TYPE_INT, "2", - "Monitor snapshot interval in seconds.", - "Monitor snapshot interval in seconds." - }, - ConfigSet::Terminator -}; +const ConfigSet::Entry ThreadMonitor::s_configdata[] = { { "interval", + CALI_TYPE_INT, + "2", + "Monitor snapshot interval in seconds.", + "Monitor snapshot interval in seconds." }, + ConfigSet::Terminator }; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/monitor/Timeseries.cpp b/src/services/monitor/Timeseries.cpp index 48555ecf2..be6887aaf 100644 --- a/src/services/monitor/Timeseries.cpp +++ b/src/services/monitor/Timeseries.cpp @@ -24,8 +24,9 @@ namespace inline double get_timestamp() { - return 1e-6 * std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()).count(); + return 1e-6 + * std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()) + .count(); } class TimeseriesService @@ -36,22 +37,24 @@ class TimeseriesService ConfigManager::ChannelPtr m_timeprofile; - unsigned m_snapshots; + unsigned m_snapshots; static const char* s_profile_spec; - void snapshot_cb(Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& srec) { - double ts_now = get_timestamp(); + void snapshot_cb(Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& srec) + { + double ts_now = get_timestamp(); Variant v_prev = c->exchange(m_timestamp_attr, Variant(ts_now)); - std::array ts_entries = { - Entry(m_timestamp_attr, v_prev), - Entry(m_snapshot_attr, cali_make_variant_from_uint(m_snapshots)) - }; + std::array ts_entries = { Entry(m_timestamp_attr, v_prev), + Entry(m_snapshot_attr, cali_make_variant_from_uint(m_snapshots)) }; Channel prof_chn = m_timeprofile->channel(); - c->flush(&prof_chn, info, [c,channel,info,ts_entries](CaliperMetadataAccessInterface&, const std::vector& frec){ + c->flush( + &prof_chn, + info, + [c, channel, info, ts_entries](CaliperMetadataAccessInterface&, const std::vector& frec) { std::vector rec; rec.reserve(frec.size() + info.size() + ts_entries.size()); rec.insert(rec.end(), frec.begin(), frec.end()); @@ -59,7 +62,8 @@ class TimeseriesService rec.insert(rec.end(), ts_entries.begin(), ts_entries.end()); channel->events().process_snapshot(c, channel, SnapshotView(), SnapshotView(rec.size(), rec.data())); - }); + } + ); c->clear(&prof_chn); @@ -69,43 +73,40 @@ class TimeseriesService ++m_snapshots; } - void post_init_cb(Caliper* c, Channel*) { + void post_init_cb(Caliper* c, Channel*) + { c->set(m_timestamp_attr, Variant(get_timestamp())); m_timeprofile->start(); } - void finish_cb(Caliper*, Channel* channel) { - Log(1).stream() << channel->name() << ": timeseries: Processed " - << m_snapshots << " snapshots\n"; + void finish_cb(Caliper*, Channel* channel) + { + Log(1).stream() << channel->name() << ": timeseries: Processed " << m_snapshots << " snapshots\n"; } TimeseriesService(Caliper* c, Channel* channel, ConfigManager::ChannelPtr prof) - : m_timeprofile { prof }, - m_snapshots { 0 } + : m_timeprofile { prof }, m_snapshots { 0 } { m_timestamp_attr = - c->create_attribute("timeseries.starttime", CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS); + c->create_attribute("timeseries.starttime", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); m_snapshot_attr = - c->create_attribute("timeseries.snapshot", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS); - m_duration_attr = - c->create_attribute("timeseries.duration", CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); + c->create_attribute("timeseries.snapshot", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + m_duration_attr = c->create_attribute( + "timeseries.duration", + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); } public: static const char* s_spec; - static void create(Caliper* c, Channel* channel) { + static void create(Caliper* c, Channel* channel) + { std::string profile_cfg_str = "timeseries.profile"; - ConfigSet cfg = services::init_config_from_spec(channel->config(), s_spec); + ConfigSet cfg = services::init_config_from_spec(channel->config(), s_spec); std::string profile_opts = cfg.get("profile.options").to_string(); if (profile_opts.size() > 0) profile_cfg_str.append("(").append(profile_opts).append(")"); @@ -115,33 +116,30 @@ class TimeseriesService mgr.add(profile_cfg_str.c_str()); if (mgr.error()) { - Log(0).stream() << channel->name() << ": timeseries: Profile config error: " - << mgr.error_msg() << "\n"; + Log(0).stream() << channel->name() << ": timeseries: Profile config error: " << mgr.error_msg() << "\n"; return; } auto profile = mgr.get_channel("timeseries.profile"); if (!profile) { - Log(0).stream() << channel->name() - << ": timeseries: Cannot create profile channel\n"; + Log(0).stream() << channel->name() << ": timeseries: Cannot create profile channel\n"; return; } TimeseriesService* instance = new TimeseriesService(c, channel, profile); channel->events().snapshot.connect( - [instance](Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec){ + [instance](Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec) { instance->snapshot_cb(c, channel, info, rec); - }); - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->post_init_cb(c, channel); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - delete instance; - }); + } + ); + channel->events().post_init_evt.connect([instance](Caliper* c, Channel* channel) { + instance->post_init_cb(c, channel); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + delete instance; + }); Log(1).stream() << channel->name() << ": Registered timeseries service\n"; } @@ -176,7 +174,7 @@ const char* TimeseriesService::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/mpireport/MpiReport.cpp b/src/services/mpireport/MpiReport.cpp index fc1ae7331..086673a92 100644 --- a/src/services/mpireport/MpiReport.cpp +++ b/src/services/mpireport/MpiReport.cpp @@ -36,7 +36,8 @@ class MpiReport std::string m_filename; bool m_append_to_file; - void write_output_cb(Caliper* c, Channel* channel, SnapshotView flush_info) { + void write_output_cb(Caliper* c, Channel* channel, SnapshotView flush_info) + { // check if we can use MPI int initialized = 0; @@ -46,8 +47,7 @@ class MpiReport PMPI_Finalized(&finalized); if (finalized) { - Log(0).stream() << channel->name() - << ": mpireport: MPI is already finalized. Cannot aggregate output." + Log(0).stream() << channel->name() << ": mpireport: MPI is already finalized. Cannot aggregate output." << std::endl; return; } @@ -77,7 +77,8 @@ class MpiReport MPI_Comm_free(&comm); } - void connect_mpi_finalize(Channel* channel) { + void connect_mpi_finalize(Channel* channel) + { MpiEvents* events = mpiwrap_get_events(channel); if (!events) { @@ -85,59 +86,52 @@ class MpiReport return; } - events->mpi_finalize_evt.connect( - [](Caliper* c, Channel* channel){ - c->flush_and_write(channel, SnapshotView()); - }); + events->mpi_finalize_evt.connect([](Caliper* c, Channel* channel) { + c->flush_and_write(channel, SnapshotView()); + }); } - MpiReport(const QuerySpec& cross_spec, const QuerySpec &local_spec, const std::string& filename, bool append) - : m_cross_spec(cross_spec), - m_local_spec(local_spec), - m_filename(filename), - m_append_to_file(append) - { } + MpiReport(const QuerySpec& cross_spec, const QuerySpec& local_spec, const std::string& filename, bool append) + : m_cross_spec(cross_spec), m_local_spec(local_spec), m_filename(filename), m_append_to_file(append) + {} public: static const char* s_spec; - static void init(Caliper* c, Channel* chn) { - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); + static void init(Caliper* c, Channel* chn) + { + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); std::string cross_cfg = config.get("config").to_string(); std::string local_cfg = config.get("local_config").to_string(); - CalQLParser cross_parser(cross_cfg.c_str()); CalQLParser local_parser(local_cfg.empty() ? cross_cfg.c_str() : local_cfg.c_str()); - for ( const auto *p : { &cross_parser, &local_parser } ) + for (const auto* p : { &cross_parser, &local_parser }) if (p->error()) { - Log(0).stream() << chn->name() << ": mpireport: config parse error: " - << p->error_msg() << std::endl; + Log(0).stream() << chn->name() << ": mpireport: config parse error: " << p->error_msg() << std::endl; return; } - MpiReport* instance = - new MpiReport(cross_parser.spec(), local_parser.spec(), - config.get("filename").to_string(), config.get("append").to_bool()); + MpiReport* instance = new MpiReport( + cross_parser.spec(), + local_parser.spec(), + config.get("filename").to_string(), + config.get("append").to_bool() + ); - chn->events().write_output_evt.connect( - [instance](Caliper* c, Channel* chn, SnapshotView info){ - instance->write_output_cb(c, chn, info); - }); - chn->events().finish_evt.connect( - [instance](Caliper*, Channel*){ - delete instance; - }); + chn->events().write_output_evt.connect([instance](Caliper* c, Channel* chn, SnapshotView info) { + instance->write_output_cb(c, chn, info); + }); + chn->events().finish_evt.connect([instance](Caliper*, Channel*) { delete instance; }); if (config.get("write_on_finalize").to_bool() == true) { - chn->events().post_init_evt.connect( - [instance](Caliper*, Channel* channel){ - instance->connect_mpi_finalize(channel); - }); + chn->events().post_init_evt.connect([instance](Caliper*, Channel* channel) { + instance->connect_mpi_finalize(channel); + }); } Log(1).stream() << chn->name() << ": Registered mpireport service" << std::endl; @@ -175,7 +169,7 @@ const char* MpiReport::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/mpit/MpitServiceMPI.cpp b/src/services/mpit/MpitServiceMPI.cpp index c211f9c2b..edecf0883 100644 --- a/src/services/mpit/MpitServiceMPI.cpp +++ b/src/services/mpit/MpitServiceMPI.cpp @@ -58,78 +58,78 @@ using namespace std; namespace cali { - bool mpit_enabled; - // vector mpit_pvar_attr; - // vector watermark_changed_attr; - // vector watermark_change_attr; -} +bool mpit_enabled; +// vector mpit_pvar_attr; +// vector watermark_changed_attr; +// vector watermark_change_attr; +} // namespace cali namespace { - struct PvarInfo { - std::vector handles; - std::vector counts; - int index; - int continuous; - int readonly; - MPI_Datatype datatype; - int pvarclass; - int bind; - int verbosity; - std::string name; - std::string desc; - int atomic; - bool aggregatable; - Attribute attr; - }; - - std::vector pvar_selection; - - std::vector pvars; - MPI_T_pvar_session pvar_session; - - unsigned num_pvars_read = 0; - unsigned num_pvars_read_error = 0; - - //Arrays storing last values of PVARs. This is a hack. Only in place because current MPI implementations do not - //support resetting of PVARs. So we store last value of pvars and subtract it - // vector > last_value_unsigned_long; - // vector > last_value_double; - - ConfigSet config; - - ConfigSet::Entry configdata[] = { - { "pvars", CALI_TYPE_STRING, "", - "List of comma-separated PVARs to read", - "List of comma-separated PVARs to read. Default: all" - }, - ConfigSet::Terminator - }; - - int num_pvars = 0; - - void snapshot_cb(Caliper* c, int scope, const SnapshotRecord*, SnapshotRecord* snapshot) { - for (const PvarInfo& pvi : pvars) { - if (pvi.attr == Attribute::invalid) - continue; - - if (pvi.handles.size() > 0 && pvi.counts.size() > 0 && pvi.counts[0] == 1) { - // read only PVAR's with count one for now - unsigned char buf[64]; - int ret = MPI_T_pvar_read(pvar_session, pvi.handles[0], buf); - - if (ret == MPI_SUCCESS) { - snapshot->append(pvi.attr.id(), Variant(pvi.attr.type(), buf, 8)); - ++num_pvars_read; - } else { - ++num_pvars_read_error; - } +struct PvarInfo { + std::vector handles; + std::vector counts; + int index; + int continuous; + int readonly; + MPI_Datatype datatype; + int pvarclass; + int bind; + int verbosity; + std::string name; + std::string desc; + int atomic; + bool aggregatable; + Attribute attr; +}; + +std::vector pvar_selection; + +std::vector pvars; +MPI_T_pvar_session pvar_session; + +unsigned num_pvars_read = 0; +unsigned num_pvars_read_error = 0; + +//Arrays storing last values of PVARs. This is a hack. Only in place because current MPI implementations do not +//support resetting of PVARs. So we store last value of pvars and subtract it +// vector > last_value_unsigned_long; +// vector > last_value_double; + +ConfigSet config; + +ConfigSet::Entry configdata[] = { { "pvars", + CALI_TYPE_STRING, + "", + "List of comma-separated PVARs to read", + "List of comma-separated PVARs to read. Default: all" }, + ConfigSet::Terminator }; + +int num_pvars = 0; + +void snapshot_cb(Caliper* c, int scope, const SnapshotRecord*, SnapshotRecord* snapshot) +{ + for (const PvarInfo& pvi : pvars) { + if (pvi.attr == Attribute::invalid) + continue; + + if (pvi.handles.size() > 0 && pvi.counts.size() > 0 && pvi.counts[0] == 1) { + // read only PVAR's with count one for now + unsigned char buf[64]; + int ret = MPI_T_pvar_read(pvar_session, pvi.handles[0], buf); + + if (ret == MPI_SUCCESS) { + snapshot->append(pvi.attr.id(), Variant(pvi.attr.type(), buf, 8)); + ++num_pvars_read; } else { ++num_pvars_read_error; } + } else { + ++num_pvars_read_error; } - - /* + } + + /* int size, watermark_changed; unsigned long long int temp_unsigned, temp_unsigned_array[MAX_COUNT] = {0}; double temp_double, temp_double_array[MAX_COUNT] = {0.0}; @@ -284,74 +284,70 @@ namespace } } */ - } - - void create_attribute_for_pvar(Caliper *c, PvarInfo& pvi) { - Attribute aggr_class_attr = c->get_attribute("class.aggregatable"); - Variant v_true(true); +} - cali_attr_type type = CALI_TYPE_INV; - - const struct calimpi_type_info_t { - MPI_Datatype mpitype; - cali_attr_type calitype; - } calimpi_types[] = { - { MPI_COUNT, CALI_TYPE_UINT }, - { MPI_UNSIGNED, CALI_TYPE_UINT }, - { MPI_UNSIGNED_LONG, CALI_TYPE_UINT }, - { MPI_UNSIGNED_LONG_LONG, CALI_TYPE_UINT }, - { MPI_INT, CALI_TYPE_INT }, - { MPI_DOUBLE, CALI_TYPE_DOUBLE }, - { MPI_CHAR, CALI_TYPE_STRING } - }; - - for (calimpi_type_info_t i : calimpi_types) - if (pvi.datatype == i.mpitype) { - type = i.calitype; - break; - } - - if (type != CALI_TYPE_INV) { - pvi.attr = - c->create_attribute(string("mpit.")+pvi.name, CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS, - (pvi.aggregatable ? 1 : 0), &aggr_class_attr, &v_true); - } else { - Log(1).stream() << "mpit: Cannot create attribute for PVAR with index " << pvi.index - << " (" << pvi.name << "): unsupported MPI datatype." << std::endl; +void create_attribute_for_pvar(Caliper* c, PvarInfo& pvi) +{ + Attribute aggr_class_attr = c->get_attribute("class.aggregatable"); + Variant v_true(true); + + cali_attr_type type = CALI_TYPE_INV; + + const struct calimpi_type_info_t { + MPI_Datatype mpitype; + cali_attr_type calitype; + } calimpi_types[] = { { MPI_COUNT, CALI_TYPE_UINT }, + { MPI_UNSIGNED, CALI_TYPE_UINT }, + { MPI_UNSIGNED_LONG, CALI_TYPE_UINT }, + { MPI_UNSIGNED_LONG_LONG, CALI_TYPE_UINT }, + { MPI_INT, CALI_TYPE_INT }, + { MPI_DOUBLE, CALI_TYPE_DOUBLE }, + { MPI_CHAR, CALI_TYPE_STRING } }; + + for (calimpi_type_info_t i : calimpi_types) + if (pvi.datatype == i.mpitype) { + type = i.calitype; + break; } + + if (type != CALI_TYPE_INV) { + pvi.attr = c->create_attribute( + string("mpit.") + pvi.name, + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS, + (pvi.aggregatable ? 1 : 0), + &aggr_class_attr, + &v_true + ); + } else { + Log(1).stream() << "mpit: Cannot create attribute for PVAR with index " << pvi.index << " (" << pvi.name + << "): unsupported MPI datatype." << std::endl; } +} - bool is_pvar_class_aggregatable(int index, int pvarclass) { - // General idea to determine aggretablitity of a PVAR: - // Any PVAR that represents an internal MPI state is by default not aggregatable - // A PVAR is aggregatable if it "makes sense" to apply one or more of these operators to it: - // COUNT, SUM, MIN, MAX, AVG. In the future, more operators may be considered. - - const struct aggregateable_t { - int pvarclass; bool state; - } aggregateable_table[] = { - { MPI_T_PVAR_CLASS_STATE, false }, - { MPI_T_PVAR_CLASS_LEVEL, true }, - { MPI_T_PVAR_CLASS_SIZE, false }, - { MPI_T_PVAR_CLASS_PERCENTAGE, true }, - { MPI_T_PVAR_CLASS_HIGHWATERMARK, false }, - { MPI_T_PVAR_CLASS_LOWWATERMARK, false }, - { MPI_T_PVAR_CLASS_COUNTER, true }, - { MPI_T_PVAR_CLASS_AGGREGATE, true }, - { MPI_T_PVAR_CLASS_TIMER, true }, - { MPI_T_PVAR_CLASS_GENERIC, false } - }; - - for (const aggregateable_t &atp : aggregateable_table) - if (atp.pvarclass == pvarclass) - return atp.state; - - return false; - - /* +bool is_pvar_class_aggregatable(int index, int pvarclass) +{ + // General idea to determine aggretablitity of a PVAR: + // Any PVAR that represents an internal MPI state is by default not aggregatable + // A PVAR is aggregatable if it "makes sense" to apply one or more of these operators to it: + // COUNT, SUM, MIN, MAX, AVG. In the future, more operators may be considered. + + const struct aggregateable_t { + int pvarclass; + bool state; + } aggregateable_table[] = { { MPI_T_PVAR_CLASS_STATE, false }, { MPI_T_PVAR_CLASS_LEVEL, true }, + { MPI_T_PVAR_CLASS_SIZE, false }, { MPI_T_PVAR_CLASS_PERCENTAGE, true }, + { MPI_T_PVAR_CLASS_HIGHWATERMARK, false }, { MPI_T_PVAR_CLASS_LOWWATERMARK, false }, + { MPI_T_PVAR_CLASS_COUNTER, true }, { MPI_T_PVAR_CLASS_AGGREGATE, true }, + { MPI_T_PVAR_CLASS_TIMER, true }, { MPI_T_PVAR_CLASS_GENERIC, false } }; + + for (const aggregateable_t& atp : aggregateable_table) + if (atp.pvarclass == pvarclass) + return atp.state; + + return false; + + /* switch(pvi.pvarclass) { Attribute attr; Attribute aggr_class_attr = c->get_attribute("class.aggregatable"); @@ -393,113 +389,118 @@ namespace } } */ - } - +} - // Allocate PVAR handles when the PVAR is bound to something other than MPI_T_BIND_NO_OBJECT - void do_mpit_allocate_bound_pvar_handles(Caliper *c, void *in_handle, int bind) { - for (PvarInfo& pvi : pvars) { - if (bind == pvi.bind) { - MPI_T_pvar_handle handle; - int count; +// Allocate PVAR handles when the PVAR is bound to something other than MPI_T_BIND_NO_OBJECT +void do_mpit_allocate_bound_pvar_handles(Caliper* c, void* in_handle, int bind) +{ + for (PvarInfo& pvi : pvars) { + if (bind == pvi.bind) { + MPI_T_pvar_handle handle; + int count; - int ret = MPI_T_pvar_handle_alloc(pvar_session, pvi.index, in_handle, &handle, &count); + int ret = MPI_T_pvar_handle_alloc(pvar_session, pvi.index, in_handle, &handle, &count); - if (ret != MPI_SUCCESS) { - Log(0).stream() << "mpit: MPI_T_pvar_handle_alloc error for PVAR at index " << pvi.index - << " (" << pvi.name << ")" << std::endl; - continue; - } + if (ret != MPI_SUCCESS) { + Log(0).stream() << "mpit: MPI_T_pvar_handle_alloc error for PVAR at index " << pvi.index << " (" + << pvi.name << ")" << std::endl; + continue; + } - pvi.handles.push_back(handle); - pvi.counts.push_back(count); + pvi.handles.push_back(handle); + pvi.counts.push_back(count); - if (pvi.continuous) { - ret = MPI_T_pvar_start(pvar_session, pvi.handles[pvi.handles.size()-1]); + if (pvi.continuous) { + ret = MPI_T_pvar_start(pvar_session, pvi.handles[pvi.handles.size() - 1]); - if (ret != MPI_SUCCESS) { - Log(0).stream() << "mpit: MPI_T_pvar_start error for PVAR at index " << pvi.index - << " (" << pvi.name << ")" << std::endl; - } + if (ret != MPI_SUCCESS) { + Log(0).stream() << "mpit: MPI_T_pvar_start error for PVAR at index " << pvi.index << " (" + << pvi.name << ")" << std::endl; } } + } - if (pvi.attr == Attribute::invalid) { - create_attribute_for_pvar(c, pvi); - } + if (pvi.attr == Attribute::invalid) { + create_attribute_for_pvar(c, pvi); } } +} - /*Allocate handles for pvars and create attributes*/ - void do_mpit_allocate_pvar_handles(Caliper *c) { - int current_num_pvars = 0; +/*Allocate handles for pvars and create attributes*/ +void do_mpit_allocate_pvar_handles(Caliper* c) +{ + int current_num_pvars = 0; - { - int ret = MPI_T_pvar_get_num(¤t_num_pvars); + { + int ret = MPI_T_pvar_get_num(¤t_num_pvars); - if (ret != MPI_SUCCESS) { - perror("MPI_T_pvar_get_num ERROR:"); - Log(0).stream() << "MPI_T_pvar_get_num ERROR." << std::endl; - return; - } + if (ret != MPI_SUCCESS) { + perror("MPI_T_pvar_get_num ERROR:"); + Log(0).stream() << "MPI_T_pvar_get_num ERROR." << std::endl; + return; } + } - // watermark_changed_attr.resize(current_num_pvars); - // watermark_change_attr.resize(current_num_pvars); - // last_value_unsigned_long.resize(current_num_pvars, {0}); - // last_value_double.resize(current_num_pvars, {0.0}); - - Log(1).stream() << "mpit: Exporting " << current_num_pvars << " PVARs." << std::endl; - - for (int index = num_pvars; index < current_num_pvars; index++) { - PvarInfo pvi; - - char namebuf[NAME_LEN] = { 0 }; - char descbuf[NAME_LEN] = { 0 }; - int namelen = NAME_LEN; - int desclen = NAME_LEN; - - MPI_T_enum enumtype; - - int iret = MPI_T_pvar_get_info(index, - namebuf, &namelen, - &pvi.verbosity, - &pvi.pvarclass, - &pvi.datatype, - &enumtype, - descbuf, &desclen, - &pvi.bind, - &pvi.readonly, - &pvi.continuous, - &pvi.atomic); - - pvi.index = index; - - if (namelen > 0) - pvi.name.assign(namebuf, namelen-1); - if (desclen > 0) - pvi.desc.assign(descbuf, desclen-1); - - // see if this pvar is in the selection list - if (!pvar_selection.empty()) { - auto it = std::find(pvar_selection.begin(), pvar_selection.end(), pvi.name); - - if (it == pvar_selection.end()) { - continue; - } + // watermark_changed_attr.resize(current_num_pvars); + // watermark_change_attr.resize(current_num_pvars); + // last_value_unsigned_long.resize(current_num_pvars, {0}); + // last_value_double.resize(current_num_pvars, {0.0}); + + Log(1).stream() << "mpit: Exporting " << current_num_pvars << " PVARs." << std::endl; + + for (int index = num_pvars; index < current_num_pvars; index++) { + PvarInfo pvi; + + char namebuf[NAME_LEN] = { 0 }; + char descbuf[NAME_LEN] = { 0 }; + int namelen = NAME_LEN; + int desclen = NAME_LEN; + + MPI_T_enum enumtype; + + int iret = MPI_T_pvar_get_info( + index, + namebuf, + &namelen, + &pvi.verbosity, + &pvi.pvarclass, + &pvi.datatype, + &enumtype, + descbuf, + &desclen, + &pvi.bind, + &pvi.readonly, + &pvi.continuous, + &pvi.atomic + ); + + pvi.index = index; + + if (namelen > 0) + pvi.name.assign(namebuf, namelen - 1); + if (desclen > 0) + pvi.desc.assign(descbuf, desclen - 1); + + // see if this pvar is in the selection list + if (!pvar_selection.empty()) { + auto it = std::find(pvar_selection.begin(), pvar_selection.end(), pvi.name); + + if (it == pvar_selection.end()) { + continue; } + } - pvi.aggregatable = is_pvar_class_aggregatable(index, pvi.pvarclass); + pvi.aggregatable = is_pvar_class_aggregatable(index, pvi.pvarclass); - // allocate a pvar handle that will be used later + // allocate a pvar handle that will be used later - int ret = -1; + int ret = -1; - switch (pvi.bind) { - case MPI_T_BIND_NO_OBJECT: + switch (pvi.bind) { + case MPI_T_BIND_NO_OBJECT: { MPI_T_pvar_handle handle; - int count; + int count; ret = MPI_T_pvar_handle_alloc(pvar_session, index, nullptr, &handle, &count); @@ -507,15 +508,15 @@ namespace pvi.counts.push_back(count); break; } - case MPI_T_BIND_MPI_COMM: - // Handle this through a Wrapper call to MPI_Comm_create(). - // Support MPI_COMM_WORLD, MPI_COMM_SELF as default. + case MPI_T_BIND_MPI_COMM: + // Handle this through a Wrapper call to MPI_Comm_create(). + // Support MPI_COMM_WORLD, MPI_COMM_SELF as default. { MPI_T_pvar_handle handle; - int count; + int count; MPI_Comm comm = MPI_COMM_WORLD; - ret = MPI_T_pvar_handle_alloc(pvar_session, index, &comm, &handle, &count); + ret = MPI_T_pvar_handle_alloc(pvar_session, index, &comm, &handle, &count); if (ret == MPI_SUCCESS) { pvi.handles.push_back(handle); @@ -525,7 +526,7 @@ namespace } comm = MPI_COMM_SELF; - ret = MPI_T_pvar_handle_alloc(pvar_session, index, &comm, &handle, &count); + ret = MPI_T_pvar_handle_alloc(pvar_session, index, &comm, &handle, &count); if (ret == MPI_SUCCESS) { pvi.handles.push_back(handle); @@ -534,109 +535,110 @@ namespace break; } - default: - break; - // Do nothing here - } + default: + break; + // Do nothing here + } - if (ret != MPI_SUCCESS) { - Log(0).stream() << "mpit: MPI_T_pvar_handle_alloc ERROR: " << ret - << " for PVAR at index " << index << " with name " << pvi.name << std::endl; - return; - } + if (ret != MPI_SUCCESS) { + Log(0).stream() << "mpit: MPI_T_pvar_handle_alloc ERROR: " << ret << " for PVAR at index " << index + << " with name " << pvi.name << std::endl; + return; + } - if (pvi.counts.size() > 0 && pvi.counts[0] > 1) { - Log(1).stream() << "mpit: PVAR at index " << pvi.index - << " (" << pvi.name << ") has count > 1 (count = " << pvi.counts[0] - << "), skipping." << std::endl; + if (pvi.counts.size() > 0 && pvi.counts[0] > 1) { + Log(1).stream() << "mpit: PVAR at index " << pvi.index << " (" << pvi.name + << ") has count > 1 (count = " << pvi.counts[0] << "), skipping." << std::endl; - continue; - } + continue; + } - // Non-continuous variables need to be started before being read. + // Non-continuous variables need to be started before being read. - if (pvi.continuous == 0) { - Log(1).stream() << "mpit: PVAR \'" << pvi.name << "\' at index " << index - << "is non-continuous. Starting this PVAR." - << std::endl; + if (pvi.continuous == 0) { + Log(1).stream() << "mpit: PVAR \'" << pvi.name << "\' at index " << index + << "is non-continuous. Starting this PVAR." << std::endl; - ret = MPI_T_pvar_start(pvar_session, pvi.handles[0]); + ret = MPI_T_pvar_start(pvar_session, pvi.handles[0]); - if (ret != MPI_SUCCESS) { - Log(0).stream() << "mpit: MPI_T_pvar_start ERROR:" << ret - << " for PVAR at index " << index << " with name " << pvi.name << endl; - return; - } + if (ret != MPI_SUCCESS) { + Log(0).stream() << "mpit: MPI_T_pvar_start ERROR:" << ret << " for PVAR at index " << index + << " with name " << pvi.name << endl; + return; } - - create_attribute_for_pvar(c, pvi); + } - if (pvi.attr != Attribute::invalid) { - Log(2).stream() << "mpit: Registered PVAR " << pvi.index - << " (" << pvi.name << ")" << std::endl; + create_attribute_for_pvar(c, pvi); - pvars.push_back(pvi); - } - } - } + if (pvi.attr != Attribute::invalid) { + Log(2).stream() << "mpit: Registered PVAR " << pvi.index << " (" << pvi.name << ")" << std::endl; - void finish_cb(Caliper*) { - Log(1).stream() << "mpit: " << num_pvars_read << " PVARs read, " - << num_pvars_read_error << " PVAR read errors." << std::endl; + pvars.push_back(pvi); + } } +} - // Register the service and initalize the MPI-T interface - void do_mpit_init(Caliper *c) - { - int thread_provided; - - config = RuntimeConfig::init("mpit", configdata); +void finish_cb(Caliper*) +{ + Log(1).stream() << "mpit: " << num_pvars_read << " PVARs read, " << num_pvars_read_error << " PVAR read errors." + << std::endl; +} - util::split(config.get("pvars").to_string(), ',', std::back_inserter(pvar_selection)); - - /* Initialize MPI_T */ - int return_val = MPI_T_init_thread(MPI_THREAD_SINGLE, &thread_provided); +// Register the service and initalize the MPI-T interface +void do_mpit_init(Caliper* c) +{ + int thread_provided; - if (return_val != MPI_SUCCESS) { - Log(0).stream() << "MPI_T_init_thread ERROR: " << return_val << ". MPIT service disabled." << endl; - return; - } + config = RuntimeConfig::init("mpit", configdata); - /* Track a performance pvar session */ - return_val = MPI_T_pvar_session_create(&pvar_session); - if (return_val != MPI_SUCCESS) { - Log(0).stream() << "MPI_T_pvar_session_create ERROR: " << return_val << ". MPIT service disabled." << endl; - return; - } - - do_mpit_allocate_pvar_handles(c); + util::split(config.get("pvars").to_string(), ',', std::back_inserter(pvar_selection)); - c->events().snapshot.connect(&snapshot_cb); - c->events().finish_evt.connect(&finish_cb); + /* Initialize MPI_T */ + int return_val = MPI_T_init_thread(MPI_THREAD_SINGLE, &thread_provided); - Log(1).stream() << "mpit: MPI-T initialized." << endl; + if (return_val != MPI_SUCCESS) { + Log(0).stream() << "MPI_T_init_thread ERROR: " << return_val << ". MPIT service disabled." << endl; + return; } - /*Thin wrapper functions to invoke pvar allocation function from another module*/ - void mpit_allocate_pvar_handles() { - Caliper c; - ::do_mpit_allocate_pvar_handles(&c); + /* Track a performance pvar session */ + return_val = MPI_T_pvar_session_create(&pvar_session); + if (return_val != MPI_SUCCESS) { + Log(0).stream() << "MPI_T_pvar_session_create ERROR: " << return_val << ". MPIT service disabled." << endl; + return; } - void mpit_allocate_bound_pvar_handles(void *handle, int bind) { - Caliper c; - ::do_mpit_allocate_bound_pvar_handles(&c, handle, bind); - } + do_mpit_allocate_pvar_handles(c); - void mpit_init(Caliper* c) { - MpiEvents::events.mpi_init_evt.connect(::do_mpit_allocate_pvar_handles); + c->events().snapshot.connect(&snapshot_cb); + c->events().finish_evt.connect(&finish_cb); - ::do_mpit_init(c); - } + Log(1).stream() << "mpit: MPI-T initialized." << endl; +} -} // anonymous namespace +/*Thin wrapper functions to invoke pvar allocation function from another module*/ +void mpit_allocate_pvar_handles() +{ + Caliper c; + ::do_mpit_allocate_pvar_handles(&c); +} + +void mpit_allocate_bound_pvar_handles(void* handle, int bind) +{ + Caliper c; + ::do_mpit_allocate_bound_pvar_handles(&c, handle, bind); +} -namespace cali +void mpit_init(Caliper* c) +{ + MpiEvents::events.mpi_init_evt.connect(::do_mpit_allocate_pvar_handles); + + ::do_mpit_init(c); +} + +} // anonymous namespace + +namespace cali { CaliperService mpit_service = { "mpit", ::mpit_init }; diff --git a/src/services/mpiwrap/MpiPattern.cpp b/src/services/mpiwrap/MpiPattern.cpp index d5385e7ef..9cdd23e3a 100644 --- a/src/services/mpiwrap/MpiPattern.cpp +++ b/src/services/mpiwrap/MpiPattern.cpp @@ -15,8 +15,7 @@ using namespace cali; -struct MpiPattern::MpiPatternImpl -{ +struct MpiPattern::MpiPatternImpl { // --- The attributes // @@ -33,11 +32,9 @@ struct MpiPattern::MpiPatternImpl // struct RequestInfo { - enum { - Unknown, Send, Recv - } op { Unknown }; + enum { Unknown, Send, Recv } op { Unknown }; - bool is_persistent { false }; + bool is_persistent { false }; int target; int tag; @@ -52,38 +49,54 @@ struct MpiPattern::MpiPatternImpl // --- initialization // - void init_attributes(Caliper* c) { + void init_attributes(Caliper* c) + { const struct attr_info_t { - const char* name; cali_attr_type type; int prop; Attribute* ptr; - } attr_info_tbl[] = { - { "mpi.recv.size", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_recv_size_attr }, - { "mpi.send.size", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_send_size_attr }, - { "mpi.coll.size", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_coll_size_attr }, - - { "total.send.count", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_send_count_attr }, - { "total.recv.count", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_recv_count_attr }, - { "total.coll.count", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_coll_count_attr }, - { "total.dest.ranks", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_dest_ranks_attr }, - { "total.src.ranks", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &total_src_ranks_attr }, - - { nullptr, CALI_TYPE_INV, 0, nullptr } - }; + const char* name; + cali_attr_type type; + int prop; + Attribute* ptr; + } attr_info_tbl[] = { { "mpi.recv.size", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_recv_size_attr }, + { "mpi.send.size", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_send_size_attr }, + { "mpi.coll.size", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_coll_size_attr }, + + { "total.send.count", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_send_count_attr }, + { "total.recv.count", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_recv_count_attr }, + { "total.coll.count", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_coll_count_attr }, + { "total.dest.ranks", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_dest_ranks_attr }, + { "total.src.ranks", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &total_src_ranks_attr }, + + { nullptr, CALI_TYPE_INV, 0, nullptr } }; for (const struct attr_info_t* a = attr_info_tbl; a && a->name; a++) *(a->ptr) = c->create_attribute(a->name, a->type, a->prop); } - void init_mpi(Caliper* c, Channel* chn) { - req_map.reserve(100); - } + void init_mpi(Caliper* c, Channel* chn) { req_map.reserve(100); } // --- point-to-point // @@ -98,14 +111,25 @@ struct MpiPattern::MpiPatternImpl std::set unique_src_ranks; std::set unique_dest_ranks; - void push_send_event(Caliper* c, Channel* channel, int size, int dest, int tag) { + void push_send_event(Caliper* c, Channel* channel, int size, int dest, int tag) + { total_send_count++; total_send_size += size; unique_dest_ranks.insert(dest); } - void handle_send_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm, MPI_Request* req) { + void handle_send_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int dest, + int tag, + MPI_Comm comm, + MPI_Request* req + ) + { RequestInfo info; info.op = RequestInfo::Send; @@ -118,29 +142,40 @@ struct MpiPattern::MpiPatternImpl PMPI_Type_size(type, &info.size); info.size *= count; - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); req_map[*req] = info; } - void push_recv_event(Caliper* c, Channel* channel, int src, int size, int tag) { + void push_recv_event(Caliper* c, Channel* channel, int src, int size, int tag) + { total_recv_count++; total_recv_size += size; unique_src_ranks.insert(src); } - void handle_recv(Caliper* c, Channel* chn, MPI_Datatype type, MPI_Comm comm, MPI_Status* status) { - int size = 0; + void handle_recv(Caliper* c, Channel* chn, MPI_Datatype type, MPI_Comm comm, MPI_Status* status) + { + int size = 0; PMPI_Type_size(type, &size); int count = 0; PMPI_Get_count(status, type, &count); - push_recv_event(c, chn, status->MPI_SOURCE, size*count, status->MPI_TAG); + push_recv_event(c, chn, status->MPI_SOURCE, size * count, status->MPI_TAG); } - void handle_irecv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) { + void handle_irecv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ) + { RequestInfo info; info.op = RequestInfo::Recv; @@ -150,13 +185,22 @@ struct MpiPattern::MpiPatternImpl info.type = type; info.count = count; - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); req_map[*req] = info; } - void handle_recv_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) { + void handle_recv_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ) + { RequestInfo info; info.op = RequestInfo::Recv; @@ -167,16 +211,15 @@ struct MpiPattern::MpiPatternImpl info.count = count; info.size = 0; - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); req_map[*req] = info; } - void handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) { + void handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) + { for (int i = 0; i < nreq; ++i) { - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); auto it = req_map.find(reqs[i]); @@ -190,10 +233,10 @@ struct MpiPattern::MpiPatternImpl } } - void handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) { + void handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) + { for (int i = 0; i < nreq; ++i) { - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); auto it = req_map.find(reqs[i]); @@ -203,12 +246,12 @@ struct MpiPattern::MpiPatternImpl RequestInfo info = it->second; if (info.op == RequestInfo::Recv) { - int size = 0; + int size = 0; PMPI_Type_size(info.type, &size); int count = 0; - PMPI_Get_count(statuses+i, info.type, &count); + PMPI_Get_count(statuses + i, info.type, &count); - push_recv_event(c, chn, statuses[i].MPI_SOURCE, size*count, statuses[i].MPI_TAG); + push_recv_event(c, chn, statuses[i].MPI_SOURCE, size * count, statuses[i].MPI_TAG); } if (!info.is_persistent) @@ -216,9 +259,9 @@ struct MpiPattern::MpiPatternImpl } } - void request_free(MPI_Request* req) { - std::lock_guard - g(req_map_lock); + void request_free(MPI_Request* req) + { + std::lock_guard g(req_map_lock); req_map.erase(*req); } @@ -226,12 +269,14 @@ struct MpiPattern::MpiPatternImpl // --- collectives // - void push_coll_event(Caliper* c, Channel* channel, CollectiveType coll_type, int size, int root) { + void push_coll_event(Caliper* c, Channel* channel, CollectiveType coll_type, int size, int root) + { ++total_coll_count; total_coll_size += size; } - void handle_comm_begin(Caliper* c, Channel* chn){ + void handle_comm_begin(Caliper* c, Channel* chn) + { total_send_count = 0; total_recv_count = 0; total_coll_count = 0; @@ -243,19 +288,18 @@ struct MpiPattern::MpiPatternImpl unique_src_ranks.clear(); } - void handle_comm_end(Caliper* c, Channel* chn){ - const Entry data[] = { - { total_send_count_attr, Variant(total_send_count) }, - { total_recv_count_attr, Variant(total_recv_count) }, - { total_coll_count_attr, Variant(total_coll_count) }, + void handle_comm_end(Caliper* c, Channel* chn) + { + const Entry data[] = { { total_send_count_attr, Variant(total_send_count) }, + { total_recv_count_attr, Variant(total_recv_count) }, + { total_coll_count_attr, Variant(total_coll_count) }, - { total_dest_ranks_attr, Variant(unique_dest_ranks.size()) }, - { total_src_ranks_attr, Variant(unique_src_ranks.size()) }, + { total_dest_ranks_attr, Variant(unique_dest_ranks.size()) }, + { total_src_ranks_attr, Variant(unique_src_ranks.size()) }, - { total_recv_size_attr, Variant(total_recv_size) }, - { total_send_size_attr, Variant(total_send_size) }, - { total_coll_size_attr, Variant(total_coll_size) } - }; + { total_recv_size_attr, Variant(total_recv_size) }, + { total_send_size_attr, Variant(total_send_size) }, + { total_coll_size_attr, Variant(total_coll_size) } }; c->push_snapshot(chn, SnapshotView(8, data)); } @@ -263,34 +307,28 @@ struct MpiPattern::MpiPatternImpl // --- constructor // - MpiPatternImpl() - { } + MpiPatternImpl() {} }; - -MpiPattern::MpiPattern() - : mP(new MpiPatternImpl) -{ } +MpiPattern::MpiPattern() : mP(new MpiPatternImpl) +{} MpiPattern::~MpiPattern() { mP.reset(); } -void -MpiPattern::init(Caliper* c, Channel* chn) +void MpiPattern::init(Caliper* c, Channel* chn) { mP->init_attributes(c); } -void -MpiPattern::init_mpi(Caliper* c, Channel* chn) +void MpiPattern::init_mpi(Caliper* c, Channel* chn) { mP->init_mpi(c, chn); } -void -MpiPattern::handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm) +void MpiPattern::handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); @@ -299,50 +337,78 @@ MpiPattern::handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, mP->push_send_event(c, chn, size, dest, tag); } -void -MpiPattern::handle_send_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm, MPI_Request* req) +void MpiPattern::handle_send_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int dest, + int tag, + MPI_Comm comm, + MPI_Request* req +) { mP->handle_send_init(c, chn, count, type, dest, tag, comm, req); } -void -MpiPattern::handle_recv(Caliper* c, Channel* chn, int, MPI_Datatype type, int, int, MPI_Comm comm, MPI_Status* status) +void MpiPattern::handle_recv( + Caliper* c, + Channel* chn, + int, + MPI_Datatype type, + int, + int, + MPI_Comm comm, + MPI_Status* status +) { mP->handle_recv(c, chn, type, comm, status); } -void -MpiPattern::handle_irecv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) +void MpiPattern::handle_irecv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req +) { mP->handle_irecv(c, chn, count, type, src, tag, comm, req); } -void -MpiPattern::handle_recv_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) +void MpiPattern::handle_recv_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req +) { mP->handle_recv_init(c, chn, count, type, src, tag, comm, req); } -void -MpiPattern::handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) +void MpiPattern::handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) { mP->handle_start(c, chn, nreq, reqs); } -void -MpiPattern::handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) +void MpiPattern::handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) { mP->handle_completion(c, chn, nreq, reqs, statuses); } -void -MpiPattern::request_free(Caliper*, Channel*, MPI_Request* req) +void MpiPattern::request_free(Caliper*, Channel*, MPI_Request* req) { mP->request_free(req); } -void -MpiPattern::handle_12n(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) +void MpiPattern::handle_12n(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); @@ -352,8 +418,7 @@ MpiPattern::handle_12n(Caliper* c, Channel* chn, int count, MPI_Datatype type, i mP->push_coll_event(c, chn, Coll_12N, (rank == root ? count : 0) * size, root); } -void -MpiPattern::handle_n21(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) +void MpiPattern::handle_n21(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); @@ -363,41 +428,35 @@ MpiPattern::handle_n21(Caliper* c, Channel* chn, int count, MPI_Datatype type, i mP->push_coll_event(c, chn, Coll_N21, (rank != root ? count : 0) * size, root); } -void -MpiPattern::handle_n2n(Caliper* c, Channel* chn, int count, MPI_Datatype type, MPI_Comm comm) +void MpiPattern::handle_n2n(Caliper* c, Channel* chn, int count, MPI_Datatype type, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); - mP->push_coll_event(c, chn, Coll_NxN, count*size, 0); + mP->push_coll_event(c, chn, Coll_NxN, count * size, 0); } -void -MpiPattern::handle_barrier(Caliper* c, Channel* chn, MPI_Comm comm) +void MpiPattern::handle_barrier(Caliper* c, Channel* chn, MPI_Comm comm) { mP->push_coll_event(c, chn, Coll_Barrier, 0, 0); } -void -MpiPattern::handle_init(Caliper* c, Channel* chn) +void MpiPattern::handle_init(Caliper* c, Channel* chn) { mP->push_coll_event(c, chn, Coll_Init, 0, 0); } -void -MpiPattern::handle_finalize(Caliper* c, Channel* chn) +void MpiPattern::handle_finalize(Caliper* c, Channel* chn) { mP->push_coll_event(c, chn, Coll_Finalize, 0, 0); - } -void -MpiPattern::handle_comm_begin(Caliper* c, Channel* chn) + +void MpiPattern::handle_comm_begin(Caliper* c, Channel* chn) { mP->handle_comm_begin(c, chn); } -void -MpiPattern::handle_comm_end(Caliper* c, Channel* chn) +void MpiPattern::handle_comm_end(Caliper* c, Channel* chn) { mP->handle_comm_end(c, chn); } diff --git a/src/services/mpiwrap/MpiPattern.h b/src/services/mpiwrap/MpiPattern.h index b9eb89192..5d4afd340 100644 --- a/src/services/mpiwrap/MpiPattern.h +++ b/src/services/mpiwrap/MpiPattern.h @@ -23,9 +23,7 @@ class MpiPattern public: - enum CollectiveType { - Unknown, Coll_Barrier, Coll_NxN, Coll_12N, Coll_N21, Coll_Init, Coll_Finalize - }; + enum CollectiveType { Unknown, Coll_Barrier, Coll_NxN, Coll_12N, Coll_N21, Coll_Init, Coll_Finalize }; MpiPattern(); @@ -37,18 +35,54 @@ class MpiPattern // --- point-to-point void handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm); - void handle_send_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm, MPI_Request* req); - - void handle_recv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Status* status); - void handle_irecv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req); - void handle_recv_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req); + void handle_send_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int dest, + int tag, + MPI_Comm comm, + MPI_Request* req + ); + + void handle_recv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Status* status + ); + void handle_irecv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ); + void handle_recv_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ); void handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs); void handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses); - void handle_comm_begin(Caliper* c, Channel* chn);//region_begin - void handle_comm_end(Caliper* c, Channel* chn);//region_end - + void handle_comm_begin(Caliper* c, Channel* chn); //region_begin + void handle_comm_end(Caliper* c, Channel* chn); //region_end + void request_free(Caliper* c, Channel* chn, MPI_Request* req); // --- collectives @@ -61,5 +95,4 @@ class MpiPattern void handle_finalize(Caliper* c, Channel* chn); }; -} - +} // namespace cali diff --git a/src/services/mpiwrap/MpiTracing.cpp b/src/services/mpiwrap/MpiTracing.cpp index 3ed806dd8..24cb5b4e2 100644 --- a/src/services/mpiwrap/MpiTracing.cpp +++ b/src/services/mpiwrap/MpiTracing.cpp @@ -13,9 +13,7 @@ using namespace cali; - -struct MpiTracing::MpiTracingImpl -{ +struct MpiTracing::MpiTracingImpl { // --- The attributes // @@ -39,11 +37,9 @@ struct MpiTracing::MpiTracingImpl // struct RequestInfo { - enum { - Unknown, Send, Recv - } op { Unknown }; + enum { Unknown, Send, Recv } op { Unknown }; - bool is_persistent { false }; + bool is_persistent { false }; int target; int tag; @@ -51,16 +47,16 @@ struct MpiTracing::MpiTracingImpl MPI_Datatype type; int size; - Node* comm_node; + Node* comm_node; }; - std::atomic comm_id; + std::atomic comm_id; // We hope that whatever MPI_Comm and MPI_Request is is default-hashable. // So far it works ... - std::unordered_map comm_map; ///< Communicator map - std::mutex comm_map_lock; + std::unordered_map comm_map; ///< Communicator map + std::mutex comm_map_lock; std::unordered_map req_map; std::mutex req_map_lock; @@ -68,38 +64,41 @@ struct MpiTracing::MpiTracingImpl // --- initialization // - void init_attributes(Caliper* c) { + void init_attributes(Caliper* c) + { const struct attr_info_t { - const char* name; cali_attr_type type; int prop; Attribute* ptr; + const char* name; + cali_attr_type type; + int prop; + Attribute* ptr; } attr_info_tbl[] = { - { "mpi.msg.src", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - &msg_src_attr }, - { "mpi.msg.dst", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - &msg_dst_attr }, - { "mpi.msg.tag", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - &msg_tag_attr }, - - { "mpi.coll.type", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, - &coll_type_attr }, - { "mpi.coll.root", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, - &coll_root_attr }, - - { "mpi.comm", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, - &comm_attr }, - { "mpi.comm.size", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, - &comm_size_attr }, - { "mpi.comm.is_world", CALI_TYPE_BOOL, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, - &comm_is_world_attr }, - { "mpi.comm.list", CALI_TYPE_USR, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, - &comm_list_attr }, - - { "mpi.msg.size", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, - &msg_size_attr }, - { "mpi.send.count", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + { "mpi.msg.src", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, &msg_src_attr }, + { "mpi.msg.dst", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, &msg_dst_attr }, + { "mpi.msg.tag", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, &msg_tag_attr }, + + { "mpi.coll.type", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, &coll_type_attr }, + { "mpi.coll.root", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS, &coll_root_attr }, + + { "mpi.comm", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, &comm_attr }, + { "mpi.comm.size", CALI_TYPE_INT, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, &comm_size_attr }, + { "mpi.comm.is_world", CALI_TYPE_BOOL, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, &comm_is_world_attr }, + { "mpi.comm.list", CALI_TYPE_USR, CALI_ATTR_DEFAULT | CALI_ATTR_SKIP_EVENTS, &comm_list_attr }, + + { "mpi.msg.size", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + &msg_size_attr }, + { "mpi.send.count", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, &send_count_attr }, - { "mpi.recv.count", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + { "mpi.recv.count", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, &recv_count_attr }, - { "mpi.coll.count", CALI_TYPE_INT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + { "mpi.coll.count", + CALI_TYPE_INT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, &coll_count_attr }, { nullptr, CALI_TYPE_INV, 0, nullptr } @@ -109,7 +108,8 @@ struct MpiTracing::MpiTracingImpl *(a->ptr) = c->create_attribute(a->name, a->type, a->prop); } - void init_mpi(Caliper* c, Channel* chn) { + void init_mpi(Caliper* c, Channel* chn) + { req_map.reserve(100); comm_map.reserve(100); @@ -120,7 +120,8 @@ struct MpiTracing::MpiTracingImpl // --- MPI object lookup // - Node* make_comm_entry(Caliper* c, MPI_Comm comm) { + Node* make_comm_entry(Caliper* c, MPI_Comm comm) + { int id = comm_id++; int size = 0; @@ -147,47 +148,56 @@ struct MpiTracing::MpiTracingImpl PMPI_Group_translate_ranks(comm_grp, size, ranks_in.data(), world_grp, ranks_out.data()); - node = - c->make_tree_entry(comm_list_attr, - Variant(CALI_TYPE_USR, ranks_out.data(), ranks_out.size()*sizeof(int)), - node); + node = c->make_tree_entry( + comm_list_attr, + Variant(CALI_TYPE_USR, ranks_out.data(), ranks_out.size() * sizeof(int)), + node + ); } return c->make_tree_entry(comm_attr, Variant(id), node); } - Node* lookup_comm(Caliper* c, MPI_Comm comm) { - std::lock_guard - g(comm_map_lock); + Node* lookup_comm(Caliper* c, MPI_Comm comm) + { + std::lock_guard g(comm_map_lock); auto it = comm_map.find(comm); if (it != comm_map.end()) return it->second; - Node* node = make_comm_entry(c, comm); + Node* node = make_comm_entry(c, comm); comm_map[comm] = node; return node; } - // --- point-to-point // - void push_send_event(Caliper* c, Channel* channel, int size, int dest, int tag, cali::Node* comm_node) { - const Entry data[] = { - { comm_node }, - { msg_dst_attr, Variant(dest) }, - { msg_tag_attr, Variant(tag) }, - { msg_size_attr, Variant(size) }, - { send_count_attr, Variant(1) } - }; + void push_send_event(Caliper* c, Channel* channel, int size, int dest, int tag, cali::Node* comm_node) + { + const Entry data[] = { { comm_node }, + { msg_dst_attr, Variant(dest) }, + { msg_tag_attr, Variant(tag) }, + { msg_size_attr, Variant(size) }, + { send_count_attr, Variant(1) } }; c->push_snapshot(channel, SnapshotView(5, data)); } - void handle_send_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm, MPI_Request* req) { + void handle_send_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int dest, + int tag, + MPI_Comm comm, + MPI_Request* req + ) + { RequestInfo info; info.op = RequestInfo::Send; @@ -201,34 +211,43 @@ struct MpiTracing::MpiTracingImpl PMPI_Type_size(type, &info.size); info.size *= count; - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); req_map[*req] = info; } - void push_recv_event(Caliper* c, Channel* channel, int src, int size, int tag, Node* comm_node) { - const Entry data[] = { - { comm_node }, - { msg_src_attr, Variant(src) }, - { msg_tag_attr, Variant(tag) }, - { msg_size_attr, Variant(size) }, - { recv_count_attr, Variant(1) } - }; + void push_recv_event(Caliper* c, Channel* channel, int src, int size, int tag, Node* comm_node) + { + const Entry data[] = { { comm_node }, + { msg_src_attr, Variant(src) }, + { msg_tag_attr, Variant(tag) }, + { msg_size_attr, Variant(size) }, + { recv_count_attr, Variant(1) } }; c->push_snapshot(channel, SnapshotView(5, data)); } - void handle_recv(Caliper* c, Channel* chn, MPI_Datatype type, MPI_Comm comm, MPI_Status* status) { - int size = 0; + void handle_recv(Caliper* c, Channel* chn, MPI_Datatype type, MPI_Comm comm, MPI_Status* status) + { + int size = 0; PMPI_Type_size(type, &size); int count = 0; PMPI_Get_count(status, type, &count); - push_recv_event(c, chn, status->MPI_SOURCE, size*count, status->MPI_TAG, lookup_comm(c, comm)); + push_recv_event(c, chn, status->MPI_SOURCE, size * count, status->MPI_TAG, lookup_comm(c, comm)); } - void handle_irecv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) { + void handle_irecv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ) + { RequestInfo info; info.op = RequestInfo::Recv; @@ -239,13 +258,22 @@ struct MpiTracing::MpiTracingImpl info.count = count; info.comm_node = lookup_comm(c, comm); - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); req_map[*req] = info; } - void handle_recv_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) { + void handle_recv_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ) + { RequestInfo info; info.op = RequestInfo::Recv; @@ -257,16 +285,15 @@ struct MpiTracing::MpiTracingImpl info.comm_node = lookup_comm(c, comm); info.size = 0; - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); req_map[*req] = info; } - void handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) { + void handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) + { for (int i = 0; i < nreq; ++i) { - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); auto it = req_map.find(reqs[i]); @@ -280,10 +307,10 @@ struct MpiTracing::MpiTracingImpl } } - void handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) { + void handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) + { for (int i = 0; i < nreq; ++i) { - std::lock_guard - g(req_map_lock); + std::lock_guard g(req_map_lock); auto it = req_map.find(reqs[i]); @@ -293,12 +320,12 @@ struct MpiTracing::MpiTracingImpl RequestInfo info = it->second; if (info.op == RequestInfo::Recv) { - int size = 0; + int size = 0; PMPI_Type_size(info.type, &size); int count = 0; - PMPI_Get_count(statuses+i, info.type, &count); + PMPI_Get_count(statuses + i, info.type, &count); - push_recv_event(c, chn, statuses[i].MPI_SOURCE, size*count, statuses[i].MPI_TAG, info.comm_node); + push_recv_event(c, chn, statuses[i].MPI_SOURCE, size * count, statuses[i].MPI_TAG, info.comm_node); } if (!info.is_persistent) @@ -306,9 +333,9 @@ struct MpiTracing::MpiTracingImpl } } - void request_free(MPI_Request* req) { - std::lock_guard - g(req_map_lock); + void request_free(MPI_Request* req) + { + std::lock_guard g(req_map_lock); req_map.erase(*req); } @@ -316,15 +343,14 @@ struct MpiTracing::MpiTracingImpl // --- collectives // - void push_coll_event(Caliper* c, Channel* channel, CollectiveType coll_type, int size, int root, Node* comm_node) { + void push_coll_event(Caliper* c, Channel* channel, CollectiveType coll_type, int size, int root, Node* comm_node) + { Node* node = c->make_tree_entry(coll_type_attr, Variant(static_cast(coll_type)), comm_node); - const Entry data[] = { - { node }, - { coll_count_attr, Variant(1) }, - { msg_size_attr, Variant(size) }, - { coll_root_attr, Variant(root) } - }; + const Entry data[] = { { node }, + { coll_count_attr, Variant(1) }, + { msg_size_attr, Variant(size) }, + { coll_root_attr, Variant(root) } }; int ne = 2; @@ -339,35 +365,28 @@ struct MpiTracing::MpiTracingImpl // --- constructor // - MpiTracingImpl() - : comm_id(0) - { } + MpiTracingImpl() : comm_id(0) {} }; - -MpiTracing::MpiTracing() - : mP(new MpiTracingImpl) -{ } +MpiTracing::MpiTracing() : mP(new MpiTracingImpl) +{} MpiTracing::~MpiTracing() { mP.reset(); } -void -MpiTracing::init(Caliper* c, Channel* chn) +void MpiTracing::init(Caliper* c, Channel* chn) { mP->init_attributes(c); } -void -MpiTracing::init_mpi(Caliper* c, Channel* chn) +void MpiTracing::init_mpi(Caliper* c, Channel* chn) { mP->init_mpi(c, chn); } -void -MpiTracing::handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm) +void MpiTracing::handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); @@ -376,50 +395,78 @@ MpiTracing::handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, mP->push_send_event(c, chn, size, dest, tag, mP->lookup_comm(c, comm)); } -void -MpiTracing::handle_send_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm, MPI_Request* req) +void MpiTracing::handle_send_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int dest, + int tag, + MPI_Comm comm, + MPI_Request* req +) { mP->handle_send_init(c, chn, count, type, dest, tag, comm, req); } -void -MpiTracing::handle_recv(Caliper* c, Channel* chn, int, MPI_Datatype type, int, int, MPI_Comm comm, MPI_Status* status) +void MpiTracing::handle_recv( + Caliper* c, + Channel* chn, + int, + MPI_Datatype type, + int, + int, + MPI_Comm comm, + MPI_Status* status +) { mP->handle_recv(c, chn, type, comm, status); } -void -MpiTracing::handle_irecv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) +void MpiTracing::handle_irecv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req +) { mP->handle_irecv(c, chn, count, type, src, tag, comm, req); } -void -MpiTracing::handle_recv_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req) +void MpiTracing::handle_recv_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req +) { mP->handle_recv_init(c, chn, count, type, src, tag, comm, req); } -void -MpiTracing::handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) +void MpiTracing::handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs) { mP->handle_start(c, chn, nreq, reqs); } -void -MpiTracing::handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) +void MpiTracing::handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses) { mP->handle_completion(c, chn, nreq, reqs, statuses); } -void -MpiTracing::request_free(Caliper*, Channel*, MPI_Request* req) +void MpiTracing::request_free(Caliper*, Channel*, MPI_Request* req) { mP->request_free(req); } -void -MpiTracing::handle_12n(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) +void MpiTracing::handle_12n(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); @@ -429,8 +476,7 @@ MpiTracing::handle_12n(Caliper* c, Channel* chn, int count, MPI_Datatype type, i mP->push_coll_event(c, chn, Coll_12N, (rank == root ? count : 0) * size, root, mP->lookup_comm(c, comm)); } -void -MpiTracing::handle_n21(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) +void MpiTracing::handle_n21(Caliper* c, Channel* chn, int count, MPI_Datatype type, int root, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); @@ -440,29 +486,25 @@ MpiTracing::handle_n21(Caliper* c, Channel* chn, int count, MPI_Datatype type, i mP->push_coll_event(c, chn, Coll_N21, (rank != root ? count : 0) * size, root, mP->lookup_comm(c, comm)); } -void -MpiTracing::handle_n2n(Caliper* c, Channel* chn, int count, MPI_Datatype type, MPI_Comm comm) +void MpiTracing::handle_n2n(Caliper* c, Channel* chn, int count, MPI_Datatype type, MPI_Comm comm) { int size = 0; PMPI_Type_size(type, &size); - mP->push_coll_event(c, chn, Coll_NxN, count*size, 0, mP->lookup_comm(c, comm)); + mP->push_coll_event(c, chn, Coll_NxN, count * size, 0, mP->lookup_comm(c, comm)); } -void -MpiTracing::handle_barrier(Caliper* c, Channel* chn, MPI_Comm comm) +void MpiTracing::handle_barrier(Caliper* c, Channel* chn, MPI_Comm comm) { mP->push_coll_event(c, chn, Coll_Barrier, 0, 0, mP->lookup_comm(c, comm)); } -void -MpiTracing::handle_init(Caliper* c, Channel* chn) +void MpiTracing::handle_init(Caliper* c, Channel* chn) { mP->push_coll_event(c, chn, Coll_Init, 0, 0, mP->lookup_comm(c, MPI_COMM_WORLD)); } -void -MpiTracing::handle_finalize(Caliper* c, Channel* chn) +void MpiTracing::handle_finalize(Caliper* c, Channel* chn) { mP->push_coll_event(c, chn, Coll_Finalize, 0, 0, mP->lookup_comm(c, MPI_COMM_WORLD)); } diff --git a/src/services/mpiwrap/MpiTracing.h b/src/services/mpiwrap/MpiTracing.h index 7fb179cd7..19866c7b8 100644 --- a/src/services/mpiwrap/MpiTracing.h +++ b/src/services/mpiwrap/MpiTracing.h @@ -23,9 +23,7 @@ class MpiTracing public: - enum CollectiveType { - Unknown, Coll_Barrier, Coll_NxN, Coll_12N, Coll_N21, Coll_Init, Coll_Finalize - }; + enum CollectiveType { Unknown, Coll_Barrier, Coll_NxN, Coll_12N, Coll_N21, Coll_Init, Coll_Finalize }; MpiTracing(); @@ -37,11 +35,47 @@ class MpiTracing // --- point-to-point void handle_send(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm); - void handle_send_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int dest, int tag, MPI_Comm comm, MPI_Request* req); - - void handle_recv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Status* status); - void handle_irecv(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req); - void handle_recv_init(Caliper* c, Channel* chn, int count, MPI_Datatype type, int src, int tag, MPI_Comm comm, MPI_Request* req); + void handle_send_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int dest, + int tag, + MPI_Comm comm, + MPI_Request* req + ); + + void handle_recv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Status* status + ); + void handle_irecv( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ); + void handle_recv_init( + Caliper* c, + Channel* chn, + int count, + MPI_Datatype type, + int src, + int tag, + MPI_Comm comm, + MPI_Request* req + ); void handle_start(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs); void handle_completion(Caliper* c, Channel* chn, int nreq, MPI_Request* reqs, MPI_Status* statuses); @@ -58,4 +92,4 @@ class MpiTracing void handle_finalize(Caliper* c, Channel* chn); }; -} +} // namespace cali diff --git a/src/services/mpiwrap/MpiWrap.cpp b/src/services/mpiwrap/MpiWrap.cpp index 2ec122f77..97ae50b41 100644 --- a/src/services/mpiwrap/MpiWrap.cpp +++ b/src/services/mpiwrap/MpiWrap.cpp @@ -26,7 +26,7 @@ extern void mpiwrap_init(Caliper* c, Channel* chn, cali::ConfigSet& cfg); extern Attribute subscription_event_attr; -} +} // namespace cali namespace { @@ -64,21 +64,22 @@ void mpi_register(Caliper* c, Channel* chn) Variant v_true(true); if (!mpifn_attr) - mpifn_attr = - c->create_attribute("mpi.function", CALI_TYPE_STRING, - CALI_ATTR_NESTED, - 1, &subscription_event_attr, &v_true); + mpifn_attr = c->create_attribute( + "mpi.function", + CALI_TYPE_STRING, + CALI_ATTR_NESTED, + 1, + &subscription_event_attr, + &v_true + ); if (!mpirank_attr) - mpirank_attr = - c->create_attribute("mpi.rank", CALI_TYPE_INT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE); + mpirank_attr = c->create_attribute( + "mpi.rank", + CALI_TYPE_INT, + CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE + ); if (!mpisize_attr) - mpisize_attr = - c->create_attribute("mpi.world.size", CALI_TYPE_INT, - CALI_ATTR_GLOBAL | - CALI_ATTR_SKIP_EVENTS); + mpisize_attr = c->create_attribute("mpi.world.size", CALI_TYPE_INT, CALI_ATTR_GLOBAL | CALI_ATTR_SKIP_EVENTS); ConfigSet cfg = services::init_config_from_spec(chn->config(), mpi_service_spec); @@ -89,7 +90,6 @@ void mpi_register(Caliper* c, Channel* chn) } // anonymous namespace - namespace cali { diff --git a/src/services/nvtx/Nvtx.cpp b/src/services/nvtx/Nvtx.cpp index 5658a9e05..5f4285eae 100644 --- a/src/services/nvtx/Nvtx.cpp +++ b/src/services/nvtx/Nvtx.cpp @@ -27,20 +27,21 @@ class NvtxBinding : public AnnotationBinding static const uint32_t s_colors[]; static const int s_num_colors = 14; - Attribute m_color_attr; - std::atomic m_color_id; + Attribute m_color_attr; + std::atomic m_color_id; std::map m_domain_map; - std::mutex m_domain_mutex; + std::mutex m_domain_mutex; - bool m_cycle_colors; + bool m_cycle_colors; - std::unordered_map m_color_map; - std::mutex m_color_map_mutex; + std::unordered_map m_color_map; + std::mutex m_color_map_mutex; - uint32_t get_attribute_color(const Attribute& attr) { - cali_id_t color_attr_id = m_color_attr.id(); - const Node* node = nullptr; + uint32_t get_attribute_color(const Attribute& attr) + { + cali_id_t color_attr_id = m_color_attr.id(); + const Node* node = nullptr; for (node = attr.node()->first_child(); node; node = node->next_sibling()) if (node->attribute() == color_attr_id) @@ -49,13 +50,13 @@ class NvtxBinding : public AnnotationBinding return node ? static_cast(node->data().to_uint()) : s_colors[0]; } - uint32_t get_value_color(const Variant& value) { + uint32_t get_value_color(const Variant& value) + { std::string valstr = value.to_string(); - uint32_t color = s_colors[0]; + uint32_t color = s_colors[0]; { - std::lock_guard - g(m_color_map_mutex); + std::lock_guard g(m_color_map_mutex); auto it = m_color_map.find(valstr); @@ -69,7 +70,8 @@ class NvtxBinding : public AnnotationBinding return color; } - uint32_t get_color(const Attribute& attr, const Variant& value) { + uint32_t get_color(const Attribute& attr, const Variant& value) + { if (m_cycle_colors) return get_value_color(value); else @@ -78,21 +80,20 @@ class NvtxBinding : public AnnotationBinding public: - void initialize(Caliper* c, Channel* chn) { + void initialize(Caliper* c, Channel* chn) + { std::string name = "nvtx.color#"; name.append(std::to_string(chn->id())); - m_color_attr = - c->create_attribute("nvtx.color", CALI_TYPE_UINT, - CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); + m_color_attr = c->create_attribute("nvtx.color", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); - m_cycle_colors = - chn->config().init("nvtx", s_configdata).get("cycle_colors").to_bool(); + m_cycle_colors = chn->config().init("nvtx", s_configdata).get("cycle_colors").to_bool(); } const char* service_tag() const { return "nvtx"; } - void on_mark_attribute(Caliper* c, Channel*, const Attribute& attr) { + void on_mark_attribute(Caliper* c, Channel*, const Attribute& attr) + { if (m_cycle_colors) return; @@ -105,14 +106,15 @@ class NvtxBinding : public AnnotationBinding c->make_tree_entry(m_color_attr, v_color, attr.node()); } - void on_begin(Caliper*, Channel*, const Attribute &attr, const Variant& value) { + void on_begin(Caliper*, Channel*, const Attribute& attr, const Variant& value) + { nvtxEventAttributes_t eventAttrib = { 0 }; - eventAttrib.version = NVTX_VERSION; - eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE; - eventAttrib.colorType = NVTX_COLOR_ARGB; - eventAttrib.color = get_color(attr, value); - eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII; + eventAttrib.version = NVTX_VERSION; + eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE; + eventAttrib.colorType = NVTX_COLOR_ARGB; + eventAttrib.color = get_color(attr, value); + eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII; eventAttrib.message.ascii = (value.type() == CALI_TYPE_STRING ? static_cast(value.data()) : value.to_string().c_str()); @@ -125,8 +127,7 @@ class NvtxBinding : public AnnotationBinding nvtxDomainHandle_t domain; { - std::lock_guard - g(m_domain_mutex); + std::lock_guard g(m_domain_mutex); auto it = m_domain_map.find(attr.id()); @@ -142,22 +143,21 @@ class NvtxBinding : public AnnotationBinding } } - void on_end(Caliper*, Channel*, const Attribute& attr, const Variant& value) { + void on_end(Caliper*, Channel*, const Attribute& attr, const Variant& value) + { if (attr.is_nested()) { nvtxRangePop(); } else { nvtxDomainHandle_t domain; { - std::lock_guard - g(m_domain_mutex); + std::lock_guard g(m_domain_mutex); auto it = m_domain_map.find(attr.id()); if (it == m_domain_map.end()) { - Log(0).stream() << "nvtx: on_end(): error: domain for attribute " - << attr.name() - << " not found!" << std::endl; + Log(0).stream() << "nvtx: on_end(): error: domain for attribute " << attr.name() << " not found!" + << std::endl; return; } @@ -169,25 +169,20 @@ class NvtxBinding : public AnnotationBinding } }; -const uint32_t NvtxBinding::s_colors[] = { - 0x0000cc00, 0x000000cc, 0x00cccc00, 0x00cc00cc, - 0x0000cccc, 0x00cc0000, 0x00cccccc, - 0x00008800, 0x00000088, 0x00888800, 0x00880088, - 0x00008888, 0x00880000, 0x00888888 -}; - +const uint32_t NvtxBinding::s_colors[] = { 0x0000cc00, 0x000000cc, 0x00cccc00, 0x00cc00cc, 0x0000cccc, + 0x00cc0000, 0x00cccccc, 0x00008800, 0x00000088, 0x00888800, + 0x00880088, 0x00008888, 0x00880000, 0x00888888 }; -const ConfigSet::Entry NvtxBinding::s_configdata[] = { - { "cycle_colors", CALI_TYPE_BOOL, "true", - "Use a different color for each annotation entry", - "Use a different color for each annotation entry" - }, - ConfigSet::Terminator -}; +const ConfigSet::Entry NvtxBinding::s_configdata[] = { { "cycle_colors", + CALI_TYPE_BOOL, + "true", + "Use a different color for each annotation entry", + "Use a different color for each annotation entry" }, + ConfigSet::Terminator }; CaliperService nvtx_service { "nvtx", &AnnotationBinding::make_binding }; -// Keep deprecated "nvprof" alias for nvtx service +// Keep deprecated "nvprof" alias for nvtx service CaliperService nvprof_service { "nvprof", &AnnotationBinding::make_binding }; } // namespace cali diff --git a/src/services/ompt/OmptService.cpp b/src/services/ompt/OmptService.cpp index 062063ac1..c95cca28a 100644 --- a/src/services/ompt/OmptService.cpp +++ b/src/services/ompt/OmptService.cpp @@ -18,19 +18,19 @@ namespace { struct OmptAPI { - ompt_set_callback_t set_callback { nullptr }; - ompt_get_state_t get_state { nullptr }; - ompt_enumerate_states_t enumerate_states { nullptr }; - ompt_get_proc_id_t get_proc_id { nullptr }; - ompt_finalize_tool_t finalize_tool { nullptr }; - - bool - init(ompt_function_lookup_t lookup) { - set_callback = (ompt_set_callback_t) (*lookup)("ompt_set_callback"); - get_state = (ompt_get_state_t) (*lookup)("ompt_get_state"); + ompt_set_callback_t set_callback { nullptr }; + ompt_get_state_t get_state { nullptr }; + ompt_enumerate_states_t enumerate_states { nullptr }; + ompt_get_proc_id_t get_proc_id { nullptr }; + ompt_finalize_tool_t finalize_tool { nullptr }; + + bool init(ompt_function_lookup_t lookup) + { + set_callback = (ompt_set_callback_t) (*lookup)("ompt_set_callback"); + get_state = (ompt_get_state_t) (*lookup)("ompt_get_state"); enumerate_states = (ompt_enumerate_states_t) (*lookup)("ompt_enumerate_states"); - get_proc_id = (ompt_get_proc_id_t) (*lookup)("ompt_get_proc_id"); - finalize_tool = (ompt_finalize_tool_t) (*lookup)("ompt_finalize_tool"); + get_proc_id = (ompt_get_proc_id_t) (*lookup)("ompt_get_proc_id"); + finalize_tool = (ompt_finalize_tool_t) (*lookup)("ompt_finalize_tool"); if (!set_callback || !get_state || !enumerate_states || !get_proc_id || !finalize_tool) return false; @@ -53,16 +53,16 @@ struct OmptAPI { } } api; -Attribute region_attr; -Attribute sync_attr; -Attribute work_attr; -Attribute thread_type_attr; -Attribute state_attr; -Attribute proc_id_attr; -Attribute thread_id_attr; -Attribute num_threads_attr; +Attribute region_attr; +Attribute sync_attr; +Attribute work_attr; +Attribute thread_type_attr; +Attribute state_attr; +Attribute proc_id_attr; +Attribute thread_id_attr; +Attribute num_threads_attr; -unsigned int num_skipped { 0 }; +unsigned int num_skipped { 0 }; // // --- The OMPT callbacks @@ -136,7 +136,14 @@ void cb_parallel_end(ompt_data_t*, ompt_data_t*, int, const void*) c.end(region_attr); } -void cb_implicit_task(ompt_scope_endpoint_t endpoint, ompt_data_t*, ompt_data_t*, unsigned int par, unsigned int index, int flags) +void cb_implicit_task( + ompt_scope_endpoint_t endpoint, + ompt_data_t*, + ompt_data_t*, + unsigned int par, + unsigned int index, + int flags +) { // ignore the initial task if (flags & ompt_task_initial) @@ -160,17 +167,9 @@ void cb_implicit_task(ompt_scope_endpoint_t endpoint, ompt_data_t*, ompt_data_t* void cb_work(int wstype, ompt_scope_endpoint_t endpoint, ompt_data_t*, ompt_data_t*, uint64_t, const void*) { - const char* work_region_names[] = { - "UNKNOWN", - "loop", - "sections", - "single_executor", - "single_other", - "workshare", - "distribute", - "taskloop", - "scope" - }; + const char* work_region_names[] = { "UNKNOWN", "loop", "sections", + "single_executor", "single_other", "workshare", + "distribute", "taskloop", "scope" }; const char* name = "UNKNOWN"; @@ -194,16 +193,8 @@ void cb_work(int wstype, ompt_scope_endpoint_t endpoint, ompt_data_t*, ompt_data void cb_sync_region(int kind, ompt_scope_endpoint_t endpoint, ompt_data_t*, ompt_data_t*, const void*) { const char* sync_region_names[] = { - "UNKNOWN", - "barrier", - "barrier_implicit", - "barrier_explicit", - "barrier_implementation", - "taskwait", - "taskgroup", - "reduction", - "barrier_implicit_workshare", - "barrier_implicit_parallel", + "UNKNOWN", "barrier", "barrier_implicit", "barrier_explicit", "barrier_implementation", + "taskwait", "taskgroup", "reduction", "barrier_implicit_workshare", "barrier_implicit_parallel", "barrier_teams" }; @@ -229,21 +220,18 @@ void cb_sync_region(int kind, ompt_scope_endpoint_t endpoint, ompt_data_t*, ompt // --- OMPT management // - void setup_ompt_callbacks() { const struct callback_info { ompt_callbacks_t cb; ompt_callback_t fn; - } callbacks[] = { - { ompt_callback_thread_begin, reinterpret_cast(cb_thread_begin) }, - { ompt_callback_thread_end, reinterpret_cast(cb_thread_end) }, - { ompt_callback_parallel_begin, reinterpret_cast(cb_parallel_begin) }, - { ompt_callback_parallel_end, reinterpret_cast(cb_parallel_end) }, - { ompt_callback_work, reinterpret_cast(cb_work) }, - { ompt_callback_sync_region, reinterpret_cast(cb_sync_region) }, - { ompt_callback_implicit_task, reinterpret_cast(cb_implicit_task) } - }; + } callbacks[] = { { ompt_callback_thread_begin, reinterpret_cast(cb_thread_begin) }, + { ompt_callback_thread_end, reinterpret_cast(cb_thread_end) }, + { ompt_callback_parallel_begin, reinterpret_cast(cb_parallel_begin) }, + { ompt_callback_parallel_end, reinterpret_cast(cb_parallel_end) }, + { ompt_callback_work, reinterpret_cast(cb_work) }, + { ompt_callback_sync_region, reinterpret_cast(cb_sync_region) }, + { ompt_callback_implicit_task, reinterpret_cast(cb_implicit_task) } }; for (auto info : callbacks) (*(api.set_callback))(info.cb, info.fn); @@ -272,16 +260,13 @@ void finalize_ompt(ompt_data_t* tool_data) ompt_start_tool_result_t start_tool_result { initialize_ompt, finalize_ompt, { 0 } }; - // // --- Caliper management // void post_init_cb(Caliper* c, Channel* channel) { - const Attribute sub_attrs[] = { - region_attr, thread_type_attr, sync_attr, work_attr, thread_id_attr - }; + const Attribute sub_attrs[] = { region_attr, thread_type_attr, sync_attr, work_attr, thread_id_attr }; for (const Attribute& attr : sub_attrs) channel->events().subscribe_attribute(c, channel, attr); @@ -290,49 +275,44 @@ void post_init_cb(Caliper* c, Channel* channel) void create_attributes(Caliper* c) { Attribute subscription_attr = c->get_attribute("subscription_event"); - Variant v_true(true); + Variant v_true(true); region_attr = - c->create_attribute("omp.parallel", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD, - 1, &subscription_attr, &v_true); - thread_type_attr = - c->create_attribute("omp.thread.type", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_UNALIGNED, - 1, &subscription_attr, &v_true); + c->create_attribute("omp.parallel", CALI_TYPE_UINT, CALI_ATTR_SCOPE_THREAD, 1, &subscription_attr, &v_true); + thread_type_attr = c->create_attribute( + "omp.thread.type", + CALI_TYPE_STRING, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED, + 1, + &subscription_attr, + &v_true + ); sync_attr = - c->create_attribute("omp.sync", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD, - 1, &subscription_attr, &v_true); + c->create_attribute("omp.sync", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, 1, &subscription_attr, &v_true); work_attr = - c->create_attribute("omp.work", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD, - 1, &subscription_attr, &v_true); - state_attr = - c->create_attribute("omp.state", CALI_TYPE_STRING, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS); - proc_id_attr = - c->create_attribute("omp.proc.id", CALI_TYPE_INT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_UNALIGNED | - CALI_ATTR_SKIP_EVENTS); - thread_id_attr = - c->create_attribute("omp.thread.id", CALI_TYPE_INT, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_UNALIGNED | - CALI_ATTR_SCOPE_THREAD); - num_threads_attr = - c->create_attribute("omp.num.threads", CALI_TYPE_INT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_UNALIGNED | - CALI_ATTR_SKIP_EVENTS); + c->create_attribute("omp.work", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD, 1, &subscription_attr, &v_true); + state_attr = c->create_attribute("omp.state", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS); + proc_id_attr = c->create_attribute( + "omp.proc.id", + CALI_TYPE_INT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS + ); + thread_id_attr = c->create_attribute( + "omp.thread.id", + CALI_TYPE_INT, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_UNALIGNED | CALI_ATTR_SCOPE_THREAD + ); + num_threads_attr = c->create_attribute( + "omp.num.threads", + CALI_TYPE_INT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS + ); } int num_ompt_channels = 0; -void pre_finish_cb(Caliper*, Channel* channel) { +void pre_finish_cb(Caliper*, Channel* channel) +{ if (--num_ompt_channels == 0) { Log(1).stream() << channel->name() << ": Finalizing OMPT" << std::endl; @@ -361,15 +341,17 @@ void register_ompt_service(Caliper* c, Channel* channel) channel->events().post_init_evt.connect(post_init_cb); channel->events().pre_finish_evt.connect(pre_finish_cb); - Log(1).stream() << channel->name() << ": " << "Registered OMPT service" << std::endl; + Log(1).stream() << channel->name() << ": " + << "Registered OMPT service" << std::endl; } -} // namespace [anonymous] +} // namespace -extern "C" { +extern "C" +{ -ompt_start_tool_result_t* -ompt_start_tool(unsigned omp_version, const char* runtime_version) { +ompt_start_tool_result_t* ompt_start_tool(unsigned omp_version, const char* runtime_version) +{ bool use_ompt = (::num_ompt_channels > 0); const char* optstr = std::getenv("CALI_USE_OMPT"); @@ -379,12 +361,10 @@ ompt_start_tool(unsigned omp_version, const char* runtime_version) { if (Log::verbosity() >= 2) Log(2).stream() << "OMPT is available. Using " << runtime_version - << ". OMPT requested: " - << (use_ompt ? "Yes" : "No") << std::endl; + << ". OMPT requested: " << (use_ompt ? "Yes" : "No") << std::endl; return use_ompt ? &::start_tool_result : nullptr; } - } namespace cali diff --git a/src/services/papi/Papi.cpp b/src/services/papi/Papi.cpp index 52b20c458..959e5fd89 100644 --- a/src/services/papi/Papi.cpp +++ b/src/services/papi/Papi.cpp @@ -35,46 +35,43 @@ void print_papi_error(const char* function, int code) Log(0).stream() << "papi: Error: " << function << ": " << PAPI_strerror(code) << std::endl; } - class PapiService { struct event_group_t { - std::vector codes; + std::vector codes; std::vector attrs; }; - typedef std::map< int, std::shared_ptr > - eventset_map_t; + typedef std::map> eventset_map_t; struct ThreadInfo { eventset_map_t eventsets; - ThreadInfo* next; - ThreadInfo* prev; + ThreadInfo* next; + ThreadInfo* prev; - void unlink() { + void unlink() + { if (next) next->prev = prev; if (prev) prev->next = next; } - ThreadInfo() - : next(nullptr), prev(nullptr) - { } + ThreadInfo() : next(nullptr), prev(nullptr) {} }; Attribute m_thread_attr; - bool m_enable_multiplex; + bool m_enable_multiplex; - unsigned m_num_eventsets; - unsigned m_num_event_mismatch; - unsigned m_num_failed_acquire; - unsigned m_num_failed_read; - unsigned m_num_failed_reset; - unsigned m_num_failed_start; - unsigned m_num_threads; + unsigned m_num_eventsets; + unsigned m_num_event_mismatch; + unsigned m_num_failed_acquire; + unsigned m_num_failed_read; + unsigned m_num_failed_reset; + unsigned m_num_failed_start; + unsigned m_num_threads; // PAPI component id -> event group info map for constructing the per-thread PAPI EventSets eventset_map_t m_event_groups; @@ -84,15 +81,15 @@ class PapiService static int s_num_instances; - bool setup_event_info(Caliper* c, const std::vector& eventlist) { + bool setup_event_info(Caliper* c, const std::vector& eventlist) + { m_event_groups.clear(); int count = 0; - for (auto &name : eventlist) { + for (auto& name : eventlist) { if (count >= MAX_COUNTERS) { - Log(0).stream() << "papi: Maximum number of counters reached, skipping " - << name << std::endl; + Log(0).stream() << "papi: Maximum number of counters reached, skipping " << name << std::endl; continue; } @@ -113,19 +110,20 @@ class PapiService continue; } - Attribute attr = - c->create_attribute(std::string("papi.")+name, CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); + Attribute attr = c->create_attribute( + std::string("papi.") + name, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); int component = PAPI_get_event_component(code); // find or create eventset info for this component auto it = m_event_groups.find(component); if (it == m_event_groups.end()) { - auto p = m_event_groups.emplace(std::make_pair(component, std::shared_ptr(new event_group_t))); + auto p = + m_event_groups.emplace(std::make_pair(component, std::shared_ptr(new event_group_t)) + ); it = p.first; } @@ -137,30 +135,26 @@ class PapiService bool all_found = (static_cast(eventlist.size()) == count); - Log(2).stream() << "papi: Found " << count - << " event codes for " << m_event_groups.size() - << " PAPI component(s)" - << std::endl; + Log(2).stream() << "papi: Found " << count << " event codes for " << m_event_groups.size() + << " PAPI component(s)" << std::endl; if (!all_found) - Log(0).stream() << "papi: Unable to process all requested counters" - << std::endl; + Log(0).stream() << "papi: Unable to process all requested counters" << std::endl; return all_found; } - bool setup_thread_eventsets(Caliper* c) { - std::map< int, std::shared_ptr > eventsets; - bool ok = true; + bool setup_thread_eventsets(Caliper* c) + { + std::map> eventsets; + bool ok = true; - for (auto &p : m_event_groups) { + for (auto& p : m_event_groups) { const PAPI_component_info_t* cpi = PAPI_get_component_info(p.first); if (Log::verbosity() >= 2) { - Log(2).stream() << "papi: Creating eventset with " << p.second->codes.size() - << " events for component " << p.first - << " (" << (cpi ? cpi->name : "UKNOWN COMPONENT") << ")" - << std::endl; + Log(2).stream() << "papi: Creating eventset with " << p.second->codes.size() << " events for component " + << p.first << " (" << (cpi ? cpi->name : "UKNOWN COMPONENT") << ")" << std::endl; } int eventset = PAPI_NULL; @@ -176,9 +170,8 @@ class PapiService if (cpi && (num > 4 /* magic number for Intel counter support :-( */ || m_enable_multiplex)) { if (Log::verbosity() >= 2) - Log(2).stream() << "papi: Initializing multiplex support for component " - << p.first << " (" << cpi->name << ")" - << std::endl; + Log(2).stream() << "papi: Initializing multiplex support for component " << p.first << " (" + << cpi->name << ")" << std::endl; ret = PAPI_assign_eventset_component(eventset, p.first); if (ret != PAPI_OK) @@ -196,11 +189,8 @@ class PapiService ok = false; break; } else if (ret > 0 && ret < num) { - Log(0).stream() << "papi: Added " << ret << " of " << num - << " events for component " << p.first - << " (" << (cpi ? cpi->name : "UKNOWN COMPONENT") - << "), skipping " << (num-ret) - << std::endl; + Log(0).stream() << "papi: Added " << ret << " of " << num << " events for component " << p.first << " (" + << (cpi ? cpi->name : "UKNOWN COMPONENT") << "), skipping " << (num - ret) << std::endl; } eventsets.emplace(std::make_pair(eventset, p.second)); @@ -208,16 +198,15 @@ class PapiService if (ok) { ThreadInfo* td = new ThreadInfo; - td->eventsets = std::move(eventsets); + td->eventsets = std::move(eventsets); { - std::lock_guard - g(m_thread_lock); + std::lock_guard g(m_thread_lock); if (m_thread_list) m_thread_list->prev = td; - td->next = m_thread_list; + td->next = m_thread_list; m_thread_list = td; m_num_eventsets += static_cast(td->eventsets.size()); @@ -226,7 +215,7 @@ class PapiService c->set(m_thread_attr, Variant(cali_make_variant_from_ptr(td))); } else { - for (const auto &p: eventsets) { + for (const auto& p : eventsets) { int eventset = p.first; PAPI_destroy_eventset(&eventset); } @@ -235,8 +224,8 @@ class PapiService return ok; } - ThreadInfo* - get_thread_info(Caliper* c) { + ThreadInfo* get_thread_info(Caliper* c) + { Entry e = c->get(m_thread_attr); if (e.empty()) @@ -245,7 +234,8 @@ class PapiService return static_cast(e.value().get_ptr()); } - void read_events(int eventset, const event_group_t& grp, SnapshotBuilder& rec) { + void read_events(int eventset, const event_group_t& grp, SnapshotBuilder& rec) + { long long values[MAX_COUNTERS]; int ret = PAPI_read(eventset, values); @@ -269,8 +259,8 @@ class PapiService rec.append(grp.attrs[i], Variant(cali_make_variant_from_uint(values[i]))); } - bool - start_thread_counting(Caliper* c) { + bool start_thread_counting(Caliper* c) + { ThreadInfo* td = get_thread_info(c); if (!td) { @@ -278,7 +268,7 @@ class PapiService return false; } - for (const auto &p : td->eventsets) { + for (const auto& p : td->eventsets) { int ret = PAPI_start(p.first); if (ret != PAPI_OK) { ++m_num_failed_start; @@ -289,9 +279,10 @@ class PapiService return true; } - void finish_eventset(int eventset) { + void finish_eventset(int eventset) + { int state = PAPI_NULL; - int ret = PAPI_state(eventset, &state); + int ret = PAPI_state(eventset, &state); if (ret != PAPI_OK) { print_papi_error("PAPI_state()", ret); return; @@ -316,8 +307,8 @@ class PapiService } } - void - finish_thread_eventsets(Caliper* c) { + void finish_thread_eventsets(Caliper* c) + { ThreadInfo* td = get_thread_info(c); if (!td) { @@ -325,14 +316,13 @@ class PapiService return; } - for (const auto &p : td->eventsets) + for (const auto& p : td->eventsets) finish_eventset(p.first); c->end(m_thread_attr); { - std::lock_guard - g(m_thread_lock); + std::lock_guard g(m_thread_lock); ThreadInfo* tmp = td->next; td->unlink(); @@ -344,8 +334,8 @@ class PapiService } } - void - snapshot(Caliper* c, SnapshotBuilder& rec) { + void snapshot(Caliper* c, SnapshotBuilder& rec) + { ThreadInfo* td = get_thread_info(c); if (!td) @@ -355,32 +345,23 @@ class PapiService read_events(p.first, *(p.second), rec); } - void - finish(Caliper* c, Channel* channel) { + void finish(Caliper* c, Channel* channel) + { Log(1).stream() << channel->name() << ": papi: Finishing" << std::endl; finish_thread_eventsets(c); unsigned errors = - m_num_event_mismatch + - m_num_failed_acquire + - m_num_failed_read + - m_num_failed_reset + - m_num_failed_start; + m_num_event_mismatch + m_num_failed_acquire + m_num_failed_read + m_num_failed_reset + m_num_failed_start; if (errors > 0) - Log(0).stream() << channel->name() << ": papi: " - << m_num_failed_acquire << " failed thread data accesses, " - << m_num_failed_read << " failed reads, " - << m_num_failed_reset << " failed resets, " - << m_num_failed_start << " failed starts, " - << m_num_event_mismatch << " event count mismatches." - << std::endl; + Log(0).stream() << channel->name() << ": papi: " << m_num_failed_acquire << " failed thread data accesses, " + << m_num_failed_read << " failed reads, " << m_num_failed_reset << " failed resets, " + << m_num_failed_start << " failed starts, " << m_num_event_mismatch + << " event count mismatches." << std::endl; - Log(1).stream() << channel->name() << ": papi: Created " - << m_num_eventsets << " PAPI event set(s) on " - << m_num_threads << " thread(s)." - << std::endl; + Log(1).stream() << channel->name() << ": papi: Created " << m_num_eventsets << " PAPI event set(s) on " + << m_num_threads << " thread(s)." << std::endl; } PapiService(Caliper* c, Channel* channel) @@ -393,25 +374,24 @@ class PapiService m_num_failed_start(0), m_num_threads(0), m_thread_list(nullptr) - { - m_thread_attr = - c->create_attribute(std::string("papi.data.")+std::to_string(channel->id()), - CALI_TYPE_PTR, CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_HIDDEN | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS); - } - + { + m_thread_attr = c->create_attribute( + std::string("papi.data.") + std::to_string(channel->id()), + CALI_TYPE_PTR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_HIDDEN | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ); + } - static bool init_papi_library() { + static bool init_papi_library() + { if (PAPI_is_initialized() == PAPI_THREAD_LEVEL_INITED) return true; int ret = PAPI_library_init(PAPI_VER_CURRENT); if (ret != PAPI_VER_CURRENT && ret > 0) { - Log(0).stream() << "papi: PAPI version mismatch: found " - << ret << ", expected " << PAPI_VER_CURRENT << std::endl; + Log(0).stream() << "papi: PAPI version mismatch: found " << ret << ", expected " << PAPI_VER_CURRENT + << std::endl; return false; } @@ -426,19 +406,20 @@ class PapiService return true; } - static void finish_papi_library() { + static void finish_papi_library() + { if (--s_num_instances == 0) { Log(1).stream() << "papi: Shutdown" << std::endl; PAPI_shutdown(); } } - public: - ~PapiService() { + ~PapiService() + { // clear all trace buffers - for (ThreadInfo* td = m_thread_list, *tmp = nullptr; td; td = tmp) { + for (ThreadInfo *td = m_thread_list, *tmp = nullptr; td; td = tmp) { tmp = td->next; delete td; } @@ -446,20 +427,18 @@ class PapiService static const char* s_spec; - static void register_papi(Caliper* c, Channel* channel) { - auto cfg = services::init_config_from_spec(channel->config(), s_spec); + static void register_papi(Caliper* c, Channel* channel) + { + auto cfg = services::init_config_from_spec(channel->config(), s_spec); auto eventlist = cfg.get("counters").to_stringlist(","); if (eventlist.empty()) { - Log(1).stream() << channel->name() - << ": papi: No counters specified, dropping papi service" - << std::endl; + Log(1).stream() << channel->name() << ": papi: No counters specified, dropping papi service" << std::endl; return; } if (!init_papi_library()) { - Log(0).stream() << channel->name() - << ": papi: PAPI library not initialized, dropping papi service" + Log(0).stream() << channel->name() << ": papi: PAPI library not initialized, dropping papi service" << std::endl; return; } @@ -470,37 +449,30 @@ class PapiService instance->m_enable_multiplex = cfg.get("enable_multiplexing").to_bool(); if (!(instance->setup_event_info(c, eventlist) && instance->setup_thread_eventsets(c))) { - Log(0).stream() << channel->name() - << ": papi: Failed to initialize event sets, dropping papi service" + Log(0).stream() << channel->name() << ": papi: Failed to initialize event sets, dropping papi service" << std::endl; finish_papi_library(); return; } - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel*){ + channel->events().post_init_evt.connect([instance](Caliper* c, Channel*) { instance->start_thread_counting(c); } + ); + channel->events().create_thread_evt.connect([instance](Caliper* c, Channel*) { + if (instance->setup_thread_eventsets(c)) instance->start_thread_counting(c); - }); - channel->events().create_thread_evt.connect( - [instance](Caliper* c, Channel*){ - if (instance->setup_thread_eventsets(c)) - instance->start_thread_counting(c); - }); - channel->events().release_thread_evt.connect( - [instance](Caliper* c, Channel*){ - instance->finish_thread_eventsets(c); - }); - channel->events().snapshot.connect( - [instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec){ - instance->snapshot(c, rec); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish(c, channel); - finish_papi_library(); - delete instance; - }); + }); + channel->events().release_thread_evt.connect([instance](Caliper* c, Channel*) { + instance->finish_thread_eventsets(c); + }); + channel->events().snapshot.connect([instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec) { + instance->snapshot(c, rec); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish(c, channel); + finish_papi_library(); + delete instance; + }); Log(1).stream() << channel->name() << ": Registered papi service" << std::endl; } @@ -528,7 +500,6 @@ const char* PapiService::s_spec = R"json( } // namespace - namespace cali { diff --git a/src/services/pcp/Pcp.cpp b/src/services/pcp/Pcp.cpp index 1ae24ce7d..92ca0f045 100644 --- a/src/services/pcp/Pcp.cpp +++ b/src/services/pcp/Pcp.cpp @@ -22,7 +22,8 @@ using namespace cali; namespace { -class PcpService { +class PcpService +{ static int s_pcp_context; static int s_num_instances; @@ -36,19 +37,20 @@ class PcpService { std::vector m_metric_info; std::vector m_metric_list; - std::vector m_prev_value; // last snapshot's value to compute diffs + std::vector m_prev_value; // last snapshot's value to compute diffs double m_prev_timestamp { 0 }; - unsigned m_num_lookups { 0 }; + unsigned m_num_lookups { 0 }; unsigned m_num_failed_lookups { 0 }; - unsigned m_num_failed_values { 0 }; + unsigned m_num_failed_values { 0 }; Attribute m_timestamp_sec_attr; Attribute m_timestamp_attr; Attribute m_time_duration_attr; - void snapshot(Caliper*, SnapshotBuilder& rec) { + void snapshot(Caliper*, SnapshotBuilder& rec) + { pmResult* res = nullptr; int status = pmFetch(static_cast(m_metric_list.size()), m_metric_list.data(), &res); @@ -69,11 +71,13 @@ class PcpService { for (int j = 0; j < res->vset[i]->numval; ++j) { pmAtomValue val; - status = pmExtractValue(res->vset[i]->valfmt, - &res->vset[i]->vlist[j], - m_metric_info[i].pmdesc.type, - &val, - PM_TYPE_DOUBLE); + status = pmExtractValue( + res->vset[i]->valfmt, + &res->vset[i]->vlist[j], + m_metric_info[i].pmdesc.type, + &val, + PM_TYPE_DOUBLE + ); if (status < 0) continue; total += val.d; @@ -95,7 +99,7 @@ class PcpService { double timestamp = res->timestamp.tv_sec + (res->timestamp.tv_usec * 1e-6); rec.append(m_timestamp_sec_attr, cali_make_variant_from_uint(res->timestamp.tv_sec)); - rec.append(m_timestamp_attr, Variant(timestamp)); + rec.append(m_timestamp_attr, Variant(timestamp)); rec.append(m_time_duration_attr, Variant(timestamp - m_prev_timestamp)); m_prev_timestamp = timestamp; @@ -105,19 +109,18 @@ class PcpService { pmFreeResult(res); } - bool setup_metrics(Caliper* c, const std::vector& names) { - std::vector list; + bool setup_metrics(Caliper* c, const std::vector& names) + { + std::vector list; std::vector info; for (const std::string& name : names) { - pmID pmid = PM_ID_NULL; - const char* namep = name.data(); - int status = pmLookupName(1, &namep, &pmid); + pmID pmid = PM_ID_NULL; + const char* namep = name.data(); + int status = pmLookupName(1, &namep, &pmid); if (status != 1) { - Log(0).stream() << "pcp: pmLookup: " - << pmErrStr(status) - << std::endl; + Log(0).stream() << "pcp: pmLookup: " << pmErrStr(status) << std::endl; return false; } @@ -131,24 +134,20 @@ class PcpService { } if (Log::verbosity() >= 2) { - Log(2).stream() << "pcp: Adding " - << name - << " (pmid=" << pmid - << ", type=" << pmdesc.type - << ", sem=" << pmdesc.sem - << ")" << std::endl; + Log(2).stream() << "pcp: Adding " << name << " (pmid=" << pmid << ", type=" << pmdesc.type + << ", sem=" << pmdesc.sem << ")" << std::endl; } // TODO: Do some sanity checking - Attribute attr = - c->create_attribute(std::string("pcp.")+name, CALI_TYPE_DOUBLE, - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_AGGREGATABLE); + Attribute attr = c->create_attribute( + std::string("pcp.") + name, + CALI_TYPE_DOUBLE, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_AGGREGATABLE + ); list.push_back(pmid); - info.push_back( { name, attr, pmdesc} ); + info.push_back({ name, attr, pmdesc }); } m_metric_list = std::move(list); @@ -158,40 +157,39 @@ class PcpService { return true; } - void finish(Caliper*, Channel* channel) { - Log(1).stream() << channel->name() << ": pcp: " - << m_num_lookups << " lookups, " - << m_num_failed_lookups << " failed." << std::endl; + void finish(Caliper*, Channel* channel) + { + Log(1).stream() << channel->name() << ": pcp: " << m_num_lookups << " lookups, " << m_num_failed_lookups + << " failed." << std::endl; } PcpService(Caliper* c, Channel* channel) { - Attribute unit_attr = - c->create_attribute("time.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + Attribute unit_attr = c->create_attribute("time.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); Variant sec_val = Variant("sec"); - m_timestamp_sec_attr = - c->create_attribute("pcp.timestamp.sec", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_timestamp_attr = - c->create_attribute("pcp.timestamp", CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_time_duration_attr = - c->create_attribute("pcp.time.duration", CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE, - 1, &unit_attr, &sec_val); + m_timestamp_sec_attr = c->create_attribute( + "pcp.timestamp.sec", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_timestamp_attr = c->create_attribute( + "pcp.timestamp", + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_time_duration_attr = c->create_attribute( + "pcp.time.duration", + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + 1, + &unit_attr, + &sec_val + ); } - static bool init_pcp_context(const char* hostname) { + static bool init_pcp_context(const char* hostname) + { if (s_pcp_context < 0) s_pcp_context = pmNewContext(PM_CONTEXT_HOST, hostname); @@ -200,37 +198,32 @@ class PcpService { return !(s_pcp_context < 0); } - static void finish_pcp_context() { + static void finish_pcp_context() + { if (--s_num_instances == 0) pmDestroyContext(s_pcp_context); } public: - static void register_pcp(Caliper* c, Channel* channel) { - auto metriclist = - channel->config().init("pcp", s_configdata).get("metrics").to_stringlist(","); + static void register_pcp(Caliper* c, Channel* channel) + { + auto metriclist = channel->config().init("pcp", s_configdata).get("metrics").to_stringlist(","); if (metriclist.empty()) { - Log(1).stream() << channel->name() - << ": pcp: No metrics specified" - << std::endl; + Log(1).stream() << channel->name() << ": pcp: No metrics specified" << std::endl; return; } int node_rank = machine::get_rank_for(machine::MachineLevel::Node); if (node_rank < 0) - Log(0).stream() << channel->name() - << ": pcp: Unable to determine node master" - << std::endl; + Log(0).stream() << channel->name() << ": pcp: Unable to determine node master" << std::endl; if (node_rank != 0) return; if (!init_pcp_context("local:")) { - Log(0).stream() << channel->name() - << ": pcp: Context not initialized" - << std::endl; + Log(0).stream() << channel->name() << ": pcp: Context not initialized" << std::endl; return; } @@ -238,48 +231,44 @@ class PcpService { ++s_num_instances; if (!instance->setup_metrics(c, metriclist)) { - Log(0).stream() << channel->name() - << ": pcp: Failed to initialize metrics" - << std::endl; + Log(0).stream() << channel->name() << ": pcp: Failed to initialize metrics" << std::endl; finish_pcp_context(); delete instance; return; } - channel->events().snapshot.connect( - [instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec){ - instance->snapshot(c, rec); - }); - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel*){ - // fetch current values to initialize m_prev_value - SnapshotBuilder builder; - instance->snapshot(c, builder); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish(c, channel); - finish_pcp_context(); - delete instance; - }); + channel->events().snapshot.connect([instance](Caliper* c, Channel*, SnapshotView, SnapshotBuilder& rec) { + instance->snapshot(c, rec); + }); + channel->events().post_init_evt.connect([instance](Caliper* c, Channel*) { + // fetch current values to initialize m_prev_value + SnapshotBuilder builder; + instance->snapshot(c, builder); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish(c, channel); + finish_pcp_context(); + delete instance; + }); Log(1).stream() << channel->name() << ": Registered pcp service" << std::endl; } }; -int PcpService::s_num_instances = 0; +int PcpService::s_num_instances = 0; int PcpService::s_pcp_context = -1; const ConfigSet::Entry PcpService::s_configdata[] = { - { "metrics", CALI_TYPE_STRING, "", + { "metrics", + CALI_TYPE_STRING, + "", "List of performance co-pilot metrics to record", - "List of performance co-pilot metrics to record, separated by ','" - }, + "List of performance co-pilot metrics to record, separated by ','" }, ConfigSet::Terminator }; -} +} // namespace namespace cali { diff --git a/src/services/pcp/PcpMemory.cpp b/src/services/pcp/PcpMemory.cpp index a653b5a2d..c8a62fe90 100644 --- a/src/services/pcp/PcpMemory.cpp +++ b/src/services/pcp/PcpMemory.cpp @@ -33,17 +33,17 @@ const char* wr_cas_metrics = ",perfevent.hwcounters.bdx_unc_imc4__UNC_M_CAS_COUNT_WR.value" ",perfevent.hwcounters.bdx_unc_imc5__UNC_M_CAS_COUNT_WR.value"; -std::vector -find_counter_attributes(const CaliperMetadataAccessInterface& db, const char* metrics) { +std::vector find_counter_attributes(const CaliperMetadataAccessInterface& db, const char* metrics) +{ std::vector res; auto counters = StringConverter(metrics).to_stringlist(); - for (const auto &s : counters) { - Attribute attr = db.get_attribute(std::string("sum#pcp.")+s); + for (const auto& s : counters) { + Attribute attr = db.get_attribute(std::string("sum#pcp.") + s); if (!attr) - attr = db.get_attribute(std::string("pcp.")+s); + attr = db.get_attribute(std::string("pcp.") + s); if (!attr) continue; @@ -53,16 +53,13 @@ find_counter_attributes(const CaliperMetadataAccessInterface& db, const char* me return res; } -std::pair -sum_attributes(const std::vector& rec, const std::vector& attributes) +std::pair sum_attributes(const std::vector& rec, const std::vector& attributes) { - double sum = 0.0; - int count = 0; + double sum = 0.0; + int count = 0; for (const Attribute& a : attributes) { - auto it = std::find_if(rec.begin(), rec.end(), [a](const Entry& e) { - return e.attribute() == a.id(); - }); + auto it = std::find_if(rec.begin(), rec.end(), [a](const Entry& e) { return e.attribute() == a.id(); }); if (it != rec.end()) { ++count; @@ -73,7 +70,6 @@ sum_attributes(const std::vector& rec, const std::vector& attr return std::make_pair(sum, count); } - class PcpMemory { std::vector rd_counter_attrs; @@ -82,10 +78,11 @@ class PcpMemory Attribute rd_result_attr; Attribute wr_result_attr; - unsigned num_computed; - unsigned num_flushes; + unsigned num_computed; + unsigned num_flushes; - void postprocess_snapshot_cb(std::vector& rec) { + void postprocess_snapshot_cb(std::vector& rec) + { auto rp = sum_attributes(rec, rd_counter_attrs); auto wp = sum_attributes(rec, wr_counter_attrs); @@ -98,7 +95,8 @@ class PcpMemory ++num_computed; } - void pre_flush_cb(Caliper* c, Channel* channel) { + void pre_flush_cb(Caliper* c, Channel* channel) + { ++num_flushes; if (rd_counter_attrs.size() + wr_counter_attrs.size() > 0) @@ -108,48 +106,42 @@ class PcpMemory wr_counter_attrs = ::find_counter_attributes(*c, wr_cas_metrics); if (rd_counter_attrs.size() + wr_counter_attrs.size() > 0) - channel->events().postprocess_snapshot.connect( - [this](Caliper*, Channel*, std::vector& rec){ - this->postprocess_snapshot_cb(rec); - }); + channel->events().postprocess_snapshot.connect([this](Caliper*, Channel*, std::vector& rec) { + this->postprocess_snapshot_cb(rec); + }); } - void finish_cb(Caliper* c, Channel* channel) { + void finish_cb(Caliper* c, Channel* channel) + { if (num_flushes > 0) { if (rd_counter_attrs.empty()) - Log(1).stream() << channel->name() - << ": pcp.memory: read metrics not found" - << std::endl; + Log(1).stream() << channel->name() << ": pcp.memory: read metrics not found" << std::endl; if (wr_counter_attrs.empty()) - Log(1).stream() << channel->name() - << ": pcp.memory: write metrics not found" - << std::endl; + Log(1).stream() << channel->name() << ": pcp.memory: write metrics not found" << std::endl; } - Log(1).stream() << channel->name() - << ": pcp.memory: Computed memory metrics for " - << num_computed << " records" + Log(1).stream() << channel->name() << ": pcp.memory: Computed memory metrics for " << num_computed << " records" << std::endl; } - PcpMemory(Caliper* c, Channel*) - : num_computed(0), num_flushes(0) - { - rd_result_attr = - c->create_attribute("mem.bytes.read", CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - wr_result_attr = - c->create_attribute("mem.bytes.written", CALI_TYPE_DOUBLE, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - } + PcpMemory(Caliper* c, Channel*) : num_computed(0), num_flushes(0) + { + rd_result_attr = c->create_attribute( + "mem.bytes.read", + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + wr_result_attr = c->create_attribute( + "mem.bytes.written", + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + } public: - static void pcp_memory_register(Caliper* c, Channel* channel) { + static void pcp_memory_register(Caliper* c, Channel* channel) + { std::string metrics = rd_cas_metrics; metrics.append(","); metrics.append(wr_cas_metrics); @@ -157,30 +149,26 @@ class PcpMemory channel->config().set("CALI_PCP_METRICS", metrics); if (!cali::services::register_service(c, channel, "pcp")) { - Log(0).stream() << channel->name() - << ": pcp.memory: Unable to register pcp service, skipping pcp.memory" + Log(0).stream() << channel->name() << ": pcp.memory: Unable to register pcp service, skipping pcp.memory" << std::endl; return; } PcpMemory* instance = new PcpMemory(c, channel); - channel->events().pre_flush_evt.connect( - [instance](Caliper* c, Channel* channel, SnapshotView){ - instance->pre_flush_cb(c, channel); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - delete instance; - }); + channel->events().pre_flush_evt.connect([instance](Caliper* c, Channel* channel, SnapshotView) { + instance->pre_flush_cb(c, channel); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + delete instance; + }); Log(1).stream() << channel->name() << ": Registered pcp.memory service" << std::endl; } - }; -} +} // namespace namespace cali { diff --git a/src/services/pthread/PthreadService.cpp b/src/services/pthread/PthreadService.cpp index 971108528..c48fbc606 100644 --- a/src/services/pthread/PthreadService.cpp +++ b/src/services/pthread/PthreadService.cpp @@ -23,7 +23,7 @@ using namespace cali; namespace { -gotcha_wrappee_handle_t orig_pthread_create_handle = 0x0; +gotcha_wrappee_handle_t orig_pthread_create_handle = 0x0; Attribute id_attr; Attribute master_attr; @@ -36,26 +36,23 @@ struct wrapper_args { // Wrapper for the user-provided thread start function. // We wrap the original thread start function to create Caliper thread scope // on the new child thread. -void* -thread_wrapper(void *arg) +void* thread_wrapper(void* arg) { uint64_t id = static_cast(pthread_self()); Caliper c; c.set(master_attr, Variant(false)); - c.set(id_attr, Variant(cali_make_variant_from_uint(id))); + c.set(id_attr, Variant(cali_make_variant_from_uint(id))); wrapper_args* wrap = static_cast(arg); - void* ret = (*(wrap->fn))(wrap->arg); + void* ret = (*(wrap->fn))(wrap->arg); delete wrap; return ret; } // Wrapper for pthread_create() -int -cali_pthread_create_wrapper(pthread_t *thread, const pthread_attr_t *attr, - void *(*fn)(void*), void* arg) +int cali_pthread_create_wrapper(pthread_t* thread, const pthread_attr_t* attr, void* (*fn)(void*), void* arg) { decltype(&pthread_create) orig_pthread_create = reinterpret_cast(gotcha_get_wrappee(orig_pthread_create_handle)); @@ -63,8 +60,7 @@ cali_pthread_create_wrapper(pthread_t *thread, const pthread_attr_t *attr, return (*orig_pthread_create)(thread, attr, thread_wrapper, new wrapper_args({ fn, arg })); } -void -post_init_cb(Caliper* c, Channel* channel) +void post_init_cb(Caliper* c, Channel* channel) { channel->events().subscribe_attribute(c, channel, id_attr); @@ -75,45 +71,43 @@ post_init_cb(Caliper* c, Channel* channel) { "pthread_create", (void*) cali_pthread_create_wrapper, &orig_pthread_create_handle } }; - gotcha_wrap(pthread_binding, sizeof(pthread_binding)/sizeof(struct gotcha_binding_t), - "caliper/pthread"); + gotcha_wrap(pthread_binding, sizeof(pthread_binding) / sizeof(struct gotcha_binding_t), "caliper/pthread"); is_wrapped = true; uint64_t id = static_cast(pthread_self()); c->set(master_attr, Variant(true)); - c->set(id_attr, Variant(cali_make_variant_from_uint(id))); + c->set(id_attr, Variant(cali_make_variant_from_uint(id))); } } // Initialization routine. -void -pthreadservice_initialize(Caliper* c, Channel* chn) +void pthreadservice_initialize(Caliper* c, Channel* chn) { Attribute subscription_attr = c->get_attribute("subscription_event"); - Variant v_true(true); - - id_attr = - c->create_attribute("pthread.id", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_UNALIGNED, - 1, &subscription_attr, &v_true); - master_attr = - c->create_attribute("pthread.is_master", CALI_TYPE_BOOL, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_UNALIGNED | - CALI_ATTR_SKIP_EVENTS); - - chn->events().post_init_evt.connect( - [](Caliper* c, Channel* chn){ - post_init_cb(c, chn); - }); + Variant v_true(true); + + id_attr = c->create_attribute( + "pthread.id", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED, + 1, + &subscription_attr, + &v_true + ); + master_attr = c->create_attribute( + "pthread.is_master", + CALI_TYPE_BOOL, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS + ); + + chn->events().post_init_evt.connect([](Caliper* c, Channel* chn) { post_init_cb(c, chn); }); Log(1).stream() << chn->name() << ": Registered pthread service" << std::endl; } -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/recorder/Recorder.cpp b/src/services/recorder/Recorder.cpp index 519814263..a44b828c8 100644 --- a/src/services/recorder/Recorder.cpp +++ b/src/services/recorder/Recorder.cpp @@ -62,14 +62,13 @@ void write_output_cb(Caliper* c, Channel* chn, SnapshotView flush_info) CaliWriter writer(stream); - c->flush(chn, flush_info, [&writer](CaliperMetadataAccessInterface& db, const std::vector& rec){ - writer.write_snapshot(db, rec); - }); + c->flush(chn, flush_info, [&writer](CaliperMetadataAccessInterface& db, const std::vector& rec) { + writer.write_snapshot(db, rec); + }); writer.write_globals(*c, c->get_globals(*chn)); - Log(1).stream() << chn->name() - << ": Recorder: Wrote " << writer.num_written() << " records." << std::endl; + Log(1).stream() << chn->name() << ": Recorder: Wrote " << writer.num_written() << " records." << std::endl; } void recorder_register(Caliper* c, Channel* chn) diff --git a/src/services/report/Report.cpp b/src/services/report/Report.cpp index 189905a0d..8af625fdc 100644 --- a/src/services/report/Report.cpp +++ b/src/services/report/Report.cpp @@ -4,7 +4,6 @@ // Report.cpp // Generates text reports from Caliper snapshots on flush() events - #include "caliper/CaliperService.h" #include "../Services.h" @@ -28,22 +27,23 @@ using namespace cali; namespace { -class Report { +class Report +{ // // --- callback functions // - void write_output(Caliper* c, Channel* channel, SnapshotView flush_info) { - ConfigSet config = services::init_config_from_spec(channel->config(), s_spec); + void write_output(Caliper* c, Channel* channel, SnapshotView flush_info) + { + ConfigSet config = services::init_config_from_spec(channel->config(), s_spec); CalQLParser parser(config.get("config").to_string().c_str()); if (parser.error()) { - Log(0).stream() << channel->name() << ": Report: config parse error: " - << parser.error_msg() << std::endl; + Log(0).stream() << channel->name() << ": Report: config parse error: " << parser.error_msg() << std::endl; return; } - QuerySpec spec(parser.spec()); + QuerySpec spec(parser.spec()); // set format default to table if it hasn't been set in the query config if (spec.format.opt == QuerySpec::FormatSpec::Default) @@ -61,14 +61,18 @@ class Report { stream.set_mode(OutputStream::Mode::Append); CaliperMetadataDB db; - QueryProcessor queryP(spec, stream); + QueryProcessor queryP(spec, stream); db.add_attribute_aliases(spec.aliases); db.add_attribute_units(spec.units); - c->flush(channel, flush_info, [&queryP,&db](CaliperMetadataAccessInterface& in_db, const std::vector& rec){ + c->flush( + channel, + flush_info, + [&queryP, &db](CaliperMetadataAccessInterface& in_db, const std::vector& rec) { queryP.process_record(db, db.merge_snapshot(in_db, rec)); - } ); + } + ); db.import_globals(*c, c->get_globals(*channel)); @@ -77,22 +81,18 @@ class Report { public: - ~Report() - { } + ~Report() {} static const char* s_spec; - static void create(Caliper* c, Channel* channel) { + static void create(Caliper* c, Channel* channel) + { Report* instance = new Report; - channel->events().write_output_evt.connect( - [instance](Caliper* c, Channel* channel, SnapshotView info){ - instance->write_output(c, channel, info); - }); - channel->events().finish_evt.connect( - [instance](Caliper*, Channel*){ - delete instance; - }); + channel->events().write_output_evt.connect([instance](Caliper* c, Channel* channel, SnapshotView info) { + instance->write_output(c, channel, info); + }); + channel->events().finish_evt.connect([instance](Caliper*, Channel*) { delete instance; }); Log(1).stream() << channel->name() << ": Registered report service" << std::endl; } @@ -124,5 +124,5 @@ const char* Report::s_spec = R"json( namespace cali { - CaliperService report_service { ::Report::s_spec, ::Report::create }; +CaliperService report_service { ::Report::s_spec, ::Report::create }; } diff --git a/src/services/rocprofiler/RocProfiler.cpp b/src/services/rocprofiler/RocProfiler.cpp index 58f2abc66..82a5c7bd2 100644 --- a/src/services/rocprofiler/RocProfiler.cpp +++ b/src/services/rocprofiler/RocProfiler.cpp @@ -24,34 +24,31 @@ using namespace cali; -#define ROCPROFILER_CALL(result) \ - { \ - rocprofiler_status_t CHECKSTATUS = result; \ - if(CHECKSTATUS != ROCPROFILER_STATUS_SUCCESS) \ - { \ - std::string status_msg = rocprofiler_get_status_string(CHECKSTATUS); \ - std::cerr << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] " \ - << " failed with error code " << CHECKSTATUS << ": " << status_msg \ - << std::endl; \ - std::stringstream errmsg{}; \ - errmsg << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] failure (" \ - << status_msg << ")"; \ - throw std::runtime_error(errmsg.str()); \ - } \ +#define ROCPROFILER_CALL(result) \ + { \ + rocprofiler_status_t CHECKSTATUS = result; \ + if (CHECKSTATUS != ROCPROFILER_STATUS_SUCCESS) { \ + std::string status_msg = rocprofiler_get_status_string(CHECKSTATUS); \ + std::cerr << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] " \ + << " failed with error code " << CHECKSTATUS << ": " << status_msg << std::endl; \ + std::stringstream errmsg {}; \ + errmsg << "[" #result "][" << __FILE__ << ":" << __LINE__ << "] failure (" << status_msg << ")"; \ + throw std::runtime_error(errmsg.str()); \ + } \ } - namespace { -int -set_external_correlation_id(rocprofiler_thread_id_t /*thr_id*/, - rocprofiler_context_id_t /*ctx_id*/, - rocprofiler_external_correlation_id_request_kind_t /*kind*/, - rocprofiler_tracing_operation_t /*op*/, - uint64_t /*internal_corr_id*/, - rocprofiler_user_data_t* external_corr_id, - void* /*user_data*/) +int set_external_correlation_id( + rocprofiler_thread_id_t /*thr_id*/, + rocprofiler_context_id_t /*ctx_id*/, + rocprofiler_external_correlation_id_request_kind_t /*kind*/, + rocprofiler_tracing_operation_t /*op*/, + uint64_t /*internal_corr_id*/, + rocprofiler_user_data_t* external_corr_id, + void* /*user_data*/ +) { Caliper c; external_corr_id->ptr = c.get_path_node().node(); @@ -59,11 +56,10 @@ set_external_correlation_id(rocprofiler_thread_id_t / } template -auto -make_array(Arg arg, Args&&... args) +auto make_array(Arg arg, Args&&... args) { constexpr auto N = 1 + sizeof...(Args); - return std::array{std::forward(arg), std::forward(args)...}; + return std::array { std::forward(arg), std::forward(args)... }; } class RocProfilerService @@ -88,16 +84,16 @@ class RocProfilerService Attribute m_flush_region_attr; - bool m_enable_api_callbacks = false; - bool m_enable_activity_tracing = false; - bool m_enable_snapshot_timestamps = false; + bool m_enable_api_callbacks = false; + bool m_enable_activity_tracing = false; + bool m_enable_snapshot_timestamps = false; - unsigned m_num_activity_records = 0; + unsigned m_num_activity_records = 0; std::map m_kernel_info; - std::mutex m_kernel_info_mutex; + std::mutex m_kernel_info_mutex; - Channel m_channel; + Channel m_channel; static RocProfilerService* s_instance; @@ -105,93 +101,81 @@ class RocProfilerService static rocprofiler_context_id_t activity_ctx; static rocprofiler_context_id_t rocprofiler_ctx; - static rocprofiler_buffer_id_t activity_buf; + static rocprofiler_buffer_id_t activity_buf; static std::map s_agents; void create_attributes(Caliper* c) { Attribute subs_attr = c->get_attribute("subscription_event"); - Variant v_true(true); + Variant v_true(true); - m_api_attr = - c->create_attribute("rocm.api", CALI_TYPE_STRING, CALI_ATTR_NESTED, - 1, &subs_attr, &v_true); + m_api_attr = c->create_attribute("rocm.api", CALI_TYPE_STRING, CALI_ATTR_NESTED, 1, &subs_attr, &v_true); m_activity_start_attr = - c->create_attribute("rocm.starttime", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + c->create_attribute("rocm.starttime", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); m_activity_end_attr = - c->create_attribute("rocm.endtime", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); - - m_activity_duration_attr = - c->create_attribute("rocm.activity.duration", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - - m_host_timestamp_attr = - c->create_attribute("rocm.host.timestamp", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS); - m_host_duration_attr = - c->create_attribute("rocm.host.duration", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - - m_activity_name_attr = - c->create_attribute("rocm.activity", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - m_activity_queue_id_attr = - c->create_attribute("rocm.activity.queue", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_activity_device_id_attr = - c->create_attribute("rocm.activity.device", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_activity_bytes_attr = - c->create_attribute("rocm.activity.bytes", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_kernel_name_attr = - c->create_attribute("rocm.kernel.name", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - m_src_agent_attr = - c->create_attribute("rocm.src.agent", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_dst_agent_attr = - c->create_attribute("rocm.dst.agent", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_agent_attr = - c->create_attribute("rocm.agent", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + c->create_attribute("rocm.endtime", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + + m_activity_duration_attr = c->create_attribute( + "rocm.activity.duration", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + + m_host_timestamp_attr = c->create_attribute( + "rocm.host.timestamp", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ); + m_host_duration_attr = c->create_attribute( + "rocm.host.duration", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + + m_activity_name_attr = c->create_attribute("rocm.activity", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + m_activity_queue_id_attr = c->create_attribute("rocm.activity.queue", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_activity_device_id_attr = c->create_attribute("rocm.activity.device", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_activity_bytes_attr = c->create_attribute("rocm.activity.bytes", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_kernel_name_attr = c->create_attribute("rocm.kernel.name", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + m_src_agent_attr = c->create_attribute("rocm.src.agent", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_dst_agent_attr = c->create_attribute("rocm.dst.agent", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_agent_attr = c->create_attribute("rocm.agent", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); m_flush_region_attr = c->create_attribute("rocprofiler.flush", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_DEFAULT); } - void update_kernel_info(uint64_t kernel_id, const std::string& name) { - std::lock_guard - g(m_kernel_info_mutex); + void update_kernel_info(uint64_t kernel_id, const std::string& name) + { + std::lock_guard g(m_kernel_info_mutex); m_kernel_info.emplace(kernel_id, name); } - const char* - get_kernel_name(uint64_t kernel_id) { - std::lock_guard - g(m_kernel_info_mutex); + const char* get_kernel_name(uint64_t kernel_id) + { + std::lock_guard g(m_kernel_info_mutex); auto it = m_kernel_info.find(kernel_id); return it != m_kernel_info.end() ? it->second.c_str() : nullptr; } - void pre_flush_cb() { + void pre_flush_cb() + { if (activity_buf.handle > 0) ROCPROFILER_CALL(rocprofiler_flush_buffer(activity_buf)); } - static void - tool_tracing_callback(rocprofiler_context_id_t context, - rocprofiler_buffer_id_t buffer_id, - rocprofiler_record_header_t** headers, - size_t num_headers, - void* user_data, - uint64_t drop_count) + static void tool_tracing_callback( + rocprofiler_context_id_t context, + rocprofiler_buffer_id_t buffer_id, + rocprofiler_record_header_t** headers, + size_t num_headers, + void* user_data, + uint64_t drop_count + ) { Caliper c; c.begin(s_instance->m_flush_region_attr, Variant("ROCPROFILER FLUSH")); @@ -199,29 +183,25 @@ class RocProfilerService for (size_t i = 0; i < num_headers; ++i) { auto* header = headers[i]; - if (header->category == ROCPROFILER_BUFFER_CATEGORY_TRACING && - header->kind == ROCPROFILER_BUFFER_TRACING_KERNEL_DISPATCH) { + if (header->category == ROCPROFILER_BUFFER_CATEGORY_TRACING + && header->kind == ROCPROFILER_BUFFER_TRACING_KERNEL_DISPATCH) { - auto* record = - static_cast(header->payload); + auto* record = static_cast(header->payload); - const Attribute attr[] = { - s_instance->m_activity_name_attr, - s_instance->m_activity_start_attr, - s_instance->m_activity_end_attr, - s_instance->m_activity_duration_attr, - s_instance->m_kernel_name_attr, - s_instance->m_agent_attr - }; + const Attribute attr[] = { s_instance->m_activity_name_attr, s_instance->m_activity_start_attr, + s_instance->m_activity_end_attr, s_instance->m_activity_duration_attr, + s_instance->m_kernel_name_attr, s_instance->m_agent_attr }; const char* activity_name = nullptr; - uint64_t len; - ROCPROFILER_CALL( - rocprofiler_query_buffer_tracing_kind_operation_name( - record->kind, record->operation, &activity_name, &len)); + uint64_t len; + ROCPROFILER_CALL(rocprofiler_query_buffer_tracing_kind_operation_name( + record->kind, + record->operation, + &activity_name, + &len + )); - const char* kernel_name = - s_instance->get_kernel_name(record->dispatch_info.kernel_id); + const char* kernel_name = s_instance->get_kernel_name(record->dispatch_info.kernel_id); uint64_t agent = s_agents.at(record->dispatch_info.agent_id.handle)->logical_node_id; @@ -238,29 +218,26 @@ class RocProfilerService FixedSizeSnapshotRecord<6> snapshot; c.make_record(6, attr, data, snapshot.builder(), correlation); - s_instance->m_channel.events().process_snapshot(&c, &s_instance->m_channel, SnapshotView(), snapshot.view()); + s_instance->m_channel.events() + .process_snapshot(&c, &s_instance->m_channel, SnapshotView(), snapshot.view()); ++s_instance->m_num_activity_records; - } else if (header->category == ROCPROFILER_BUFFER_CATEGORY_TRACING && - header->kind == ROCPROFILER_BUFFER_TRACING_MEMORY_COPY) { - - auto* record = - static_cast(header->payload); - - const Attribute attr[] = { - s_instance->m_activity_name_attr, - s_instance->m_activity_start_attr, - s_instance->m_activity_end_attr, - s_instance->m_activity_duration_attr, - s_instance->m_src_agent_attr, - s_instance->m_dst_agent_attr - }; + } else if (header->category == ROCPROFILER_BUFFER_CATEGORY_TRACING && header->kind == ROCPROFILER_BUFFER_TRACING_MEMORY_COPY) { + + auto* record = static_cast(header->payload); + + const Attribute attr[] = { s_instance->m_activity_name_attr, s_instance->m_activity_start_attr, + s_instance->m_activity_end_attr, s_instance->m_activity_duration_attr, + s_instance->m_src_agent_attr, s_instance->m_dst_agent_attr }; const char* activity_name = nullptr; - uint64_t len; - ROCPROFILER_CALL( - rocprofiler_query_buffer_tracing_kind_operation_name( - record->kind, record->operation, &activity_name, &len)); + uint64_t len; + ROCPROFILER_CALL(rocprofiler_query_buffer_tracing_kind_operation_name( + record->kind, + record->operation, + &activity_name, + &len + )); uint64_t src_agent = s_agents.at(record->src_agent_id.handle)->logical_node_id; uint64_t dst_agent = s_agents.at(record->dst_agent_id.handle)->logical_node_id; @@ -278,7 +255,8 @@ class RocProfilerService FixedSizeSnapshotRecord<6> snapshot; c.make_record(6, attr, data, snapshot.builder(), correlation); - s_instance->m_channel.events().process_snapshot(&c, &s_instance->m_channel, SnapshotView(), snapshot.view()); + s_instance->m_channel.events() + .process_snapshot(&c, &s_instance->m_channel, SnapshotView(), snapshot.view()); ++s_instance->m_num_activity_records; } @@ -287,26 +265,30 @@ class RocProfilerService c.end(s_instance->m_flush_region_attr); } - static void tool_api_cb(rocprofiler_callback_tracing_record_t record, - rocprofiler_user_data_t* user_data, - void* /* callback_data */) + static void tool_api_cb( + rocprofiler_callback_tracing_record_t record, + rocprofiler_user_data_t* user_data, + void* /* callback_data */ + ) { if (!s_instance) return; if (record.kind == ROCPROFILER_CALLBACK_TRACING_CODE_OBJECT) { - if (record.operation == ROCPROFILER_CODE_OBJECT_DEVICE_KERNEL_SYMBOL_REGISTER && - record.phase == ROCPROFILER_CALLBACK_PHASE_LOAD) { - auto *data = static_cast(record.payload); + if (record.operation == ROCPROFILER_CODE_OBJECT_DEVICE_KERNEL_SYMBOL_REGISTER + && record.phase == ROCPROFILER_CALLBACK_PHASE_LOAD) { + auto* data = + static_cast(record.payload + ); s_instance->update_kernel_info(data->kernel_id, util::demangle(data->kernel_name)); } } else { if (record.phase == ROCPROFILER_CALLBACK_PHASE_ENTER) { const char* name = nullptr; - uint64_t len = 0; + uint64_t len = 0; ROCPROFILER_CALL( - rocprofiler_query_callback_tracing_kind_operation_name( - record.kind, record.operation, &name, &len)); + rocprofiler_query_callback_tracing_kind_operation_name(record.kind, record.operation, &name, &len) + ); Caliper c; if (name) c.begin(s_instance->m_api_attr, Variant(CALI_TYPE_STRING, name, len)); @@ -317,7 +299,8 @@ class RocProfilerService } } - void snapshot_cb(Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& snapshot) { + void snapshot_cb(Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& snapshot) + { auto ts = rocprofiler_timestamp_t {}; rocprofiler_get_timestamp(&ts); @@ -325,11 +308,11 @@ class RocProfilerService Variant v_now(cali_make_variant_from_uint(timestamp)); Variant v_prev = c->exchange(m_host_timestamp_attr, v_now); - snapshot.append(Entry(m_host_duration_attr, - cali_make_variant_from_uint(timestamp - v_prev.to_uint()))); + snapshot.append(Entry(m_host_duration_attr, cali_make_variant_from_uint(timestamp - v_prev.to_uint()))); } - void post_init_cb(Caliper* c, Channel* channel) { + void post_init_cb(Caliper* c, Channel* channel) + { if (m_enable_api_callbacks) { channel->events().subscribe_attribute(c, channel, m_api_attr); ROCPROFILER_CALL(rocprofiler_start_context(hip_api_ctx)); @@ -339,10 +322,7 @@ class RocProfilerService ROCPROFILER_CALL(rocprofiler_start_context(rocprofiler_ctx)); ROCPROFILER_CALL(rocprofiler_start_context(activity_ctx)); - channel->events().pre_flush_evt.connect( - [this](Caliper*, Channel*, SnapshotView){ - this->pre_flush_cb(); - }); + channel->events().pre_flush_evt.connect([this](Caliper*, Channel*, SnapshotView) { this->pre_flush_cb(); }); } if (m_enable_snapshot_timestamps) { @@ -351,13 +331,15 @@ class RocProfilerService c->set(m_host_timestamp_attr, Variant(cali_make_variant_from_uint(static_cast(ts)))); channel->events().snapshot.connect( - [this](Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& snapshot){ + [this](Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& snapshot) { this->snapshot_cb(c, channel, trigger_info, snapshot); - }); + } + ); } } - void pre_finish_cb(Caliper* c, Channel* channel) { + void pre_finish_cb(Caliper* c, Channel* channel) + { int status = 0; ROCPROFILER_CALL(rocprofiler_context_is_active(hip_api_ctx, &status)); if (status) @@ -369,21 +351,17 @@ class RocProfilerService if (status) ROCPROFILER_CALL(rocprofiler_stop_context(activity_ctx)); - Log(1).stream() << channel->name() - << ": rocprofiler: wrote " << m_num_activity_records << " activity records\n"; + Log(1).stream() << channel->name() << ": rocprofiler: wrote " << m_num_activity_records + << " activity records\n"; } - RocProfilerService(Caliper* c, Channel* channel) - : m_channel { *channel } + RocProfilerService(Caliper* c, Channel* channel) : m_channel { *channel } { auto config = services::init_config_from_spec(channel->config(), s_spec); - m_enable_api_callbacks = - config.get("enable_api_callbacks").to_bool(); - m_enable_activity_tracing = - config.get("enable_activity_tracing").to_bool(); - m_enable_snapshot_timestamps = - config.get("enable_snapshot_timestamps").to_bool(); + m_enable_api_callbacks = config.get("enable_api_callbacks").to_bool(); + m_enable_activity_tracing = config.get("enable_activity_tracing").to_bool(); + m_enable_snapshot_timestamps = config.get("enable_snapshot_timestamps").to_bool(); create_attributes(c); } @@ -393,77 +371,91 @@ class RocProfilerService static const char* s_spec; static const char* s_roctracer_spec; - static int tool_init(rocprofiler_client_finalize_t fini_func, void* tool_data) { + static int tool_init(rocprofiler_client_finalize_t fini_func, void* tool_data) + { ROCPROFILER_CALL(rocprofiler_create_context(&hip_api_ctx)); ROCPROFILER_CALL(rocprofiler_create_context(&activity_ctx)); ROCPROFILER_CALL(rocprofiler_create_context(&rocprofiler_ctx)); - ROCPROFILER_CALL( - rocprofiler_configure_callback_tracing_service( - hip_api_ctx, - ROCPROFILER_CALLBACK_TRACING_HIP_RUNTIME_API, - nullptr, 0, tool_api_cb, nullptr)); - ROCPROFILER_CALL( - rocprofiler_configure_callback_tracing_service( - rocprofiler_ctx, - ROCPROFILER_CALLBACK_TRACING_CODE_OBJECT, - nullptr, 0, tool_api_cb, nullptr)); - - ROCPROFILER_CALL( - rocprofiler_create_buffer( - activity_ctx, 16*1024*1024, 15*1024*1024, - ROCPROFILER_BUFFER_POLICY_LOSSLESS, - tool_tracing_callback, - nullptr, &activity_buf)); + ROCPROFILER_CALL(rocprofiler_configure_callback_tracing_service( + hip_api_ctx, + ROCPROFILER_CALLBACK_TRACING_HIP_RUNTIME_API, + nullptr, + 0, + tool_api_cb, + nullptr + )); + ROCPROFILER_CALL(rocprofiler_configure_callback_tracing_service( + rocprofiler_ctx, + ROCPROFILER_CALLBACK_TRACING_CODE_OBJECT, + nullptr, + 0, + tool_api_cb, + nullptr + )); + + ROCPROFILER_CALL(rocprofiler_create_buffer( + activity_ctx, + 16 * 1024 * 1024, + 15 * 1024 * 1024, + ROCPROFILER_BUFFER_POLICY_LOSSLESS, + tool_tracing_callback, + nullptr, + &activity_buf + )); // auto kernel_dispatch_cb_ops = // std::array{ROCPROFILER_KERNEL_DISPATCH_COMPLETE}; - ROCPROFILER_CALL( - rocprofiler_configure_buffer_tracing_service( - activity_ctx, - ROCPROFILER_BUFFER_TRACING_KERNEL_DISPATCH, - nullptr, 0, activity_buf)); - ROCPROFILER_CALL( - rocprofiler_configure_buffer_tracing_service( - activity_ctx, - ROCPROFILER_BUFFER_TRACING_MEMORY_COPY, - nullptr, 0, activity_buf)); -/* + ROCPROFILER_CALL(rocprofiler_configure_buffer_tracing_service( + activity_ctx, + ROCPROFILER_BUFFER_TRACING_KERNEL_DISPATCH, + nullptr, + 0, + activity_buf + )); + ROCPROFILER_CALL(rocprofiler_configure_buffer_tracing_service( + activity_ctx, + ROCPROFILER_BUFFER_TRACING_MEMORY_COPY, + nullptr, + 0, + activity_buf + )); + /* ROCPROFILER_CALL( rocprofiler_configure_buffer_tracing_service( activity_ctx, ROCPROFILER_BUFFER_TRACING_CORRELATION_ID_RETIREMENT, nullptr, 0, activity_buf)); */ - auto external_corr_id_request_kinds = - make_array(ROCPROFILER_EXTERNAL_CORRELATION_REQUEST_KERNEL_DISPATCH, - ROCPROFILER_EXTERNAL_CORRELATION_REQUEST_MEMORY_COPY); - - ROCPROFILER_CALL( - rocprofiler_configure_external_correlation_id_request_service( - activity_ctx, - external_corr_id_request_kinds.data(), - external_corr_id_request_kinds.size(), - set_external_correlation_id, - nullptr)); + auto external_corr_id_request_kinds = make_array( + ROCPROFILER_EXTERNAL_CORRELATION_REQUEST_KERNEL_DISPATCH, + ROCPROFILER_EXTERNAL_CORRELATION_REQUEST_MEMORY_COPY + ); + + ROCPROFILER_CALL(rocprofiler_configure_external_correlation_id_request_service( + activity_ctx, + external_corr_id_request_kinds.data(), + external_corr_id_request_kinds.size(), + set_external_correlation_id, + nullptr + )); rocprofiler_query_available_agents_cb_t iterate_agents = [](rocprofiler_agent_version_t, const void** agents_arr, size_t num_agents, void*) { - for(size_t i = 0; i < num_agents; ++i) - { + for (size_t i = 0; i < num_agents; ++i) { const auto* agent_v = static_cast(agents_arr[i]); s_agents.emplace(agent_v->id.handle, agent_v); } return ROCPROFILER_STATUS_SUCCESS; }; - ROCPROFILER_CALL( - rocprofiler_query_available_agents( - ROCPROFILER_AGENT_INFO_VERSION_0, - iterate_agents, - sizeof(rocprofiler_agent_t), - nullptr)); + ROCPROFILER_CALL(rocprofiler_query_available_agents( + ROCPROFILER_AGENT_INFO_VERSION_0, + iterate_agents, + sizeof(rocprofiler_agent_t), + nullptr + )); // auto client_thread = rocprofiler_callback_thread_t{}; // ROCPROFILER_CALL( // rocprofiler_create_callback_thread(&client_thread)); @@ -473,56 +465,49 @@ class RocProfilerService return 0; } - static void tool_fini(void* tool_data) - { } + static void tool_fini(void* tool_data) {} - static void register_rocprofiler(Caliper* c, Channel* channel) { + static void register_rocprofiler(Caliper* c, Channel* channel) + { if (s_instance) { - Log(0).stream() << channel->name() - << ": rocprofiler service is already active, disabling!" - << std::endl; + Log(0).stream() << channel->name() << ": rocprofiler service is already active, disabling!" << std::endl; } s_instance = new RocProfilerService(c, channel); - channel->events().post_init_evt.connect( - [](Caliper* c, Channel* channel){ - s_instance->post_init_cb(c, channel); - }); - channel->events().pre_finish_evt.connect( - [](Caliper* c, Channel* channel){ - s_instance->pre_finish_cb(c, channel); - }); - channel->events().finish_evt.connect( - [](Caliper* c, Channel* channel){ - delete s_instance; - s_instance = nullptr; - }); - - Log(1).stream() << channel->name() - << ": Registered rocprofiler service." - << " Activity tracing is " - << (s_instance->m_enable_activity_tracing ? "on" : "off") + channel->events().post_init_evt.connect([](Caliper* c, Channel* channel) { + s_instance->post_init_cb(c, channel); + }); + channel->events().pre_finish_evt.connect([](Caliper* c, Channel* channel) { + s_instance->pre_finish_cb(c, channel); + }); + channel->events().finish_evt.connect([](Caliper* c, Channel* channel) { + delete s_instance; + s_instance = nullptr; + }); + + Log(1).stream() << channel->name() << ": Registered rocprofiler service." + << " Activity tracing is " << (s_instance->m_enable_activity_tracing ? "on" : "off") << std::endl; } - static void register_rocprofiler_as_roctracer(Caliper* c, Channel* channel) { + static void register_rocprofiler_as_roctracer(Caliper* c, Channel* channel) + { // a compatibility layer to convert roctracer options into rocprofiler options - Log(1).stream() << channel->name() - << ": rocprofiler: Using roctracer compatibility layer.\n"; + Log(1).stream() << channel->name() << ": rocprofiler: Using roctracer compatibility layer.\n"; auto config = services::init_config_from_spec(channel->config(), s_roctracer_spec); bool enable_activity_tracing = config.get("trace_activities").to_bool(); bool enable_snapshot_timestamps = - config.get("snapshot_duration").to_bool() || - config.get("snapshot_timestamps").to_bool(); + config.get("snapshot_duration").to_bool() || config.get("snapshot_timestamps").to_bool(); - channel->config().set("CALI_ROCPROFILER_ENABLE_ACTIVITY_TRACING", - enable_activity_tracing ? "true" : "false"); - channel->config().set("CALI_ROCPROFILER_ENABLE_SNAPSHOT_TIMESTAMPS", - enable_snapshot_timestamps ? "true" : "false"); + channel->config().set("CALI_ROCPROFILER_ENABLE_ACTIVITY_TRACING", enable_activity_tracing ? "true" : "false"); + channel->config().set( + "CALI_ROCPROFILER_ENABLE_SNAPSHOT_TIMESTAMPS", + enable_snapshot_timestamps ? "true" : "false" + ); register_rocprofiler(c, channel); } @@ -530,8 +515,8 @@ class RocProfilerService RocProfilerService* RocProfilerService::s_instance = nullptr; -rocprofiler_context_id_t RocProfilerService::hip_api_ctx = {}; -rocprofiler_context_id_t RocProfilerService::activity_ctx = {}; +rocprofiler_context_id_t RocProfilerService::hip_api_ctx = {}; +rocprofiler_context_id_t RocProfilerService::activity_ctx = {}; rocprofiler_context_id_t RocProfilerService::rocprofiler_ctx = {}; rocprofiler_buffer_id_t RocProfilerService::activity_buf = {}; @@ -593,26 +578,24 @@ const char* RocProfilerService::s_roctracer_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace extern "C" { -rocprofiler_tool_configure_result_t* -rocprofiler_configure(uint32_t version, - const char* runtime_version, - uint32_t priority, - rocprofiler_client_id_t* id) +rocprofiler_tool_configure_result_t* rocprofiler_configure( + uint32_t version, + const char* runtime_version, + uint32_t priority, + rocprofiler_client_id_t* id +) { id->name = "Caliper"; - static auto cfg = - rocprofiler_tool_configure_result_t { - sizeof(rocprofiler_tool_configure_result_t), - &RocProfilerService::tool_init, - &RocProfilerService::tool_fini, - nullptr - }; + static auto cfg = rocprofiler_tool_configure_result_t { sizeof(rocprofiler_tool_configure_result_t), + &RocProfilerService::tool_init, + &RocProfilerService::tool_fini, + nullptr }; return &cfg; } @@ -622,14 +605,9 @@ rocprofiler_configure(uint32_t version, namespace cali { -CaliperService rocprofiler_service -{ - ::RocProfilerService::s_spec, ::RocProfilerService::register_rocprofiler -}; +CaliperService rocprofiler_service { ::RocProfilerService::s_spec, ::RocProfilerService::register_rocprofiler }; -CaliperService roctracer_service -{ - ::RocProfilerService::s_roctracer_spec, ::RocProfilerService::register_rocprofiler_as_roctracer -}; +CaliperService roctracer_service { ::RocProfilerService::s_roctracer_spec, + ::RocProfilerService::register_rocprofiler_as_roctracer }; -} \ No newline at end of file +} // namespace cali \ No newline at end of file diff --git a/src/services/rocprofiler/RocTX.cpp b/src/services/rocprofiler/RocTX.cpp index d5745cf0f..1d7eaf634 100644 --- a/src/services/rocprofiler/RocTX.cpp +++ b/src/services/rocprofiler/RocTX.cpp @@ -21,15 +21,16 @@ namespace class RocTXBinding : public AnnotationBinding { - std::map< cali_id_t, std::vector > m_range_map; - std::mutex m_range_map_mutex; + std::map> m_range_map; + std::mutex m_range_map_mutex; unsigned m_num_stack_errors { 0 }; unsigned m_num_range_errors { 0 }; public: - void on_begin(Caliper*, Channel*, const Attribute &attr, const Variant& value) { + void on_begin(Caliper*, Channel*, const Attribute& attr, const Variant& value) + { const char* msg = nullptr; std::string str; // string obj must not be deleted until end of function @@ -44,34 +45,31 @@ class RocTXBinding : public AnnotationBinding if (attr.is_nested()) { roctxRangePush(msg); } else { - roctx_range_id_t roctx_id = - roctxRangeStart(msg); + roctx_range_id_t roctx_id = roctxRangeStart(msg); - std::lock_guard - g(m_range_map_mutex); + std::lock_guard g(m_range_map_mutex); m_range_map[attr.id()].push_back(roctx_id); } - } - void on_end(Caliper*, Channel*, const Attribute& attr, const Variant&) { + void on_end(Caliper*, Channel*, const Attribute& attr, const Variant&) + { if (attr.is_nested()) { if (roctxRangePop() < 0) ++m_num_stack_errors; } else { - bool found = false; + bool found = false; roctx_range_id_t roctx_id = 0; { - std::lock_guard - g(m_range_map_mutex); + std::lock_guard g(m_range_map_mutex); auto it = m_range_map.find(attr.id()); if (it != m_range_map.end() && it->second.size() > 0) { roctx_id = it->second.back(); - found = true; + found = true; it->second.pop_back(); } } @@ -83,16 +81,13 @@ class RocTXBinding : public AnnotationBinding } } - void finalize(Caliper*, Channel* channel) { + void finalize(Caliper*, Channel* channel) + { if (m_num_range_errors > 0) - Log(0).stream() << channel->name() << "roctx: " - << m_num_range_errors - << " range start/stop errors!" + Log(0).stream() << channel->name() << "roctx: " << m_num_range_errors << " range start/stop errors!" << std::endl; if (m_num_stack_errors > 0) - Log(0).stream() << channel->name() << "roctx: " - << m_num_stack_errors - << " region stack errors!" + Log(0).stream() << channel->name() << "roctx: " << m_num_stack_errors << " region stack errors!" << std::endl; } @@ -108,7 +103,7 @@ const char* RocTXBinding::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/roctracer/RocTracer.cpp b/src/services/roctracer/RocTracer.cpp index 729ca29f6..dbf6da10d 100644 --- a/src/services/roctracer/RocTracer.cpp +++ b/src/services/roctracer/RocTracer.cpp @@ -27,7 +27,8 @@ using namespace cali; namespace { -class RocTracerService { +class RocTracerService +{ Attribute m_api_attr; Attribute m_activity_start_attr; @@ -45,111 +46,100 @@ class RocTracerService { Attribute m_flush_region_attr; - unsigned m_num_records; - unsigned m_num_flushed; - unsigned m_num_flushes; + unsigned m_num_records; + unsigned m_num_flushed; + unsigned m_num_flushes; - unsigned m_num_correlations_stored; - unsigned m_num_correlations_found; - unsigned m_num_correlations_missed; + unsigned m_num_correlations_stored; + unsigned m_num_correlations_found; + unsigned m_num_correlations_missed; - std::mutex m_correlation_map_mutex; + std::mutex m_correlation_map_mutex; std::map m_correlation_map; roctracer_pool_t* m_roctracer_pool; - Channel m_channel; + Channel m_channel; - bool m_enable_tracing; - bool m_record_names; - bool m_record_host_duration; - bool m_record_host_timestamp; + bool m_enable_tracing; + bool m_record_names; + bool m_record_host_duration; + bool m_record_host_timestamp; static RocTracerService* s_instance; - void create_callback_attributes(Caliper* c) { + void create_callback_attributes(Caliper* c) + { Attribute subs_attr = c->get_attribute("subscription_event"); - Variant v_true(true); + Variant v_true(true); - m_api_attr = - c->create_attribute("rocm.api", CALI_TYPE_STRING, - CALI_ATTR_NESTED, - 1, &subs_attr, &v_true); + m_api_attr = c->create_attribute("rocm.api", CALI_TYPE_STRING, CALI_ATTR_NESTED, 1, &subs_attr, &v_true); } - void create_activity_attributes(Caliper* c) { + void create_activity_attributes(Caliper* c) + { m_activity_start_attr = - c->create_attribute("rocm.starttime", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + c->create_attribute("rocm.starttime", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); m_activity_end_attr = - c->create_attribute("rocm.endtime", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); - - m_activity_duration_attr = - c->create_attribute("rocm.activity.duration", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - - m_activity_name_attr = - c->create_attribute("rocm.activity", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - m_activity_queue_id_attr = - c->create_attribute("rocm.activity.queue", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_activity_device_id_attr = - c->create_attribute("rocm.activity.device", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_activity_bytes_attr = - c->create_attribute("rocm.activity.bytes", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); - m_kernel_name_attr = - c->create_attribute("rocm.kernel.name", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - - m_flush_region_attr = - c->create_attribute("roctracer.flush", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + c->create_attribute("rocm.endtime", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + + m_activity_duration_attr = c->create_attribute( + "rocm.activity.duration", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + + m_activity_name_attr = c->create_attribute("rocm.activity", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + m_activity_queue_id_attr = c->create_attribute("rocm.activity.queue", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_activity_device_id_attr = c->create_attribute("rocm.activity.device", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_activity_bytes_attr = c->create_attribute("rocm.activity.bytes", CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + m_kernel_name_attr = c->create_attribute("rocm.kernel.name", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + + m_flush_region_attr = c->create_attribute("roctracer.flush", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); } - void create_host_attributes(Caliper* c) { + void create_host_attributes(Caliper* c) + { m_host_starttime_attr = - c->create_attribute("rocm.host.starttime", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_PROCESS | - CALI_ATTR_SKIP_EVENTS); + c->create_attribute("rocm.host.starttime", CALI_TYPE_UINT, CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_SKIP_EVENTS); if (!(m_record_host_duration || m_record_host_timestamp)) return; int hide_offset = m_record_host_timestamp ? 0 : CALI_ATTR_HIDDEN; - m_host_timestamp_attr = - c->create_attribute("rocm.host.timestamp", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - hide_offset); + m_host_timestamp_attr = c->create_attribute( + "rocm.host.timestamp", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | hide_offset + ); if (m_record_host_duration) { - m_host_duration_attr = - c->create_attribute("rocm.host.duration", CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); + m_host_duration_attr = c->create_attribute( + "rocm.host.duration", + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); } } - void subscribe_attributes(Caliper* c, Channel* channel) { + void subscribe_attributes(Caliper* c, Channel* channel) + { channel->events().subscribe_attribute(c, channel, m_api_attr); } - void push_correlation(uint64_t id, cali::Node* node) { - std::lock_guard - g(m_correlation_map_mutex); + void push_correlation(uint64_t id, cali::Node* node) + { + std::lock_guard g(m_correlation_map_mutex); m_correlation_map[id] = node; } - cali::Node* pop_correlation(uint64_t id) { + cali::Node* pop_correlation(uint64_t id) + { cali::Node* ret = nullptr; - std::lock_guard - g(m_correlation_map_mutex); + std::lock_guard g(m_correlation_map_mutex); auto it = m_correlation_map.find(id); @@ -164,12 +154,11 @@ class RocTracerService { static void hip_api_callback(uint32_t domain, uint32_t cid, const void* callback_data, void* arg) { // skip unneeded callbacks - if (cid == HIP_API_ID___hipPushCallConfiguration || - cid == HIP_API_ID___hipPopCallConfiguration) + if (cid == HIP_API_ID___hipPushCallConfiguration || cid == HIP_API_ID___hipPopCallConfiguration) return; - auto instance = static_cast(arg); - auto data = static_cast(callback_data); + auto instance = static_cast(arg); + auto data = static_cast(callback_data); Caliper c; if (data->phase == ACTIVITY_API_PHASE_ENTER) { @@ -183,21 +172,23 @@ class RocTracerService { cali::Node* node = nullptr; switch (cid) { - case HIP_API_ID_hipLaunchKernel: - case HIP_API_ID_hipExtLaunchKernel: + case HIP_API_ID_hipLaunchKernel: + case HIP_API_ID_hipExtLaunchKernel: { Entry e = c.get(instance->m_api_attr); if (e.is_reference()) node = e.node(); if (instance->m_record_names) { - kernel = hipKernelNameRefByPtr(data->args.hipLaunchKernel.function_address, - data->args.hipLaunchKernel.stream); + kernel = hipKernelNameRefByPtr( + data->args.hipLaunchKernel.function_address, + data->args.hipLaunchKernel.stream + ); } } break; - case HIP_API_ID_hipModuleLaunchKernel: - case HIP_API_ID_hipExtModuleLaunchKernel: - case HIP_API_ID_hipHccModuleLaunchKernel: + case HIP_API_ID_hipModuleLaunchKernel: + case HIP_API_ID_hipExtModuleLaunchKernel: + case HIP_API_ID_hipHccModuleLaunchKernel: { Entry e = c.get(instance->m_api_attr); if (e.is_reference()) @@ -207,60 +198,58 @@ class RocTracerService { } } break; - case HIP_API_ID_hipMemcpy: - case HIP_API_ID_hipMemcpy2D: - case HIP_API_ID_hipMemcpy2DAsync: - case HIP_API_ID_hipMemcpy2DFromArray: - case HIP_API_ID_hipMemcpy2DFromArrayAsync: - case HIP_API_ID_hipMemcpy2DToArray: - case HIP_API_ID_hipMemcpy2DToArrayAsync: - case HIP_API_ID_hipMemcpy3D: - case HIP_API_ID_hipMemcpy3DAsync: - case HIP_API_ID_hipMemcpyAsync: - case HIP_API_ID_hipMemcpyAtoH: - case HIP_API_ID_hipMemcpyDtoD: - case HIP_API_ID_hipMemcpyDtoDAsync: - case HIP_API_ID_hipMemcpyDtoH: - case HIP_API_ID_hipMemcpyDtoHAsync: - case HIP_API_ID_hipMemcpyFromArray: - case HIP_API_ID_hipMemcpyFromSymbol: - case HIP_API_ID_hipMemcpyFromSymbolAsync: - case HIP_API_ID_hipMemcpyHtoA: - case HIP_API_ID_hipMemcpyHtoD: - case HIP_API_ID_hipMemcpyHtoDAsync: - case HIP_API_ID_hipMemcpyParam2D: - case HIP_API_ID_hipMemcpyParam2DAsync: - case HIP_API_ID_hipMemcpyPeer: - case HIP_API_ID_hipMemcpyPeerAsync: - case HIP_API_ID_hipMemcpyToArray: - case HIP_API_ID_hipMemcpyToSymbol: - case HIP_API_ID_hipMemcpyToSymbolAsync: - case HIP_API_ID_hipMemcpyWithStream: - case HIP_API_ID_hipMemset: - case HIP_API_ID_hipMemset2D: - case HIP_API_ID_hipMemset2DAsync: - case HIP_API_ID_hipMemset3D: - case HIP_API_ID_hipMemset3DAsync: - case HIP_API_ID_hipMemsetAsync: - case HIP_API_ID_hipMemsetD16: - case HIP_API_ID_hipMemsetD32: - case HIP_API_ID_hipMemsetD32Async: - case HIP_API_ID_hipMemsetD8: - case HIP_API_ID_hipMemsetD8Async: + case HIP_API_ID_hipMemcpy: + case HIP_API_ID_hipMemcpy2D: + case HIP_API_ID_hipMemcpy2DAsync: + case HIP_API_ID_hipMemcpy2DFromArray: + case HIP_API_ID_hipMemcpy2DFromArrayAsync: + case HIP_API_ID_hipMemcpy2DToArray: + case HIP_API_ID_hipMemcpy2DToArrayAsync: + case HIP_API_ID_hipMemcpy3D: + case HIP_API_ID_hipMemcpy3DAsync: + case HIP_API_ID_hipMemcpyAsync: + case HIP_API_ID_hipMemcpyAtoH: + case HIP_API_ID_hipMemcpyDtoD: + case HIP_API_ID_hipMemcpyDtoDAsync: + case HIP_API_ID_hipMemcpyDtoH: + case HIP_API_ID_hipMemcpyDtoHAsync: + case HIP_API_ID_hipMemcpyFromArray: + case HIP_API_ID_hipMemcpyFromSymbol: + case HIP_API_ID_hipMemcpyFromSymbolAsync: + case HIP_API_ID_hipMemcpyHtoA: + case HIP_API_ID_hipMemcpyHtoD: + case HIP_API_ID_hipMemcpyHtoDAsync: + case HIP_API_ID_hipMemcpyParam2D: + case HIP_API_ID_hipMemcpyParam2DAsync: + case HIP_API_ID_hipMemcpyPeer: + case HIP_API_ID_hipMemcpyPeerAsync: + case HIP_API_ID_hipMemcpyToArray: + case HIP_API_ID_hipMemcpyToSymbol: + case HIP_API_ID_hipMemcpyToSymbolAsync: + case HIP_API_ID_hipMemcpyWithStream: + case HIP_API_ID_hipMemset: + case HIP_API_ID_hipMemset2D: + case HIP_API_ID_hipMemset2DAsync: + case HIP_API_ID_hipMemset3D: + case HIP_API_ID_hipMemset3DAsync: + case HIP_API_ID_hipMemsetAsync: + case HIP_API_ID_hipMemsetD16: + case HIP_API_ID_hipMemsetD32: + case HIP_API_ID_hipMemsetD32Async: + case HIP_API_ID_hipMemsetD8: + case HIP_API_ID_hipMemsetD8Async: { Entry e = c.get(instance->m_api_attr); if (e.is_reference()) node = e.node(); } - default: + default: break; } if (!kernel.empty()) { kernel = util::demangle(kernel.c_str()); - node = c.make_tree_entry(instance->m_kernel_name_attr, - Variant(kernel.c_str()), - node); + node = c.make_tree_entry(instance->m_kernel_name_attr, Variant(kernel.c_str()), node); } if (node) { @@ -273,28 +262,21 @@ class RocTracerService { } } - unsigned flush_record(Caliper* c, const roctracer_record_t* record) { + unsigned flush_record(Caliper* c, const roctracer_record_t* record) + { unsigned num_records = 0; if (record->domain == ACTIVITY_DOMAIN_HIP_OPS || record->domain == ACTIVITY_DOMAIN_HCC_OPS) { - Attribute attr[7] = { - m_activity_name_attr, - m_activity_start_attr, - m_activity_end_attr, - m_activity_duration_attr, - m_activity_device_id_attr, - m_activity_queue_id_attr, - m_activity_bytes_attr - }; - Variant data[7] = { - Variant(roctracer_op_string(record->domain, record->op, record->kind)), - Variant(cali_make_variant_from_uint(record->begin_ns)), - Variant(cali_make_variant_from_uint(record->end_ns)), - Variant(cali_make_variant_from_uint(record->end_ns - record->begin_ns)), - Variant(cali_make_variant_from_uint(record->device_id)), - Variant(cali_make_variant_from_uint(record->queue_id)), - Variant() - }; + Attribute attr[7] = { m_activity_name_attr, m_activity_start_attr, m_activity_end_attr, + m_activity_duration_attr, m_activity_device_id_attr, m_activity_queue_id_attr, + m_activity_bytes_attr }; + Variant data[7] = { Variant(roctracer_op_string(record->domain, record->op, record->kind)), + Variant(cali_make_variant_from_uint(record->begin_ns)), + Variant(cali_make_variant_from_uint(record->end_ns)), + Variant(cali_make_variant_from_uint(record->end_ns - record->begin_ns)), + Variant(cali_make_variant_from_uint(record->device_id)), + Variant(cali_make_variant_from_uint(record->queue_id)), + Variant() }; size_t num = 6; @@ -319,16 +301,15 @@ class RocTracerService { return num_records; } - void flush_activity_records(Caliper* c, const char* begin, const char* end) { + void flush_activity_records(Caliper* c, const char* begin, const char* end) + { c->begin(m_flush_region_attr, Variant("ROCTRACER FLUSH")); unsigned num_flushed = 0; unsigned num_records = 0; - const roctracer_record_t* record = - reinterpret_cast(begin); - const roctracer_record_t* end_record = - reinterpret_cast(end); + const roctracer_record_t* record = reinterpret_cast(begin); + const roctracer_record_t* end_record = reinterpret_cast(end); while (record < end_record) { num_flushed += flush_record(c, record); @@ -339,10 +320,8 @@ class RocTracerService { } if (Log::verbosity() >= 2) { - Log(2).stream() << m_channel.name() << ": roctracer: Flushed " - << num_records << " records (" - << num_flushed << " flushed, " - << num_records - num_flushed << " skipped).\n"; + Log(2).stream() << m_channel.name() << ": roctracer: Flushed " << num_records << " records (" << num_flushed + << " flushed, " << num_records - num_flushed << " skipped).\n"; } m_num_flushed += num_flushed; @@ -352,20 +331,20 @@ class RocTracerService { c->end(m_flush_region_attr); } - void pre_flush_cb() { - roctracer_flush_activity_expl(m_roctracer_pool); - } + void pre_flush_cb() { roctracer_flush_activity_expl(m_roctracer_pool); } - void snapshot_cb(Caliper* c, Channel* channel, const SnapshotView, SnapshotBuilder& snapshot) { + void snapshot_cb(Caliper* c, Channel* channel, const SnapshotView, SnapshotBuilder& snapshot) + { uint64_t timestamp = 0; roctracer_get_timestamp(×tamp); - Variant v_now(cali_make_variant_from_uint(timestamp)); - Variant v_prev = c->exchange(m_host_timestamp_attr, v_now); + Variant v_now(cali_make_variant_from_uint(timestamp)); + Variant v_prev = c->exchange(m_host_timestamp_attr, v_now); if (m_record_host_duration) - snapshot.append(Entry(m_host_duration_attr, - Variant(cali_make_variant_from_uint(timestamp - v_prev.to_uint())))); + snapshot.append( + Entry(m_host_duration_attr, Variant(cali_make_variant_from_uint(timestamp - v_prev.to_uint()))) + ); } #if 0 @@ -386,22 +365,22 @@ class RocTracerService { } #endif - static void rt_activity_callback(const char* begin, const char* end, void* arg) { + static void rt_activity_callback(const char* begin, const char* end, void* arg) + { auto instance = static_cast(arg); Caliper c; instance->flush_activity_records(&c, begin, end); if (Log::verbosity() >= 2) { - unitfmt_result bytes = unitfmt(end-begin, unitfmt_bytes); - Log(2).stream() << instance->m_channel.name() - << ": roctracer: processed " - << bytes.val << bytes.symbol + unitfmt_result bytes = unitfmt(end - begin, unitfmt_bytes); + Log(2).stream() << instance->m_channel.name() << ": roctracer: processed " << bytes.val << bytes.symbol << " buffer" << std::endl; } } - void init_tracing(Channel* channel) { + void init_tracing(Channel* channel) + { roctracer_properties_t properties {}; memset(&properties, 0, sizeof(roctracer_properties_t)); @@ -412,46 +391,41 @@ class RocTracerService { properties.buffer_callback_arg = this; if (roctracer_open_pool_expl(&properties, &m_roctracer_pool) != 0) { - Log(0).stream() << channel->name() << ": roctracer: roctracer_open_pool_expl(): " - << roctracer_error_string() - << std::endl; + Log(0).stream() << channel->name() + << ": roctracer: roctracer_open_pool_expl(): " << roctracer_error_string() << std::endl; return; } if (roctracer_default_pool_expl(m_roctracer_pool) != 0) { - Log(0).stream() << channel->name() << ": roctracer: roctracer_default_pool_expl(): " - << roctracer_error_string() - << std::endl; + Log(0).stream() << channel->name() + << ": roctracer: roctracer_default_pool_expl(): " << roctracer_error_string() << std::endl; return; } if (roctracer_enable_domain_activity_expl(ACTIVITY_DOMAIN_HIP_OPS, m_roctracer_pool) != 0) { - Log(0).stream() << channel->name() << ": roctracer: roctracer_enable_activity_expl(): " - << roctracer_error_string() + Log(0).stream() << channel->name() + << ": roctracer: roctracer_enable_activity_expl(): " << roctracer_error_string() << std::endl; return; } if (roctracer_enable_domain_activity_expl(ACTIVITY_DOMAIN_HCC_OPS, m_roctracer_pool) != 0) { - Log(0).stream() << channel->name() << ": roctracer: roctracer_enable_activity_expl(): " - << roctracer_error_string() + Log(0).stream() << channel->name() + << ": roctracer: roctracer_enable_activity_expl(): " << roctracer_error_string() << std::endl; return; } - channel->events().pre_flush_evt.connect( - [this](Caliper*, Channel*, SnapshotView){ - this->pre_flush_cb(); - }); + channel->events().pre_flush_evt.connect([this](Caliper*, Channel*, SnapshotView) { this->pre_flush_cb(); }); Log(1).stream() << channel->name() << ": roctracer: Tracing initialized" << std::endl; } - void init_callbacks(Channel* channel) { + void init_callbacks(Channel* channel) + { roctracer_set_properties(ACTIVITY_DOMAIN_HIP_API, NULL); if (roctracer_enable_domain_callback(ACTIVITY_DOMAIN_HIP_API, RocTracerService::hip_api_callback, this) != 0) { - Log(0).stream() << channel->name() << ": roctracer: enable callback (HIP): " - << roctracer_error_string() + Log(0).stream() << channel->name() << ": roctracer: enable callback (HIP): " << roctracer_error_string() << std::endl; return; } @@ -459,7 +433,8 @@ class RocTracerService { Log(1).stream() << channel->name() << ": roctracer: Callbacks initialized" << std::endl; } - void finish_tracing(Channel* channel) { + void finish_tracing(Channel* channel) + { roctracer_disable_domain_activity(ACTIVITY_DOMAIN_HCC_OPS); roctracer_disable_domain_activity(ACTIVITY_DOMAIN_HIP_OPS); roctracer_close_pool_expl(m_roctracer_pool); @@ -468,13 +443,15 @@ class RocTracerService { Log(1).stream() << channel->name() << ": roctracer: Tracing stopped" << std::endl; } - void finish_callbacks(Channel* channel) { + void finish_callbacks(Channel* channel) + { roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HIP_API); Log(1).stream() << channel->name() << ": roctracer: Callbacks stopped" << std::endl; } - void post_init_cb(Caliper* c, Channel* channel) { + void post_init_cb(Caliper* c, Channel* channel) + { subscribe_attributes(c, channel); uint64_t starttime = 0; @@ -485,10 +462,9 @@ class RocTracerService { if (m_record_host_timestamp || m_record_host_duration) { c->set(m_host_timestamp_attr, cali_make_variant_from_uint(starttime)); - channel->events().snapshot.connect( - [](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec){ - s_instance->snapshot_cb(c, chn, info, rec); - }); + channel->events().snapshot.connect([](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec) { + s_instance->snapshot_cb(c, chn, info, rec); + }); } init_callbacks(channel); // apparently must happen before init_tracing() @@ -497,56 +473,53 @@ class RocTracerService { init_tracing(channel); } - void pre_finish_cb(Caliper* c, Channel* channel) { + void pre_finish_cb(Caliper* c, Channel* channel) + { finish_callbacks(channel); if (m_enable_tracing) finish_tracing(channel); } - void finish_cb(Caliper* c, Channel* channel) { + void finish_cb(Caliper* c, Channel* channel) + { if (m_enable_tracing) { - Log(1).stream() << channel->name() << ": roctracer: " - << m_num_flushes << " activity flushes, " - << m_num_records << " records processed, " - << m_num_flushed << " records flushed." - << std::endl; + Log(1).stream() << channel->name() << ": roctracer: " << m_num_flushes << " activity flushes, " + << m_num_records << " records processed, " << m_num_flushed << " records flushed." + << std::endl; if (Log::verbosity() >= 2) { - Log(2).stream() << channel->name() << ": roctracer: " - << m_num_correlations_stored << " correlations stored; " - << m_num_correlations_found << " correlations found, " - << m_num_correlations_missed << " missed." - << std::endl; + Log(2).stream() << channel->name() << ": roctracer: " << m_num_correlations_stored + << " correlations stored; " << m_num_correlations_found << " correlations found, " + << m_num_correlations_missed << " missed." << std::endl; } } } RocTracerService(Caliper* c, Channel* channel) - : m_num_records { 0 }, - m_num_flushed { 0 }, - m_num_flushes { 0 }, + : m_num_records { 0 }, + m_num_flushed { 0 }, + m_num_flushes { 0 }, m_num_correlations_stored { 0 }, - m_num_correlations_found { 0 }, + m_num_correlations_found { 0 }, m_num_correlations_missed { 0 }, m_roctracer_pool { nullptr }, - m_channel { *channel } + m_channel { *channel } { auto config = services::init_config_from_spec(channel->config(), s_spec); - m_enable_tracing = config.get("trace_activities").to_bool(); - m_record_names = config.get("record_kernel_names").to_bool(); - m_record_host_duration = - config.get("snapshot_duration").to_bool(); - m_record_host_timestamp = - config.get("snapshot_timestamps").to_bool(); + m_enable_tracing = config.get("trace_activities").to_bool(); + m_record_names = config.get("record_kernel_names").to_bool(); + m_record_host_duration = config.get("snapshot_duration").to_bool(); + m_record_host_timestamp = config.get("snapshot_timestamps").to_bool(); create_callback_attributes(c); create_activity_attributes(c); create_host_attributes(c); } - ~RocTracerService() { + ~RocTracerService() + { #if 0 for (char* buffer : m_trace_buffers) delete[] buffer; @@ -557,35 +530,28 @@ class RocTracerService { static const char* s_spec; - static void register_roctracer(Caliper* c, Channel* channel) { + static void register_roctracer(Caliper* c, Channel* channel) + { if (s_instance) { - Log(0).stream() << channel->name() - << ": roctracer service is already active, disabling!" - << std::endl; + Log(0).stream() << channel->name() << ": roctracer service is already active, disabling!" << std::endl; } s_instance = new RocTracerService(c, channel); - channel->events().post_init_evt.connect( - [](Caliper* c, Channel* channel){ - s_instance->post_init_cb(c, channel); - }); - channel->events().pre_finish_evt.connect( - [](Caliper* c, Channel* channel){ - s_instance->pre_finish_cb(c, channel); - }); - channel->events().finish_evt.connect( - [](Caliper* c, Channel* channel){ - s_instance->finish_cb(c, channel); - delete s_instance; - s_instance = nullptr; - }); - - Log(1).stream() << channel->name() - << ": Registered roctracer service." - << " Activity tracing is " - << (s_instance->m_enable_tracing ? "on" : "off") - << std::endl; + channel->events().post_init_evt.connect([](Caliper* c, Channel* channel) { + s_instance->post_init_cb(c, channel); + }); + channel->events().pre_finish_evt.connect([](Caliper* c, Channel* channel) { + s_instance->pre_finish_cb(c, channel); + }); + channel->events().finish_evt.connect([](Caliper* c, Channel* channel) { + s_instance->finish_cb(c, channel); + delete s_instance; + s_instance = nullptr; + }); + + Log(1).stream() << channel->name() << ": Registered roctracer service." + << " Activity tracing is " << (s_instance->m_enable_tracing ? "on" : "off") << std::endl; } }; @@ -619,7 +585,7 @@ const char* RocTracerService::s_spec = R"json( RocTracerService* RocTracerService::s_instance = nullptr; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/roctx/RocTX.cpp b/src/services/roctx/RocTX.cpp index 649aae443..d4d156375 100644 --- a/src/services/roctx/RocTX.cpp +++ b/src/services/roctx/RocTX.cpp @@ -21,15 +21,16 @@ namespace class RocTXBinding : public AnnotationBinding { - std::map< cali_id_t, std::vector > m_range_map; - std::mutex m_range_map_mutex; + std::map> m_range_map; + std::mutex m_range_map_mutex; unsigned m_num_stack_errors { 0 }; unsigned m_num_range_errors { 0 }; public: - void on_begin(Caliper*, Channel*, const Attribute &attr, const Variant& value) { + void on_begin(Caliper*, Channel*, const Attribute& attr, const Variant& value) + { const char* msg = nullptr; std::string str; // string obj must not be deleted until end of function @@ -44,34 +45,31 @@ class RocTXBinding : public AnnotationBinding if (attr.is_nested()) { roctxRangePush(msg); } else { - roctx_range_id_t roctx_id = - roctxRangeStart(msg); + roctx_range_id_t roctx_id = roctxRangeStart(msg); - std::lock_guard - g(m_range_map_mutex); + std::lock_guard g(m_range_map_mutex); m_range_map[attr.id()].push_back(roctx_id); } - } - void on_end(Caliper*, Channel*, const Attribute& attr, const Variant&) { + void on_end(Caliper*, Channel*, const Attribute& attr, const Variant&) + { if (attr.is_nested()) { if (roctxRangePop() < 0) ++m_num_stack_errors; } else { - bool found = false; + bool found = false; roctx_range_id_t roctx_id = 0; { - std::lock_guard - g(m_range_map_mutex); + std::lock_guard g(m_range_map_mutex); auto it = m_range_map.find(attr.id()); if (it != m_range_map.end() && it->second.size() > 0) { roctx_id = it->second.back(); - found = true; + found = true; it->second.pop_back(); } } @@ -83,23 +81,20 @@ class RocTXBinding : public AnnotationBinding } } - void finalize(Caliper*, Channel* channel) { + void finalize(Caliper*, Channel* channel) + { if (m_num_range_errors > 0) - Log(0).stream() << channel->name() << "roctx: " - << m_num_range_errors - << " range start/stop errors!" + Log(0).stream() << channel->name() << "roctx: " << m_num_range_errors << " range start/stop errors!" << std::endl; if (m_num_stack_errors > 0) - Log(0).stream() << channel->name() << "roctx: " - << m_num_stack_errors - << " region stack errors!" + Log(0).stream() << channel->name() << "roctx: " << m_num_stack_errors << " region stack errors!" << std::endl; } const char* service_tag() const { return "roctx"; } }; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/sampler/Sampler.cpp b/src/services/sampler/Sampler.cpp index 33e46540f..bfdaba360 100644 --- a/src/services/sampler/Sampler.cpp +++ b/src/services/sampler/Sampler.cpp @@ -43,12 +43,12 @@ Attribute timer_attr; Attribute sampler_attr; Attribute ucursor_attr; -int nsec_interval = 0; +int nsec_interval = 0; -int n_samples = 0; -int n_processed_samples = 0; +int n_samples = 0; +int n_processed_samples = 0; -Channel channel; +Channel channel; const char* spec = R"json( { "name": "sampler", @@ -63,7 +63,7 @@ const char* spec = R"json( } )json"; -void on_prof(int sig, siginfo_t *info, void *context) +void on_prof(int sig, siginfo_t* info, void* context) { ++n_samples; @@ -72,12 +72,12 @@ void on_prof(int sig, siginfo_t *info, void *context) if (!c) return; - Entry data[2]; + Entry data[2]; unsigned count = 0; #ifdef CALI_SAMPLER_GET_PC - uint64_t pc = static_cast( CALI_SAMPLER_GET_PC(context) ); - Variant v_pc(CALI_TYPE_ADDR, &pc, sizeof(uint64_t)); + uint64_t pc = static_cast(CALI_SAMPLER_GET_PC(context)); + Variant v_pc(CALI_TYPE_ADDR, &pc, sizeof(uint64_t)); data[count++] = Entry(sampler_attr, v_pc); #endif @@ -122,8 +122,7 @@ void clear_signal() signal(SIGPROF, SIG_IGN); } -struct TimerWrap -{ +struct TimerWrap { timer_t timer; }; @@ -133,7 +132,7 @@ void setup_settimer(Caliper* c) std::memset(&sev, 0, sizeof(sev)); - sev.sigev_notify = SIGEV_THREAD_ID; // Linux-specific! + sev.sigev_notify = SIGEV_THREAD_ID; // Linux-specific! sev._sigev_un._tid = syscall(SYS_gettid); sev.sigev_signo = SIGPROF; @@ -162,7 +161,8 @@ void setup_settimer(Caliper* c) Log(2).stream() << "Sampler: Registered timer " << v_timer << endl; } -void clear_timer(Caliper* c, Channel* chn) { +void clear_timer(Caliper* c, Channel* chn) +{ Entry e = c->get(timer_attr); if (e.empty()) { @@ -181,26 +181,28 @@ void clear_timer(Caliper* c, Channel* chn) { delete twrap; } -void create_thread_cb(Caliper* c, Channel* chn) { +void create_thread_cb(Caliper* c, Channel* chn) +{ setup_settimer(c); } -void release_thread_cb(Caliper* c, Channel* chn) { +void release_thread_cb(Caliper* c, Channel* chn) +{ clear_timer(c, chn); } -void pre_finish_cb(Caliper* c, Channel* chn) { +void pre_finish_cb(Caliper* c, Channel* chn) +{ clear_timer(c, chn); clear_signal(); } -void finish_cb(Caliper* c, Channel* chn) { - Log(1).stream() << chn->name() - << ": Sampler: processed " << n_processed_samples << " samples (" - << n_samples << " total, " - << n_samples - n_processed_samples << " dropped)." << endl; +void finish_cb(Caliper* c, Channel* chn) +{ + Log(1).stream() << chn->name() << ": Sampler: processed " << n_processed_samples << " samples (" << n_samples + << " total, " << n_samples - n_processed_samples << " dropped)." << endl; - n_samples = 0; + n_samples = 0; n_processed_samples = 0; channel = Channel(); @@ -210,34 +212,33 @@ void sampler_register(Caliper* c, Channel* chn) { if (channel) { Log(0).stream() << chn->name() << ": Sampler: Cannot enable sampler service twice!" - << " It is already enabled in channel " - << channel.name() << std::endl; + << " It is already enabled in channel " << channel.name() << std::endl; return; } ConfigSet config = services::init_config_from_spec(chn->config(), spec); Attribute symbol_class_attr = c->get_attribute("class.symboladdress"); - Variant v_true(true); - - timer_attr = - c->create_attribute(std::string("cali.sampler.timer.")+std::to_string(chn->id()), CALI_TYPE_PTR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN); - sampler_attr = - c->create_attribute("cali.sampler.pc", CALI_TYPE_ADDR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE, - 1, &symbol_class_attr, &v_true); - ucursor_attr = - c->create_attribute("cali.unw_cursor", CALI_TYPE_PTR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN); + Variant v_true(true); + + timer_attr = c->create_attribute( + std::string("cali.sampler.timer.") + std::to_string(chn->id()), + CALI_TYPE_PTR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); + sampler_attr = c->create_attribute( + "cali.sampler.pc", + CALI_TYPE_ADDR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE, + 1, + &symbol_class_attr, + &v_true + ); + ucursor_attr = c->create_attribute( + "cali.unw_cursor", + CALI_TYPE_PTR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN + ); int frequency = config.get("frequency").to_int(); @@ -245,8 +246,7 @@ void sampler_register(Caliper* c, Channel* chn) frequency = std::min(std::max(frequency, 1), 10000); nsec_interval = 1000000000 / frequency; - c->set(chn, c->create_attribute("sample.frequency", CALI_TYPE_INT, CALI_ATTR_GLOBAL), - Variant(frequency)); + c->set(chn, c->create_attribute("sample.frequency", CALI_TYPE_INT, CALI_ATTR_GLOBAL), Variant(frequency)); chn->events().create_thread_evt.connect(create_thread_cb); chn->events().release_thread_evt.connect(release_thread_cb); @@ -258,8 +258,8 @@ void sampler_register(Caliper* c, Channel* chn) setup_signal(); setup_settimer(c); - Log(1).stream() << chn->name() << ": Registered sampler service. Using " - << frequency << "Hz sampling frequency." << endl; + Log(1).stream() << chn->name() << ": Registered sampler service. Using " << frequency << "Hz sampling frequency." + << endl; } } // namespace diff --git a/src/services/sampler/context_ppc.h b/src/services/sampler/context_ppc.h index 48c1f1368..ef4c954b6 100644 --- a/src/services/sampler/context_ppc.h +++ b/src/services/sampler/context_ppc.h @@ -5,7 +5,7 @@ #define _PPC_REG_PC 32 #if __WORDSIZE == 32 -#define CALI_SAMPLER_GET_PC(ctx) ( ((ucontext_t*) (ctx))->uc_mcontext.uc_regs->gregs[_PPC_REG_PC] ) +#define CALI_SAMPLER_GET_PC(ctx) (((ucontext_t*) (ctx))->uc_mcontext.uc_regs->gregs[_PPC_REG_PC]) #else -#define CALI_SAMPLER_GET_PC(ctx) ( ((ucontext_t*) (ctx))->uc_mcontext.gp_regs[_PPC_REG_PC] ) +#define CALI_SAMPLER_GET_PC(ctx) (((ucontext_t*) (ctx))->uc_mcontext.gp_regs[_PPC_REG_PC]) #endif diff --git a/src/services/statistics/Statistics.cpp b/src/services/statistics/Statistics.cpp index 1d2b933e5..e99617c65 100644 --- a/src/services/statistics/Statistics.cpp +++ b/src/services/statistics/Statistics.cpp @@ -34,25 +34,25 @@ class Statistics unsigned num_threads; unsigned max_threads; - void finish_cb(Caliper* c, Channel* chn) { + void finish_cb(Caliper* c, Channel* chn) + { Log(1).stream() << chn->name() << ": statistics:" << "\n Number of begin events: " << num_begin.load() << "\n Number of end events: " << num_end.load() << "\n Number of set events: " << num_set.load() << "\n Number of snapshots: " << num_snapshots.load() - << "\n Max snapshot entries: " << max_snapshot_len - << std::endl; + << "\n Max snapshot entries: " << max_snapshot_len << std::endl; if (chn->id() == 0) { // only print this for the default channel auto vec = c->get_all_attributes(); - unsigned n_global = 0; + unsigned n_global = 0; unsigned n_hidden_ref = 0; unsigned n_hidden_val = 0; - unsigned n_val = 0; - unsigned n_ref = 0; + unsigned n_val = 0; + unsigned n_ref = 0; for (const Attribute& attr : vec) { if (attr.is_hidden()) { @@ -68,81 +68,62 @@ class Statistics continue; } - if(attr.store_as_value()) + if (attr.store_as_value()) ++n_val; else ++n_ref; } Log(1).stream() << "Global statistics:" - << "\n Number of attributes: " << vec.size() - << "\n reference: " << n_ref - << "\n value: " << n_val - << "\n global: " << n_global - << "\n hidden: " << (n_hidden_ref + n_hidden_val) - << " (" << n_hidden_ref << " reference, " << n_hidden_val << " value)" - << "\n Number of threads: " << num_threads - << " (max " << max_threads << ")" << std::endl; + << "\n Number of attributes: " << vec.size() << "\n reference: " << n_ref + << "\n value: " << n_val << "\n global: " << n_global + << "\n hidden: " << (n_hidden_ref + n_hidden_val) << " (" << n_hidden_ref + << " reference, " << n_hidden_val << " value)" + << "\n Number of threads: " << num_threads << " (max " << max_threads << ")" + << std::endl; } } Statistics() - : num_snapshots(0), - num_begin(0), - num_end(0), - num_set(0), - max_snapshot_len(0), - num_threads(1), - max_threads(1) - { } + : num_snapshots(0), num_begin(0), num_end(0), num_set(0), max_snapshot_len(0), num_threads(1), max_threads(1) + {} public: - static void statistics_service_register(Caliper* c, Channel* chn) { + static void statistics_service_register(Caliper* c, Channel* chn) + { Statistics* instance = new Statistics; - chn->events().pre_begin_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute&, const Variant&){ - ++instance->num_begin; - }); - chn->events().pre_end_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute&, const Variant&){ - ++instance->num_end; - }); - chn->events().pre_set_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute&, const Variant&){ - ++instance->num_set; - }); - chn->events().snapshot.connect( - [instance](Caliper*,Channel*,SnapshotView,SnapshotBuilder& rec){ - ++instance->num_snapshots; - }); - chn->events().process_snapshot.connect( - [instance](Caliper*,Channel*,SnapshotView,SnapshotView rec){ - std::lock_guard - g(instance->lock); - - instance->max_snapshot_len = std::max(rec.size(), instance->max_snapshot_len); - }); + chn->events().pre_begin_evt.connect([instance](Caliper* c, Channel* chn, const Attribute&, const Variant&) { + ++instance->num_begin; + }); + chn->events().pre_end_evt.connect([instance](Caliper* c, Channel* chn, const Attribute&, const Variant&) { + ++instance->num_end; + }); + chn->events().pre_set_evt.connect([instance](Caliper* c, Channel* chn, const Attribute&, const Variant&) { + ++instance->num_set; + }); + chn->events().snapshot.connect([instance](Caliper*, Channel*, SnapshotView, SnapshotBuilder& rec) { + ++instance->num_snapshots; + }); + chn->events().process_snapshot.connect([instance](Caliper*, Channel*, SnapshotView, SnapshotView rec) { + std::lock_guard g(instance->lock); + + instance->max_snapshot_len = std::max(rec.size(), instance->max_snapshot_len); + }); if (chn->id() == 0) { - chn->events().create_thread_evt.connect( - [instance](Caliper* c, Channel* chn){ - ++instance->num_threads; - instance->max_threads = - std::max(instance->num_threads, instance->max_threads); - }); - chn->events().release_thread_evt.connect( - [instance](Caliper* c, Channel* chn){ - --instance->num_threads; - }); + chn->events().create_thread_evt.connect([instance](Caliper* c, Channel* chn) { + ++instance->num_threads; + instance->max_threads = std::max(instance->num_threads, instance->max_threads); + }); + chn->events().release_thread_evt.connect([instance](Caliper* c, Channel* chn) { --instance->num_threads; }); } - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finish_cb(c, chn); - delete instance; - }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finish_cb(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered statistics service" << std::endl; } diff --git a/src/services/symbollookup/Lookup.h b/src/services/symbollookup/Lookup.h index 0a62bb127..cd3bbf130 100644 --- a/src/services/symbollookup/Lookup.h +++ b/src/services/symbollookup/Lookup.h @@ -15,22 +15,21 @@ namespace cali namespace symbollookup { -class Lookup { +class Lookup +{ struct LookupImpl; std::unique_ptr mP; public: - enum Kind { - Name = 1, File = 2, Line = 4, Module = 8 - }; + enum Kind { Name = 1, File = 2, Line = 4, Module = 8 }; struct Result { std::string name; std::string file; - int line; + int line; std::string module; - bool success; + bool success; }; Result lookup(uint64_t address, int what) const; diff --git a/src/services/symbollookup/LookupLibdw.cpp b/src/services/symbollookup/LookupLibdw.cpp index 461d3576b..6b827e5e3 100644 --- a/src/services/symbollookup/LookupLibdw.cpp +++ b/src/services/symbollookup/LookupLibdw.cpp @@ -22,34 +22,32 @@ Dwfl_Callbacks* get_dwfl_callbacks() { static char* debuginfopath = nullptr; - static bool initialized = false; + static bool initialized = false; static Dwfl_Callbacks callbacks; if (!initialized) { - callbacks.find_elf = dwfl_linux_proc_find_elf; + callbacks.find_elf = dwfl_linux_proc_find_elf; callbacks.find_debuginfo = dwfl_standard_find_debuginfo; callbacks.debuginfo_path = &debuginfopath; - initialized = true; + initialized = true; } return &callbacks; } -} // namespace [anonymous] +} // namespace - -struct Lookup::LookupImpl -{ +struct Lookup::LookupImpl { Dwfl* dwfl { nullptr }; - Lookup::Result - lookup(uintptr_t address, int what) { + Lookup::Result lookup(uintptr_t address, int what) + { Result result { "UNKNOWN", "UNKNOWN", 0, "UNKNOWN", false }; if (!dwfl) return result; - Dwfl_Module *mod = dwfl_addrmodule (dwfl, address); + Dwfl_Module* mod = dwfl_addrmodule(dwfl, address); if (!mod) return result; @@ -60,10 +58,10 @@ struct Lookup::LookupImpl result.name = util::demangle(dwfl_module_addrname(mod, address)); if (what & Kind::File || what & Kind::Line) { - Dwfl_Line *line = dwfl_module_getsrc(mod, address); + Dwfl_Line* line = dwfl_module_getsrc(mod, address); if (line) { - int lineno, linecol; + int lineno, linecol; const char* src = dwfl_lineinfo(line, &address, &lineno, &linecol, nullptr, nullptr); if (src) { @@ -83,14 +81,14 @@ struct Lookup::LookupImpl return result; } - LookupImpl() { + LookupImpl() + { Log(2).stream() << "symbollookup: Loading debug info" << std::endl; dwfl = dwfl_begin(get_dwfl_callbacks()); if (dwfl_linux_proc_report(dwfl, getpid()) != 0) { - Log(0).stream() << "symbollookup: dwfl_linux_proc_report() error: " - << dwfl_errmsg(dwfl_errno()) + Log(0).stream() << "symbollookup: dwfl_linux_proc_report() error: " << dwfl_errmsg(dwfl_errno()) << std::endl; dwfl_end(dwfl); dwfl = nullptr; @@ -98,31 +96,22 @@ struct Lookup::LookupImpl } if (dwfl_report_end(dwfl, nullptr, nullptr) != 0) { - Log(0).stream() << "symbollookup: dwfl_report_end() error: " - << dwfl_errmsg(dwfl_errno()) - << std::endl; + Log(0).stream() << "symbollookup: dwfl_report_end() error: " << dwfl_errmsg(dwfl_errno()) << std::endl; dwfl_end(dwfl); dwfl = nullptr; } } - ~LookupImpl() { - dwfl_end(dwfl); - } + ~LookupImpl() { dwfl_end(dwfl); } }; - -Lookup::Result -Lookup::lookup(uint64_t address, int what) const +Lookup::Result Lookup::lookup(uint64_t address, int what) const { return mP->lookup(static_cast(address), what); } -Lookup::Lookup() - : mP(new LookupImpl) -{ -} +Lookup::Lookup() : mP(new LookupImpl) +{} Lookup::~Lookup() -{ -} +{} diff --git a/src/services/symbollookup/SymbolLookup.cpp b/src/services/symbollookup/SymbolLookup.cpp index 199c887fb..d8acc8fd0 100644 --- a/src/services/symbollookup/SymbolLookup.cpp +++ b/src/services/symbollookup/SymbolLookup.cpp @@ -41,7 +41,7 @@ class SymbolLookup Attribute sym_node_attr; std::unordered_map lookup_cache; - std::mutex lookup_cache_mutex; + std::mutex lookup_cache_mutex; }; Node m_root_node; @@ -53,11 +53,11 @@ class SymbolLookup bool m_lookup_mod; std::map> m_sym_attr_map; - std::mutex m_sym_attr_mutex; + std::mutex m_sym_attr_mutex; std::vector m_addr_attr_names; - Lookup m_lookup; + Lookup m_lookup; unsigned m_num_lookups; unsigned m_num_cached; @@ -67,10 +67,10 @@ class SymbolLookup // --- methods // - void make_symbol_attributes(Caliper* c, const Attribute& attr) { + void make_symbol_attributes(Caliper* c, const Attribute& attr) + { { - std::lock_guard - g(m_sym_attr_mutex); + std::lock_guard g(m_sym_attr_mutex); if (m_sym_attr_map.count(attr) > 0) return; @@ -80,31 +80,27 @@ class SymbolLookup sym_attribs->target_attr = attr; sym_attribs->file_attr = - c->create_attribute("source.file#" + attr.name(), - CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + c->create_attribute("source.file#" + attr.name(), CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); sym_attribs->line_attr = - c->create_attribute("source.line#" + attr.name(), - CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); + c->create_attribute("source.line#" + attr.name(), CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS); sym_attribs->func_attr = - c->create_attribute("source.function#" + attr.name(), - CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - sym_attribs->loc_attr = - c->create_attribute("sourceloc#" + attr.name(), - CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - sym_attribs->mod_attr = - c->create_attribute("module#" + attr.name(), - CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - sym_attribs->sym_node_attr = - c->create_attribute("symbollookup.node#" + attr.name(), - CALI_TYPE_UINT, CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN); - - std::lock_guard - g(m_sym_attr_mutex); + c->create_attribute("source.function#" + attr.name(), CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + sym_attribs->loc_attr = + c->create_attribute("sourceloc#" + attr.name(), CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + sym_attribs->mod_attr = c->create_attribute("module#" + attr.name(), CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + sym_attribs->sym_node_attr = c->create_attribute( + "symbollookup.node#" + attr.name(), + CALI_TYPE_UINT, + CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN + ); + + std::lock_guard g(m_sym_attr_mutex); m_sym_attr_map.insert(std::make_pair(attr, sym_attribs)); } - void check_attributes(Caliper* c) { + void check_attributes(Caliper* c) + { std::vector vec; if (m_addr_attr_names.empty()) { @@ -116,20 +112,19 @@ class SymbolLookup if (attr) vec.push_back(attr); else - Log(0).stream() << "Symbollookup: Address attribute \"" - << s << "\" not found!" << std::endl; + Log(0).stream() << "Symbollookup: Address attribute \"" << s << "\" not found!" << std::endl; } } if (vec.empty()) - Log(1).stream() << "Symbollookup: No address attributes found." - << std::endl; + Log(1).stream() << "Symbollookup: No address attributes found." << std::endl; for (const Attribute& a : vec) make_symbol_attributes(c, a); } - Node* perform_lookup(Caliper* c, Entry e, SymbolAttributeInfo& sym_info, Node* parent) { + Node* perform_lookup(Caliper* c, Entry e, SymbolAttributeInfo& sym_info, Node* parent) + { if (e.empty()) return nullptr; @@ -138,8 +133,7 @@ class SymbolLookup uintptr_t addr = e.value().to_uint(); { - std::lock_guard - g(sym_info.lookup_cache_mutex); + std::lock_guard g(sym_info.lookup_cache_mutex); auto it = sym_info.lookup_cache.find(addr); if (it != sym_info.lookup_cache.end()) { @@ -167,7 +161,7 @@ class SymbolLookup // We go from coarse grained to fine grained info here to speed up tree creation if (m_lookup_mod) - node = c->make_tree_entry(sym_info.mod_attr, Variant(result.module.c_str()), node); + node = c->make_tree_entry(sym_info.mod_attr, Variant(result.module.c_str()), node); if (m_lookup_file) node = c->make_tree_entry(sym_info.file_attr, Variant(result.file.c_str()), node); if (m_lookup_functions) @@ -176,22 +170,22 @@ class SymbolLookup node = c->make_tree_entry(sym_info.line_attr, Variant(static_cast(result.line)), node); if (m_lookup_sourceloc) { std::string tmp = result.file + ":" + std::to_string(result.line); - node = c->make_tree_entry(sym_info.loc_attr, Variant(tmp.c_str()), node); + node = c->make_tree_entry(sym_info.loc_attr, Variant(tmp.c_str()), node); } if (node == parent) return nullptr; { - std::lock_guard - g(sym_info.lookup_cache_mutex); + std::lock_guard g(sym_info.lookup_cache_mutex); sym_info.lookup_cache[addr] = node; } return node; } - Node* find_symbol_node_entry(Caliper* c, Node* node, Attribute sym_node_attr) { + Node* find_symbol_node_entry(Caliper* c, Node* node, Attribute sym_node_attr) + { for (node = node->first_child(); node; node = node->next_sibling()) if (node->attribute() == sym_node_attr.id()) return c->node(node->data().to_uint()); @@ -199,7 +193,8 @@ class SymbolLookup return nullptr; } - Entry get_symbol_entry(Caliper* c, Entry e, SymbolAttributeInfo& sym_info) { + Entry get_symbol_entry(Caliper* c, Entry e, SymbolAttributeInfo& sym_info) + { e = e.get(sym_info.target_attr); if (e.is_reference()) { @@ -209,7 +204,7 @@ class SymbolLookup return Entry(sym_node); Entry parent = get_symbol_entry(c, Entry(e.node()->parent()), sym_info); - sym_node = perform_lookup(c, e, sym_info, parent.empty() ? &m_root_node : parent.node()); + sym_node = perform_lookup(c, e, sym_info, parent.empty() ? &m_root_node : parent.node()); if (sym_node) c->make_tree_entry(sym_info.sym_node_attr, Variant(sym_node->id()), e.node()); @@ -222,7 +217,8 @@ class SymbolLookup return e; } - void process_snapshot(Caliper* c, std::vector& rec) { + void process_snapshot(Caliper* c, std::vector& rec) + { if (m_sym_attr_map.empty()) return; @@ -231,8 +227,7 @@ class SymbolLookup std::map> sym_map; { - std::lock_guard - g(m_sym_attr_mutex); + std::lock_guard g(m_sym_attr_mutex); sym_map = m_sym_attr_map; } @@ -242,7 +237,7 @@ class SymbolLookup for (auto& it : sym_map) { SnapshotView v(rec.size(), rec.data()); - Entry e = get_symbol_entry(c, v.get(it.first), *(it.second)); + Entry e = get_symbol_entry(c, v.get(it.first), *(it.second)); if (!e.empty()) result.push_back(e); @@ -252,57 +247,51 @@ class SymbolLookup } // some final log output; print warning if we didn't find an address attribute - void finish_log(Caliper* c, Channel* chn) { - Log(1).stream() << chn->name() << ": Symbollookup: Performed " - << m_num_lookups << " address lookups, " - << m_num_cached << " cached, " - << m_num_failed << " failed." - << std::endl; + void finish_log(Caliper* c, Channel* chn) + { + Log(1).stream() << chn->name() << ": Symbollookup: Performed " << m_num_lookups << " address lookups, " + << m_num_cached << " cached, " << m_num_failed << " failed." << std::endl; } - void init_lookup() { + void init_lookup() + { m_num_lookups = 0; m_num_failed = 0; } SymbolLookup(Caliper* c, Channel* chn) - : m_root_node(CALI_INV_ID, CALI_INV_ID, Variant()), - m_num_lookups(0), - m_num_cached(0), - m_num_failed(0) - { - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); + : m_root_node(CALI_INV_ID, CALI_INV_ID, Variant()), m_num_lookups(0), m_num_cached(0), m_num_failed(0) + { + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - m_addr_attr_names = config.get("attributes").to_stringlist(",:"); + m_addr_attr_names = config.get("attributes").to_stringlist(",:"); - m_lookup_functions = config.get("lookup_functions").to_bool(); - m_lookup_sourceloc = config.get("lookup_sourceloc").to_bool(); - m_lookup_file = config.get("lookup_file").to_bool(); - m_lookup_line = config.get("lookup_line").to_bool(); - m_lookup_mod = config.get("lookup_module").to_bool(); - } + m_lookup_functions = config.get("lookup_functions").to_bool(); + m_lookup_sourceloc = config.get("lookup_sourceloc").to_bool(); + m_lookup_file = config.get("lookup_file").to_bool(); + m_lookup_line = config.get("lookup_line").to_bool(); + m_lookup_mod = config.get("lookup_module").to_bool(); + } public: static const char* s_spec; - static void symbollookup_register(Caliper* c, Channel* chn) { + static void symbollookup_register(Caliper* c, Channel* chn) + { SymbolLookup* instance = new SymbolLookup(c, chn); - chn->events().pre_flush_evt.connect( - [instance](Caliper* c, Channel* chn, SnapshotView){ - instance->check_attributes(c); - instance->init_lookup(); - }); - chn->events().postprocess_snapshot.connect( - [instance](Caliper* c, Channel* chn, std::vector& rec){ - instance->process_snapshot(c, rec); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finish_log(c, chn); - delete instance; - }); + chn->events().pre_flush_evt.connect([instance](Caliper* c, Channel* chn, SnapshotView) { + instance->check_attributes(c); + instance->init_lookup(); + }); + chn->events().postprocess_snapshot.connect([instance](Caliper* c, Channel* chn, std::vector& rec) { + instance->process_snapshot(c, rec); + }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finish_log(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered symbollookup service" << std::endl; } @@ -346,8 +335,7 @@ const char* SymbolLookup::s_spec = R"json( } )json"; -} // namespace [anonymous] - +} // namespace namespace cali { diff --git a/src/services/sysalloc/SysAllocService.cpp b/src/services/sysalloc/SysAllocService.cpp index ea27f71a8..94441b7e3 100644 --- a/src/services/sysalloc/SysAllocService.cpp +++ b/src/services/sysalloc/SysAllocService.cpp @@ -30,22 +30,20 @@ gotcha_wrappee_handle_t orig_free_handle = 0x0; void* cali_malloc_wrapper(size_t size); void* cali_calloc_wrapper(size_t num, size_t size); -void* cali_realloc_wrapper(void *ptr, size_t size); -void cali_free_wrapper(void *ptr); +void* cali_realloc_wrapper(void* ptr, size_t size); +void cali_free_wrapper(void* ptr); -const char *malloc_str = "malloc"; -const char *calloc_str = "calloc"; -const char *realloc_str = "realloc"; -const char *free_str = "free"; +const char* malloc_str = "malloc"; +const char* calloc_str = "calloc"; +const char* realloc_str = "realloc"; +const char* free_str = "free"; bool bindings_are_active = false; -struct gotcha_binding_t alloc_bindings[] = { - { malloc_str, (void*) cali_malloc_wrapper, &orig_malloc_handle }, - { calloc_str, (void*) cali_calloc_wrapper, &orig_calloc_handle }, - { realloc_str, (void*) cali_realloc_wrapper, &orig_realloc_handle }, - { free_str, (void*) cali_free_wrapper, &orig_free_handle } -}; +struct gotcha_binding_t alloc_bindings[] = { { malloc_str, (void*) cali_malloc_wrapper, &orig_malloc_handle }, + { calloc_str, (void*) cali_calloc_wrapper, &orig_calloc_handle }, + { realloc_str, (void*) cali_realloc_wrapper, &orig_realloc_handle }, + { free_str, (void*) cali_free_wrapper, &orig_free_handle } }; ChannelList* sysalloc_channels = nullptr; @@ -54,7 +52,7 @@ void* cali_malloc_wrapper(size_t size) decltype(&std::malloc) orig_malloc = reinterpret_cast(gotcha_get_wrappee(orig_malloc_handle)); - void *ret = (*orig_malloc)(size); + void* ret = (*orig_malloc)(size); int saved_errno = errno; @@ -75,7 +73,7 @@ void* cali_calloc_wrapper(size_t num, size_t size) decltype(&std::calloc) orig_calloc = reinterpret_cast(gotcha_get_wrappee(orig_calloc_handle)); - void *ret = (*orig_calloc)(num, size); + void* ret = (*orig_calloc)(num, size); int saved_errno = errno; @@ -91,7 +89,7 @@ void* cali_calloc_wrapper(size_t num, size_t size) return ret; } -void* cali_realloc_wrapper(void *ptr, size_t size) +void* cali_realloc_wrapper(void* ptr, size_t size) { decltype(&std::realloc) orig_realloc = reinterpret_cast(gotcha_get_wrappee(orig_realloc_handle)); @@ -103,7 +101,7 @@ void* cali_realloc_wrapper(void *ptr, size_t size) c.memory_region_end(&p->channel, ptr); } - void *ret = (*orig_realloc)(ptr, size); + void* ret = (*orig_realloc)(ptr, size); int saved_errno = errno; @@ -119,10 +117,9 @@ void* cali_realloc_wrapper(void *ptr, size_t size) return ret; } -void cali_free_wrapper(void *ptr) +void cali_free_wrapper(void* ptr) { - decltype(&std::free) orig_free = - reinterpret_cast(gotcha_get_wrappee(orig_free_handle)); + decltype(&std::free) orig_free = reinterpret_cast(gotcha_get_wrappee(orig_free_handle)); for (ChannelList* p = sysalloc_channels; p; p = p->next) { Caliper c = Caliper::sigsafe_instance(); @@ -134,13 +131,11 @@ void cali_free_wrapper(void *ptr) (*orig_free)(ptr); } - -void init_alloc_hooks() { +void init_alloc_hooks() +{ Log(1).stream() << "sysalloc: Initializing system alloc hooks" << std::endl; - gotcha_wrap(alloc_bindings, - sizeof(alloc_bindings)/sizeof(struct gotcha_binding_t), - "caliper/sysalloc"); + gotcha_wrap(alloc_bindings, sizeof(alloc_bindings) / sizeof(struct gotcha_binding_t), "caliper/sysalloc"); bindings_are_active = true; } @@ -170,26 +165,24 @@ void clear_alloc_hooks() } #endif -void sysalloc_initialize(Caliper* c, Channel* chn) { - chn->events().post_init_evt.connect( - [](Caliper* c, Channel* chn){ - if (!bindings_are_active) - init_alloc_hooks(); +void sysalloc_initialize(Caliper* c, Channel* chn) +{ + chn->events().post_init_evt.connect([](Caliper* c, Channel* chn) { + if (!bindings_are_active) + init_alloc_hooks(); - ChannelList::add(&sysalloc_channels, *chn); - }); + ChannelList::add(&sysalloc_channels, *chn); + }); - chn->events().pre_finish_evt.connect( - [](Caliper* c, Channel* chn){ - Log(2).stream() << chn->name() << ": Removing sysalloc hooks" << std::endl; - ChannelList::remove(&sysalloc_channels, *chn); - }); + chn->events().pre_finish_evt.connect([](Caliper* c, Channel* chn) { + Log(2).stream() << chn->name() << ": Removing sysalloc hooks" << std::endl; + ChannelList::remove(&sysalloc_channels, *chn); + }); Log(1).stream() << chn->name() << ": Registered sysalloc service" << std::endl; } -} // namespace [anonymous] - +} // namespace namespace cali { diff --git a/src/services/tau/tau.cpp b/src/services/tau/tau.cpp index 8c241e214..0418b93e8 100644 --- a/src/services/tau/tau.cpp +++ b/src/services/tau/tau.cpp @@ -27,15 +27,16 @@ class TAUBinding : public cali::AnnotationBinding public: - void initialize(Caliper* c, Channel* chn) { + void initialize(Caliper* c, Channel* chn) + { // initialize TAU - int argc = 1; - const char *dummy = "Caliper Application"; - char* argv[1]; + int argc = 1; + const char* dummy = "Caliper Application"; + char* argv[1]; argv[0] = const_cast(dummy); - Tau_init(argc,argv); + Tau_init(argc, argv); - int flag = 0; + int flag = 0; PMPI_Initialized(&flag); if (flag == 0) { @@ -43,33 +44,36 @@ class TAUBinding : public cali::AnnotationBinding } else { int rank = 0; PMPI_Comm_rank(MPI_COMM_WORLD, &rank); - + Tau_set_node(rank); } // register for events of interest? } - void finalize(Caliper*, Channel*) { + void finalize(Caliper*, Channel*) + { // do something? } const char* service_tag() const { return "tau"; }; // handle a begin event by starting a TAU timer - void on_begin(Caliper* c, Channel*, const Attribute& attr, const Variant& value) { + void on_begin(Caliper* c, Channel*, const Attribute& attr, const Variant& value) + { if (attr.type() == CALI_TYPE_STRING) { - Tau_start((const char*)value.data()); + Tau_start((const char*) value.data()); } else { - Tau_start((const char*)(value.to_string().data())); + Tau_start((const char*) (value.to_string().data())); } } // handle an end event by stopping a TAU timer - void on_end(Caliper* c, Channel*, const Attribute& attr, const Variant& value) { + void on_end(Caliper* c, Channel*, const Attribute& attr, const Variant& value) + { if (attr.type() == CALI_TYPE_STRING) { Tau_stop((const char*) value.data()); } else { - Tau_stop((const char*)(value.to_string().data())); + Tau_stop((const char*) (value.to_string().data())); } } }; @@ -82,4 +86,3 @@ namespace cali CaliperService tau_service { "tau", &AnnotationBinding::make_binding<::TAUBinding> }; } - diff --git a/src/services/templates/MeasurementService.cpp b/src/services/templates/MeasurementService.cpp index 00ddaafe0..78f687b34 100644 --- a/src/services/templates/MeasurementService.cpp +++ b/src/services/templates/MeasurementService.cpp @@ -26,14 +26,14 @@ using namespace cali; namespace { -typedef std::chrono::system_clock Clock; +typedef std::chrono::system_clock Clock; typedef std::chrono::time_point TimePoint; // Our "measurement function" -std::tuple measure(const TimePoint& start, const std::string& name) { +std::tuple measure(const TimePoint& start, const std::string& name) +{ auto now = Clock::now(); - uint64_t val = - name.length() * std::chrono::duration_cast(now - start).count(); + uint64_t val = name.length() * std::chrono::duration_cast(now - start).count(); return std::make_tuple(true, val); } @@ -53,12 +53,13 @@ class MeasurementTemplateService Attribute prval_attr; // A hidden attribute to store the previous measurement value on the Caliper blackboard }; - std::vector m_info; // Data for the configured measurement variables + std::vector m_info; // Data for the configured measurement variables - unsigned m_num_errors; // Number of measurement errors encountered at runtime - TimePoint m_starttime; // Initial value for our measurement function + unsigned m_num_errors; // Number of measurement errors encountered at runtime + TimePoint m_starttime; // Initial value for our measurement function - void snapshot_cb(Caliper* c, Channel* /*channel*/, SnapshotView /*trigger_info*/, SnapshotBuilder& rec) { + void snapshot_cb(Caliper* c, Channel* /*channel*/, SnapshotView /*trigger_info*/, SnapshotBuilder& rec) + { // The snapshot callback triggers performance measurements. // Measurement services should make measurements and add them to the // provided SnapshotRecord parameter, e.g. using rec->append(). @@ -70,7 +71,7 @@ class MeasurementTemplateService // Make measurements for all configured variables for (const MeasurementInfo& m : m_info) { - bool success; + bool success; uint64_t val; std::tie(success, val) = measure(m_starttime, m.name); @@ -96,7 +97,8 @@ class MeasurementTemplateService } } - void post_init_cb(Caliper* /*c*/, Channel* /*channel*/) { + void post_init_cb(Caliper* /*c*/, Channel* /*channel*/) + { // This callback is invoked when the channel is fully initialized // and ready to make measurements. This is a good place to initialize // measurement values, if needed. @@ -104,7 +106,8 @@ class MeasurementTemplateService m_starttime = Clock::now(); } - void finish_cb(Caliper* c, Channel* channel) { + void finish_cb(Caliper* c, Channel* channel) + { // This callback is invoked when the channel is being destroyed. // This is a good place to shut down underlying measurement libraries // (but keep in mind multiple channels may be active), report errors, @@ -112,12 +115,12 @@ class MeasurementTemplateService // the services they rely on may already be destroyed. if (m_num_errors > 0) - Log(0).stream() << channel->name() << ": measurement: " - << m_num_errors << " measurement errors!" + Log(0).stream() << channel->name() << ": measurement: " << m_num_errors << " measurement errors!" << std::endl; } - MeasurementInfo create_measurement_info(Caliper* c, Channel* channel, const std::string& name) { + MeasurementInfo create_measurement_info(Caliper* c, Channel* channel, const std::string& name) + { MeasurementInfo m; m.name = name; @@ -134,25 +137,22 @@ class MeasurementTemplateService // the Caliper context tree). Use SKIP_EVENTS to avoid triggering // events when using set/begin/end on this attribute. This attribute // is for absolute measurement values for . - m.value_attr = - c->create_attribute(std::string("measurement.val.") + name, - CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS); + m.value_attr = c->create_attribute( + std::string("measurement.val.") + name, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ); Variant v_true(true); // The delta attribute stores the difference of the measurement // value since the last snapshot. We add the "aggregatable" property // here, which lets Caliper aggregate these values automatically. - m.delta_attr = - c->create_attribute(std::string("measurement.") + name, - CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); + m.delta_attr = c->create_attribute( + std::string("measurement.") + name, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); // We use a hidden attribute to store the previous measurement // for on Caliper's per-thread blackboard. This is a @@ -161,19 +161,16 @@ class MeasurementTemplateService // In case more thread-specific information must be stored, it is // better to combine them in a structure and create a CALI_TYPE_PTR // attribute for this thread info in the service instance. - m.prval_attr = - c->create_attribute(std::string("measurement.pv.") + std::to_string(channel->id()) + name, - CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN | - CALI_ATTR_SKIP_EVENTS); + m.prval_attr = c->create_attribute( + std::string("measurement.pv.") + std::to_string(channel->id()) + name, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN | CALI_ATTR_SKIP_EVENTS + ); return m; } - MeasurementTemplateService(Caliper* c, Channel* channel) - : m_num_errors(0) + MeasurementTemplateService(Caliper* c, Channel* channel) : m_num_errors(0) { // Get the service configuration. This reads the configuration // variables defined in s_configdata from the environment, config @@ -190,7 +187,7 @@ class MeasurementTemplateService // Create a MeasurementInfo entry for each of the "measurement // variables" in the configuration. for (const std::string& name : names) - m_info.push_back( create_measurement_info(c, channel, name) ); + m_info.push_back(create_measurement_info(c, channel, name)); } public: @@ -215,30 +212,29 @@ class MeasurementTemplateService // any necessary objects like Caliper attributes, and register callback // functions. - static void register_measurement_template_service(Caliper* c, Channel* channel) { + static void register_measurement_template_service(Caliper* c, Channel* channel) + { // Create a new service instance for this channel MeasurementTemplateService* instance = new MeasurementTemplateService(c, channel); // Register callback functions using lambdas - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->post_init_cb(c, channel); - }); + channel->events().post_init_evt.connect([instance](Caliper* c, Channel* channel) { + instance->post_init_cb(c, channel); + }); channel->events().snapshot.connect( - [instance](Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec){ + [instance](Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec) { instance->snapshot_cb(c, channel, trigger_info, rec); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - // This callback is invoked when the channel is destroyed. - // No other callback will be invoked afterwards. - // Delete the channel's service instance here! - instance->finish_cb(c, channel); - delete instance; - }); - - Log(1).stream() << channel->name() << ": Registered measurement template service" - << std::endl; + } + ); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + // This callback is invoked when the channel is destroyed. + // No other callback will be invoked afterwards. + // Delete the channel's service instance here! + instance->finish_cb(c, channel); + delete instance; + }); + + Log(1).stream() << channel->name() << ": Registered measurement template service" << std::endl; } }; @@ -255,14 +251,12 @@ const char* MeasurementTemplateService::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { -CaliperService measurement_template_service = { - ::MeasurementTemplateService::s_spec, - ::MeasurementTemplateService::register_measurement_template_service -}; +CaliperService measurement_template_service = { ::MeasurementTemplateService::s_spec, + ::MeasurementTemplateService::register_measurement_template_service }; } // namespace cali diff --git a/src/services/textlog/TextLog.cpp b/src/services/textlog/TextLog.cpp index 3f078421d..721ecfae7 100644 --- a/src/services/textlog/TextLog.cpp +++ b/src/services/textlog/TextLog.cpp @@ -32,24 +32,24 @@ namespace class TextLogService { - std::mutex trigger_attr_mutex; - std::vector trigger_attributes; + std::mutex trigger_attr_mutex; + std::vector trigger_attributes; - std::vector trigger_attr_names; + std::vector trigger_attr_names; - std::string stream_filename; - std::string formatstr; + std::string stream_filename; + std::string formatstr; - SnapshotTextFormatter formatter; - OutputStream stream; + SnapshotTextFormatter formatter; + OutputStream stream; - Attribute set_event_attr; - Attribute end_event_attr; + Attribute set_event_attr; + Attribute end_event_attr; - std::mutex stream_mutex; + std::mutex stream_mutex; - std::string - create_default_formatstring(const std::vector& attr_names) { + std::string create_default_formatstring(const std::vector& attr_names) + { if (attr_names.size() < 1) return "%time.inclusive.duration%"; @@ -58,7 +58,7 @@ class TextLogService for (const std::string& s : attr_names) name_sizes += s.size(); - int w = std::max(0, (80-10-name_sizes-2*attr_names.size()) / attr_names.size()); + int w = std::max(0, (80 - 10 - name_sizes - 2 * attr_names.size()) / attr_names.size()); std::ostringstream os; @@ -70,20 +70,21 @@ class TextLogService return os.str(); } - void check_attribute(const Attribute& attr) { + void check_attribute(const Attribute& attr) + { std::vector::iterator it = std::find(trigger_attr_names.begin(), trigger_attr_names.end(), attr.name()); if (it != trigger_attr_names.end()) { - std::lock_guard - g(trigger_attr_mutex); + std::lock_guard g(trigger_attr_mutex); trigger_attributes.push_back(attr); Log(1).stream() << "textlog: Found " << *it << std::endl; } } - bool is_triggering_event(const Attribute& event_attr, SnapshotView trigger_info) { + bool is_triggering_event(const Attribute& event_attr, SnapshotView trigger_info) + { if (!event_attr) return false; @@ -92,8 +93,7 @@ class TextLogService if (!event.empty()) { cali_id_t id = event.value().to_id(); - std::lock_guard - g(trigger_attr_mutex); + std::lock_guard g(trigger_attr_mutex); for (const Attribute& a : trigger_attributes) if (id == a.id()) @@ -103,15 +103,15 @@ class TextLogService return false; } - bool is_triggering_snapshot(SnapshotView trigger_info) { + bool is_triggering_snapshot(SnapshotView trigger_info) + { if (trigger_info.empty()) return false; // check if any of the textlog trigger attributes are in trigger_info { - std::lock_guard - g(trigger_attr_mutex); + std::lock_guard g(trigger_attr_mutex); for (const Attribute& a : trigger_attributes) if (!trigger_info.get(a).empty()) @@ -119,18 +119,17 @@ class TextLogService } // check if there is a begin or end event with any of the textlog triggers - return is_triggering_event(end_event_attr, trigger_info) || - is_triggering_event(set_event_attr, trigger_info); + return is_triggering_event(end_event_attr, trigger_info) || is_triggering_event(set_event_attr, trigger_info); } - void process_snapshot(Caliper* c, SnapshotView trigger_info, SnapshotView snapshot) { + void process_snapshot(Caliper* c, SnapshotView trigger_info, SnapshotView snapshot) + { if (!is_triggering_snapshot(trigger_info)) return; std::vector rec(snapshot.begin(), snapshot.end()); - std::lock_guard - g(stream_mutex); + std::lock_guard g(stream_mutex); if (!stream) stream.set_filename(stream_filename.c_str(), *c, rec); @@ -140,7 +139,8 @@ class TextLogService formatter.print(*osptr, *c, rec) << std::endl; } - void post_init(Caliper* c, Channel* chn) { + void post_init(Caliper* c, Channel* chn) + { if (formatstr.size() == 0) formatstr = create_default_formatstring(trigger_attr_names); @@ -152,43 +152,33 @@ class TextLogService for (const Attribute& a : c->get_all_attributes()) check_attribute(a); - chn->events().process_snapshot.connect( - [this](Caliper* c, Channel* chn, SnapshotView info, SnapshotView rec){ - process_snapshot(c, info, rec); - }); + chn->events().process_snapshot.connect([this](Caliper* c, Channel* chn, SnapshotView info, SnapshotView rec) { + process_snapshot(c, info, rec); + }); } TextLogService(Caliper* c, Channel* chn) - { - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - - trigger_attr_names = - config.get("trigger").to_stringlist(","); - stream_filename = - config.get("filename").to_string(); - formatstr = - config.get("formatstring").to_string(); - } + { + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); + + trigger_attr_names = config.get("trigger").to_stringlist(","); + stream_filename = config.get("filename").to_string(); + formatstr = config.get("formatstring").to_string(); + } public: static const char* s_spec; - static void textlog_register(Caliper* c, Channel* chn) { + static void textlog_register(Caliper* c, Channel* chn) + { TextLogService* instance = new TextLogService(c, chn); - chn->events().create_attr_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr){ - instance->check_attribute(attr); - }); - chn->events().post_init_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->post_init(c, chn); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - delete instance; - }); + chn->events().create_attr_evt.connect([instance](Caliper* c, Channel* chn, const Attribute& attr) { + instance->check_attribute(attr); + }); + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* chn) { instance->post_init(c, chn); }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { delete instance; }); Log(1).stream() << chn->name() << ": Registered text log service" << std::endl; } diff --git a/src/services/timer/Timer.cpp b/src/services/timer/Timer.cpp index b0c624d95..55d6a6441 100644 --- a/src/services/timer/Timer.cpp +++ b/src/services/timer/Timer.cpp @@ -38,11 +38,9 @@ class TimerService uint64_t prev_snapshot_timestamp; // A per-attribute stack of timestamps for computing inclusive times - std::map< cali_id_t, std::vector > inclusive_timer_stack; + std::map> inclusive_timer_stack; - TimerInfo() - : prev_snapshot_timestamp(0) - { } + TimerInfo() : prev_snapshot_timestamp(0) {} }; using clock = std::chrono::steady_clock; @@ -57,27 +55,26 @@ class TimerService Attribute offset_attr; // Keeps all created timer info objects so we can delete them later - std::vector info_obj_list; - std::mutex info_obj_mutex; + std::vector info_obj_list; + std::mutex info_obj_mutex; - bool record_inclusive_duration; + bool record_inclusive_duration; Attribute begin_evt_attr; Attribute end_evt_attr; - int n_stack_errors { 0 }; + int n_stack_errors { 0 }; - TimerInfo* acquire_timerinfo(Caliper* c) { - TimerInfo* ti = - static_cast(c->get(timerinfo_attr).value().get_ptr()); + TimerInfo* acquire_timerinfo(Caliper* c) + { + TimerInfo* ti = static_cast(c->get(timerinfo_attr).value().get_ptr()); if (!ti && !c->is_signal()) { ti = new TimerInfo; c->set(timerinfo_attr, Variant(cali_make_variant_from_ptr(ti))); - std::lock_guard - g(info_obj_mutex); + std::lock_guard g(info_obj_mutex); info_obj_list.push_back(ti); } @@ -85,8 +82,9 @@ class TimerService return ti; } - void snapshot_cb(Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec) { - auto now = clock::now(); + void snapshot_cb(Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec) + { + auto now = clock::now(); uint64_t nsec = chrono::duration_cast(now - tstart).count(); rec.append(offset_attr, Variant(nsec)); @@ -127,7 +125,8 @@ class TimerService } } - void post_init_cb(Caliper* c, Channel* chn) { + void post_init_cb(Caliper* c, Channel* chn) + { // Find begin/end event snapshot event info attributes begin_evt_attr = c->get_attribute("cali.event.begin"); @@ -135,8 +134,10 @@ class TimerService if (!begin_evt_attr || !end_evt_attr) { if (record_inclusive_duration) - Log(1).stream() << chn->name() << ": Timestamp: Note: event trigger attributes not registered,\n" - " disabling phase timers." << std::endl; + Log(1).stream() << chn->name() + << ": Timestamp: Note: event trigger attributes not registered,\n" + " disabling phase timers." + << std::endl; record_inclusive_duration = false; } @@ -145,84 +146,77 @@ class TimerService acquire_timerinfo(c); } - void finish_cb(Caliper*, Channel* chn) { + void finish_cb(Caliper*, Channel* chn) + { if (n_stack_errors > 0) - Log(1).stream() << chn->name() << ": timestamp: Encountered " - << n_stack_errors - << " inclusive time stack errors!" - << std::endl; + Log(1).stream() << chn->name() << ": timestamp: Encountered " << n_stack_errors + << " inclusive time stack errors!" << std::endl; } - TimerService(Caliper* c, Channel* chn) - : tstart(clock::now()) - { - ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); - record_inclusive_duration = config.get("inclusive_duration").to_bool(); - - Attribute unit_attr = - c->create_attribute("time.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); - Variant nsec_val = Variant("nsec"); - - offset_attr = - c->create_attribute("time.offset.ns", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS, - 1, &unit_attr, &nsec_val); - snapshot_duration_attr = - c->create_attribute("time.duration.ns", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE, - 1, &unit_attr, &nsec_val); - inclusive_duration_attr = - c->create_attribute("time.inclusive.duration.ns", CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE, - 1, &unit_attr, &nsec_val); - timerinfo_attr = - c->create_attribute(std::string("timer.info.") + std::to_string(chn->id()), CALI_TYPE_PTR, - CALI_ATTR_ASVALUE | - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_HIDDEN); - } + TimerService(Caliper* c, Channel* chn) : tstart(clock::now()) + { + ConfigSet config = services::init_config_from_spec(chn->config(), s_spec); + record_inclusive_duration = config.get("inclusive_duration").to_bool(); + + Attribute unit_attr = c->create_attribute("time.unit", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + Variant nsec_val = Variant("nsec"); + + offset_attr = c->create_attribute( + "time.offset.ns", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS, + 1, + &unit_attr, + &nsec_val + ); + snapshot_duration_attr = c->create_attribute( + "time.duration.ns", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + 1, + &unit_attr, + &nsec_val + ); + inclusive_duration_attr = c->create_attribute( + "time.inclusive.duration.ns", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE, + 1, + &unit_attr, + &nsec_val + ); + timerinfo_attr = c->create_attribute( + std::string("timer.info.") + std::to_string(chn->id()), + CALI_TYPE_PTR, + CALI_ATTR_ASVALUE | CALI_ATTR_SCOPE_THREAD | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN + ); + } - ~TimerService() { - std::lock_guard - g(info_obj_mutex); + ~TimerService() + { + std::lock_guard g(info_obj_mutex); - for (TimerInfo* ti : info_obj_list) - delete ti; - } + for (TimerInfo* ti : info_obj_list) + delete ti; + } public: static const char* s_spec; - static void timer_register(Caliper* c, Channel* chn) { + static void timer_register(Caliper* c, Channel* chn) + { TimerService* instance = new TimerService(c, chn); - chn->events().post_init_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->post_init_cb(c, chn); - }); - chn->events().create_thread_evt.connect( - [instance](Caliper* c, Channel*){ - instance->acquire_timerinfo(c); - }); - chn->events().snapshot.connect( - [instance](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec){ - instance->snapshot_cb(c, chn, info, rec); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finish_cb(c, chn); - delete instance; - }); + chn->events().post_init_evt.connect([instance](Caliper* c, Channel* chn) { instance->post_init_cb(c, chn); }); + chn->events().create_thread_evt.connect([instance](Caliper* c, Channel*) { instance->acquire_timerinfo(c); }); + chn->events().snapshot.connect([instance](Caliper* c, Channel* chn, SnapshotView info, SnapshotBuilder& rec) { + instance->snapshot_cb(c, chn, info, rec); + }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finish_cb(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered timer service" << endl; } @@ -248,13 +242,12 @@ const char* timestamp_spec = R"json( } )json"; -} // namespace - +} // namespace namespace cali { -CaliperService timer_service = { ::TimerService::s_spec, ::TimerService::timer_register }; +CaliperService timer_service = { ::TimerService::s_spec, ::TimerService::timer_register }; CaliperService timestamp_service = { timestamp_spec, ::TimerService::timer_register }; } // namespace cali diff --git a/src/services/topdown/IntelTopdown.cpp b/src/services/topdown/IntelTopdown.cpp index a3d22fb09..80064bb28 100644 --- a/src/services/topdown/IntelTopdown.cpp +++ b/src/services/topdown/IntelTopdown.cpp @@ -45,14 +45,12 @@ class IntelTopdown unsigned num_bsp_computed; unsigned num_bsp_skipped; - enum Level { - All = 1, - Top = 2 - }; + enum Level { All = 1, Top = 2 }; Level level; - Variant get_val_from_rec(const std::vector& rec, const char* name) { + Variant get_val_from_rec(const std::vector& rec, const char* name) + { Variant ret; auto c_it = counter_attrs.find(name); @@ -61,9 +59,7 @@ class IntelTopdown cali_id_t attr_id = c_it->second.id(); - auto it = std::find_if(rec.begin(), rec.end(), [attr_id](const Entry& e){ - return e.attribute() == attr_id; - }); + auto it = std::find_if(rec.begin(), rec.end(), [attr_id](const Entry& e) { return e.attribute() == attr_id; }); if (it != rec.end()) ret = it->value(); @@ -73,15 +69,16 @@ class IntelTopdown return ret; } - bool find_counter_attrs(CaliperMetadataAccessInterface& db) { - const char* list = (level == All ? s_all_counters : s_top_counters); - auto counters = StringConverter(list).to_stringlist(); + bool find_counter_attrs(CaliperMetadataAccessInterface& db) + { + const char* list = (level == All ? s_all_counters : s_top_counters); + auto counters = StringConverter(list).to_stringlist(); - for (const auto &s : counters) { - Attribute attr = db.get_attribute(std::string("sum#papi.")+s); + for (const auto& s : counters) { + Attribute attr = db.get_attribute(std::string("sum#papi.") + s); if (!attr) - attr = db.get_attribute(std::string("papi.")+s); + attr = db.get_attribute(std::string("papi.") + s); if (!attr) { Log(0).stream() << "topdown: " << s << " counter attribute not found!" << std::endl; return false; @@ -93,71 +90,56 @@ class IntelTopdown return true; } - void make_result_attrs(CaliperMetadataAccessInterface& db) { - const char* res_top[] = { - "retiring", "backend_bound", "frontend_bound", "bad_speculation", nullptr - }; - const char* res_all[] = { - "retiring", "backend_bound", "frontend_bound", "bad_speculation", - "branch_mispredict", "machine_clears", - "frontend_latency", "frontend_bandwidth", - "memory_bound", "core_bound", - "ext_mem_bound", "l1_bound", "l2_bound", "l3_bound", - nullptr - }; + void make_result_attrs(CaliperMetadataAccessInterface& db) + { + const char* res_top[] = { "retiring", "backend_bound", "frontend_bound", "bad_speculation", nullptr }; + const char* res_all[] = { "retiring", "backend_bound", "frontend_bound", + "bad_speculation", "branch_mispredict", "machine_clears", + "frontend_latency", "frontend_bandwidth", "memory_bound", + "core_bound", "ext_mem_bound", "l1_bound", + "l2_bound", "l3_bound", nullptr }; const char** res = (level == Top ? res_top : res_all); for (const char** s = res; s && *s; ++s) - result_attrs[std::string(*s)] = - db.create_attribute(std::string("topdown.")+(*s), CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS); + result_attrs[std::string(*s)] = db.create_attribute( + std::string("topdown.") + (*s), + CALI_TYPE_DOUBLE, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS + ); } - std::vector - compute_toplevel(const std::vector& rec) + std::vector compute_toplevel(const std::vector& rec) { std::vector ret; - Variant v_cpu_clk_unhalted_thread_p = - get_val_from_rec(rec, "CPU_CLK_THREAD_UNHALTED:THREAD_P"); - Variant v_uops_retired_retire_slots = - get_val_from_rec(rec, "UOPS_RETIRED:RETIRE_SLOTS"); - Variant v_uops_issued_any = - get_val_from_rec(rec, "UOPS_ISSUED:ANY"); - Variant v_int_misc_recovery_cycles = - get_val_from_rec(rec, "INT_MISC:RECOVERY_CYCLES"); - Variant v_idq_uops_not_delivered_core = - get_val_from_rec(rec, "IDQ_UOPS_NOT_DELIVERED:CORE"); - - bool is_incomplete = - v_cpu_clk_unhalted_thread_p.empty() || - v_uops_retired_retire_slots.empty() || - v_uops_issued_any.empty() || - v_int_misc_recovery_cycles.empty() || - v_idq_uops_not_delivered_core.empty(); - bool is_nonzero = - v_cpu_clk_unhalted_thread_p.to_double() > 0.0 && - v_uops_retired_retire_slots.to_double() > 0.0 && - v_uops_issued_any.to_double() > 0.0 && - v_int_misc_recovery_cycles.to_double() > 0.0 && - v_idq_uops_not_delivered_core.to_double() > 0.0; + Variant v_cpu_clk_unhalted_thread_p = get_val_from_rec(rec, "CPU_CLK_THREAD_UNHALTED:THREAD_P"); + Variant v_uops_retired_retire_slots = get_val_from_rec(rec, "UOPS_RETIRED:RETIRE_SLOTS"); + Variant v_uops_issued_any = get_val_from_rec(rec, "UOPS_ISSUED:ANY"); + Variant v_int_misc_recovery_cycles = get_val_from_rec(rec, "INT_MISC:RECOVERY_CYCLES"); + Variant v_idq_uops_not_delivered_core = get_val_from_rec(rec, "IDQ_UOPS_NOT_DELIVERED:CORE"); + + bool is_incomplete = v_cpu_clk_unhalted_thread_p.empty() || v_uops_retired_retire_slots.empty() + || v_uops_issued_any.empty() || v_int_misc_recovery_cycles.empty() + || v_idq_uops_not_delivered_core.empty(); + bool is_nonzero = v_cpu_clk_unhalted_thread_p.to_double() > 0.0 && v_uops_retired_retire_slots.to_double() > 0.0 + && v_uops_issued_any.to_double() > 0.0 && v_int_misc_recovery_cycles.to_double() > 0.0 + && v_idq_uops_not_delivered_core.to_double() > 0.0; double slots = 4.0 * v_cpu_clk_unhalted_thread_p.to_double(); if (is_incomplete || !is_nonzero || slots < 1.0) return ret; - double retiring = v_uops_retired_retire_slots.to_double() / slots; - double bad_speculation = - (v_uops_issued_any.to_double() - - v_uops_retired_retire_slots.to_double() - + 4.0 * v_int_misc_recovery_cycles.to_double()) / slots; + double retiring = v_uops_retired_retire_slots.to_double() / slots; + double bad_speculation = (v_uops_issued_any.to_double() - v_uops_retired_retire_slots.to_double() + + 4.0 * v_int_misc_recovery_cycles.to_double()) + / slots; double frontend_bound = v_idq_uops_not_delivered_core.to_double() / slots; - double backend_bound = - 1.0 - (retiring + bad_speculation + frontend_bound); + double backend_bound = 1.0 - (retiring + bad_speculation + frontend_bound); ret.reserve(4); - ret.push_back(Entry(result_attrs["retiring"], Variant(std::max(retiring, 0.0)))); + ret.push_back(Entry(result_attrs["retiring"], Variant(std::max(retiring, 0.0)))); ret.push_back(Entry(result_attrs["backend_bound"], Variant(std::max(backend_bound, 0.0)))); ret.push_back(Entry(result_attrs["frontend_bound"], Variant(std::max(frontend_bound, 0.0)))); ret.push_back(Entry(result_attrs["bad_speculation"], Variant(std::max(bad_speculation, 0.0)))); @@ -165,73 +147,50 @@ class IntelTopdown return ret; } - std::vector - compute_backend_bound(const std::vector& rec) + std::vector compute_backend_bound(const std::vector& rec) { std::vector ret; - Variant v_cpu_clk_unhalted_thread_p = - get_val_from_rec(rec, "CPU_CLK_THREAD_UNHALTED:THREAD_P"); - Variant v_cycle_activity_stalls_ldm_pending = - get_val_from_rec(rec, "CYCLE_ACTIVITY:STALLS_LDM_PENDING"); - Variant v_cycle_activity_cycles_no_execute = - get_val_from_rec(rec, "CYCLE_ACTIVITY:CYCLES_NO_EXECUTE"); - Variant v_uops_executed_core_cycles_ge_1 = - get_val_from_rec(rec, "UOPS_EXECUTED:CORE_CYCLES_GE_1"); - Variant v_uops_executed_core_cycles_ge_2 = - get_val_from_rec(rec, "UOPS_EXECUTED:CORE_CYCLES_GE_2"); - Variant v_mem_load_uops_retired_l3_miss = - get_val_from_rec(rec, "MEM_LOAD_UOPS_RETIRED:L3_MISS"); - Variant v_mem_load_uops_retired_l3_hit = - get_val_from_rec(rec, "MEM_LOAD_UOPS_RETIRED:L3_HIT"); - Variant v_cycle_activity_stalls_l2_pending = - get_val_from_rec(rec, "CYCLE_ACTIVITY:STALLS_L2_PENDING"); - Variant v_cycle_activity_stalls_l1d_pending = - get_val_from_rec(rec, "CYCLE_ACTIVITY:STALLS_L1D_PENDING"); - - bool is_incomplete = - v_cpu_clk_unhalted_thread_p.empty() || - v_cycle_activity_stalls_ldm_pending.empty() || - v_cycle_activity_cycles_no_execute.empty() || - v_uops_executed_core_cycles_ge_1.empty() || - v_uops_executed_core_cycles_ge_2.empty() || - v_mem_load_uops_retired_l3_miss.empty() || - v_mem_load_uops_retired_l3_hit.empty() || - v_cycle_activity_stalls_l2_pending.empty() || - v_cycle_activity_stalls_l1d_pending.empty(); + Variant v_cpu_clk_unhalted_thread_p = get_val_from_rec(rec, "CPU_CLK_THREAD_UNHALTED:THREAD_P"); + Variant v_cycle_activity_stalls_ldm_pending = get_val_from_rec(rec, "CYCLE_ACTIVITY:STALLS_LDM_PENDING"); + Variant v_cycle_activity_cycles_no_execute = get_val_from_rec(rec, "CYCLE_ACTIVITY:CYCLES_NO_EXECUTE"); + Variant v_uops_executed_core_cycles_ge_1 = get_val_from_rec(rec, "UOPS_EXECUTED:CORE_CYCLES_GE_1"); + Variant v_uops_executed_core_cycles_ge_2 = get_val_from_rec(rec, "UOPS_EXECUTED:CORE_CYCLES_GE_2"); + Variant v_mem_load_uops_retired_l3_miss = get_val_from_rec(rec, "MEM_LOAD_UOPS_RETIRED:L3_MISS"); + Variant v_mem_load_uops_retired_l3_hit = get_val_from_rec(rec, "MEM_LOAD_UOPS_RETIRED:L3_HIT"); + Variant v_cycle_activity_stalls_l2_pending = get_val_from_rec(rec, "CYCLE_ACTIVITY:STALLS_L2_PENDING"); + Variant v_cycle_activity_stalls_l1d_pending = get_val_from_rec(rec, "CYCLE_ACTIVITY:STALLS_L1D_PENDING"); + + bool is_incomplete = v_cpu_clk_unhalted_thread_p.empty() || v_cycle_activity_stalls_ldm_pending.empty() + || v_cycle_activity_cycles_no_execute.empty() || v_uops_executed_core_cycles_ge_1.empty() + || v_uops_executed_core_cycles_ge_2.empty() || v_mem_load_uops_retired_l3_miss.empty() + || v_mem_load_uops_retired_l3_hit.empty() || v_cycle_activity_stalls_l2_pending.empty() + || v_cycle_activity_stalls_l1d_pending.empty(); double clocks = v_cpu_clk_unhalted_thread_p.to_double(); if (is_incomplete || !(clocks > 1.0)) return ret; - double memory_bound = - v_cycle_activity_stalls_ldm_pending.to_double() / clocks; + double memory_bound = v_cycle_activity_stalls_ldm_pending.to_double() / clocks; double be_bound_at_exe = - (v_cycle_activity_cycles_no_execute.to_double() - + v_uops_executed_core_cycles_ge_1.to_double() - - v_uops_executed_core_cycles_ge_2.to_double()) / clocks; - double l3_tot = - v_mem_load_uops_retired_l3_hit.to_double() + - 7.0 * v_mem_load_uops_retired_l3_miss.to_double(); + (v_cycle_activity_cycles_no_execute.to_double() + v_uops_executed_core_cycles_ge_1.to_double() + - v_uops_executed_core_cycles_ge_2.to_double()) + / clocks; + double l3_tot = v_mem_load_uops_retired_l3_hit.to_double() + 7.0 * v_mem_load_uops_retired_l3_miss.to_double(); double l3_hit_fraction = 0.0; double l3_miss_fraction = 0.0; if (l3_tot > 0.0) { - l3_hit_fraction = - v_mem_load_uops_retired_l3_hit.to_double() / l3_tot; - l3_miss_fraction = - v_mem_load_uops_retired_l3_miss.to_double() / l3_tot; + l3_hit_fraction = v_mem_load_uops_retired_l3_hit.to_double() / l3_tot; + l3_miss_fraction = v_mem_load_uops_retired_l3_miss.to_double() / l3_tot; } - double ext_mem_bound = - v_cycle_activity_stalls_l2_pending.to_double() * l3_miss_fraction / clocks; + double ext_mem_bound = v_cycle_activity_stalls_l2_pending.to_double() * l3_miss_fraction / clocks; double l1_bound = - (v_cycle_activity_stalls_ldm_pending.to_double() - - v_cycle_activity_stalls_l1d_pending.to_double()) / clocks; + (v_cycle_activity_stalls_ldm_pending.to_double() - v_cycle_activity_stalls_l1d_pending.to_double()) + / clocks; double l2_bound = - (v_cycle_activity_stalls_l1d_pending.to_double() - - v_cycle_activity_stalls_l2_pending.to_double()) / clocks; - double l3_bound = - v_cycle_activity_stalls_l2_pending.to_double() * l3_hit_fraction / clocks; + (v_cycle_activity_stalls_l1d_pending.to_double() - v_cycle_activity_stalls_l2_pending.to_double()) / clocks; + double l3_bound = v_cycle_activity_stalls_l2_pending.to_double() * l3_hit_fraction / clocks; ret.reserve(6); ret.push_back(Entry(result_attrs["memory_bound"], Variant(memory_bound))); @@ -244,22 +203,17 @@ class IntelTopdown return ret; } - std::vector - compute_frontend_bound(const std::vector& rec) + std::vector compute_frontend_bound(const std::vector& rec) { std::vector ret; - Variant v_cpu_clk_unhalted_thread_p = - get_val_from_rec(rec, "CPU_CLK_THREAD_UNHALTED:THREAD_P"); - Variant v_idq_uops_not_delivered = - get_val_from_rec(rec, "IDQ_UOPS_NOT_DELIVERED:CYCLES_0_UOPS_DELIV_CORE"); + Variant v_cpu_clk_unhalted_thread_p = get_val_from_rec(rec, "CPU_CLK_THREAD_UNHALTED:THREAD_P"); + Variant v_idq_uops_not_delivered = get_val_from_rec(rec, "IDQ_UOPS_NOT_DELIVERED:CYCLES_0_UOPS_DELIV_CORE"); - bool is_incomplete = - v_cpu_clk_unhalted_thread_p.empty() || - v_idq_uops_not_delivered.empty(); + bool is_incomplete = v_cpu_clk_unhalted_thread_p.empty() || v_idq_uops_not_delivered.empty(); double clocks = v_cpu_clk_unhalted_thread_p.to_double(); - double uops = v_idq_uops_not_delivered.to_double(); + double uops = v_idq_uops_not_delivered.to_double(); if (is_incomplete || clocks < 1.0 || uops > clocks) return ret; @@ -273,28 +227,22 @@ class IntelTopdown return ret; } - std::vector - compute_bad_speculation(const std::vector& rec) + std::vector compute_bad_speculation(const std::vector& rec) { std::vector ret; - Variant v_br_misp_retired_all_branches = - get_val_from_rec(rec, "BR_MISP_RETIRED:ALL_BRANCHES"); - Variant v_machine_clears_count = - get_val_from_rec(rec, "MACHINE_CLEARS:COUNT"); + Variant v_br_misp_retired_all_branches = get_val_from_rec(rec, "BR_MISP_RETIRED:ALL_BRANCHES"); + Variant v_machine_clears_count = get_val_from_rec(rec, "MACHINE_CLEARS:COUNT"); - bool is_incomplete = - v_br_misp_retired_all_branches.empty() || - v_machine_clears_count.empty(); + bool is_incomplete = v_br_misp_retired_all_branches.empty() || v_machine_clears_count.empty(); double br_misp_retired_all_branches = v_br_misp_retired_all_branches.to_double(); - double machine_clears_count = v_machine_clears_count.to_double(); + double machine_clears_count = v_machine_clears_count.to_double(); if (is_incomplete || !(br_misp_retired_all_branches + machine_clears_count > 1.0)) return ret; - double branch_mispredict = - br_misp_retired_all_branches / (br_misp_retired_all_branches + machine_clears_count); + double branch_mispredict = br_misp_retired_all_branches / (br_misp_retired_all_branches + machine_clears_count); ret.reserve(2); ret.push_back(Entry(result_attrs["branch_mispredict"], Variant(branch_mispredict))); @@ -303,7 +251,8 @@ class IntelTopdown return ret; } - void postprocess_snapshot_cb(std::vector& rec) { + void postprocess_snapshot_cb(std::vector& rec) + { std::vector result = compute_toplevel(rec); if (result.empty()) @@ -343,15 +292,13 @@ class IntelTopdown } } - void finish_cb(Caliper* c, Channel* channel) { - Log(1).stream() << channel->name() - << ": topdown: Computed topdown metrics for " << num_top_computed - << " records, skipped " << num_top_skipped - << std::endl; + void finish_cb(Caliper* c, Channel* channel) + { + Log(1).stream() << channel->name() << ": topdown: Computed topdown metrics for " << num_top_computed + << " records, skipped " << num_top_skipped << std::endl; if (Log::verbosity() >= 2) { - Log(2).stream() << channel->name() - << ": topdown: Records processed per topdown level: " + Log(2).stream() << channel->name() << ": topdown: Records processed per topdown level: " << "\n top: " << num_top_computed << " computed, " << num_top_skipped << " skipped," << "\n bad spec: " << num_bsp_computed << " computed, " << num_bsp_skipped << " skipped," << "\n frontend: " << num_bsp_computed << " computed, " << num_bsp_skipped << " skipped," @@ -360,11 +307,9 @@ class IntelTopdown if (!counters_not_found.empty()) { std::ostringstream os; - for (auto &p : counters_not_found) + for (auto& p : counters_not_found) os << "\n " << p.first << ": " << p.second; - Log(2).stream() << channel->name() << ": topdown: Counters not found:" - << os.str() - << std::endl; + Log(2).stream() << channel->name() << ": topdown: Counters not found:" << os.str() << std::endl; } } } @@ -379,57 +324,52 @@ class IntelTopdown num_bsp_computed(0), num_bsp_skipped(0), level(lvl) - { } + {} public: static const char* s_spec; - static void intel_topdown_register(Caliper* c, Channel* channel) { - Level level = Top; + static void intel_topdown_register(Caliper* c, Channel* channel) + { + Level level = Top; const char* counters = s_top_counters; - auto config = services::init_config_from_spec(channel->config(), s_spec); + auto config = services::init_config_from_spec(channel->config(), s_spec); std::string lvlcfg = config.get("level").to_string(); if (lvlcfg == "all") { level = All; counters = s_all_counters; } else if (lvlcfg != "top") { - Log(0).stream() << channel->name() << ": topdown: Unknown level \"" - << lvlcfg << "\", skipping topdown" << std::endl; + Log(0).stream() << channel->name() << ": topdown: Unknown level \"" << lvlcfg << "\", skipping topdown" + << std::endl; return; } channel->config().set("CALI_PAPI_COUNTERS", counters); if (!cali::services::register_service(c, channel, "papi")) { - Log(0).stream() << channel->name() - << ": topdown: Unable to register papi service, skipping topdown" + Log(0).stream() << channel->name() << ": topdown: Unable to register papi service, skipping topdown" << std::endl; return; } IntelTopdown* instance = new IntelTopdown(level); - channel->events().pre_flush_evt.connect( - [instance](Caliper* c, Channel* channel, SnapshotView){ - if (instance->find_counter_attrs(*c)) - instance->make_result_attrs(*c); - else - Log(0).stream() << channel->name() - << ": topdown: Could not find counter attributes!" - << std::endl; - }); - channel->events().postprocess_snapshot.connect( - [instance](Caliper*, Channel*, std::vector& rec){ - instance->postprocess_snapshot_cb(rec); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - delete instance; - }); + channel->events().pre_flush_evt.connect([instance](Caliper* c, Channel* channel, SnapshotView) { + if (instance->find_counter_attrs(*c)) + instance->make_result_attrs(*c); + else + Log(0).stream() << channel->name() << ": topdown: Could not find counter attributes!" << std::endl; + }); + channel->events().postprocess_snapshot.connect([instance](Caliper*, Channel*, std::vector& rec) { + instance->postprocess_snapshot_cb(rec); + }); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + delete instance; + }); Log(1).stream() << channel->name() << ": Registered topdown service. Level: " << lvlcfg << "." << std::endl; } @@ -473,7 +413,7 @@ const char* IntelTopdown::s_spec = R"json( } )json"; -} +} // namespace namespace cali { diff --git a/src/services/trace/Trace.cpp b/src/services/trace/Trace.cpp index 83f5de7d6..cf83d9c70 100644 --- a/src/services/trace/Trace.cpp +++ b/src/services/trace/Trace.cpp @@ -31,27 +31,22 @@ namespace class Trace { - enum BufferPolicy { - Flush, Grow, Stop - }; + enum BufferPolicy { Flush, Grow, Stop }; struct TraceBuffer { - std::atomic stopped; - std::atomic retired; + std::atomic stopped; + std::atomic retired; - TraceBufferChunk* chunks; - TraceBuffer* next; - TraceBuffer* prev; + TraceBufferChunk* chunks; + TraceBuffer* next; + TraceBuffer* prev; - TraceBuffer(size_t s) - : stopped(false), retired(false), chunks(new TraceBufferChunk(s)), next(0), prev(0) - { } + TraceBuffer(size_t s) : stopped(false), retired(false), chunks(new TraceBufferChunk(s)), next(0), prev(0) {} - ~TraceBuffer() { - delete chunks; - } + ~TraceBuffer() { delete chunks; } - void unlink() { + void unlink() + { if (next) next->prev = prev; if (prev) @@ -59,36 +54,35 @@ class Trace } }; - BufferPolicy policy = BufferPolicy::Grow; - size_t buffersize = 2 * 1024 * 1024; + BufferPolicy policy = BufferPolicy::Grow; + size_t buffersize = 2 * 1024 * 1024; - size_t dropped_snapshots = 0; + size_t dropped_snapshots = 0; - unsigned num_acquired = 0; - unsigned num_released = 0; - unsigned num_retired = 0; + unsigned num_acquired = 0; + unsigned num_released = 0; + unsigned num_retired = 0; - Attribute tbuf_attr; + Attribute tbuf_attr; TraceBuffer* tbuf_list = nullptr; util::spinlock tbuf_lock; - std::mutex flush_lock; + std::mutex flush_lock; - TraceBuffer* acquire_tbuf(Caliper* c, Channel* chn, bool can_alloc) { + TraceBuffer* acquire_tbuf(Caliper* c, Channel* chn, bool can_alloc) + { // we store a pointer to the thread-local trace buffer for this channel // on the thread's blackboard - TraceBuffer* tbuf = - static_cast(c->get(tbuf_attr).value().get_ptr()); + TraceBuffer* tbuf = static_cast(c->get(tbuf_attr).value().get_ptr()); if (!tbuf && can_alloc) { tbuf = new TraceBuffer(buffersize); c->set(tbuf_attr, Variant(cali_make_variant_from_ptr(tbuf))); - std::lock_guard - g(tbuf_lock); + std::lock_guard g(tbuf_lock); if (tbuf_list) tbuf_list->prev = tbuf; @@ -102,7 +96,8 @@ class Trace return tbuf; } - TraceBuffer* handle_overflow(Caliper* c, Channel* chn, TraceBuffer* tbuf) { + TraceBuffer* handle_overflow(Caliper* c, Channel* chn, TraceBuffer* tbuf) + { switch (policy) { case BufferPolicy::Stop: tbuf->stopped.store(true); @@ -110,36 +105,38 @@ class Trace return 0; case BufferPolicy::Grow: - { - TraceBufferChunk* newchunk = new TraceBufferChunk(buffersize); - - if (!newchunk) { - Log(0).stream() << "Trace: error: unable to allocate new trace buffer. Recording stopped." << std::endl; - tbuf->stopped.store(true); - return 0; - } + { + TraceBufferChunk* newchunk = new TraceBufferChunk(buffersize); + + if (!newchunk) { + Log(0).stream() << "Trace: error: unable to allocate new trace buffer. Recording stopped." + << std::endl; + tbuf->stopped.store(true); + return 0; + } - newchunk->append(tbuf->chunks); - tbuf->chunks = newchunk; + newchunk->append(tbuf->chunks); + tbuf->chunks = newchunk; - return tbuf; - } + return tbuf; + } case BufferPolicy::Flush: - { - Log(1).stream() << chn->name() << ": Trace buffer full: flushing." << std::endl; + { + Log(1).stream() << chn->name() << ": Trace buffer full: flushing." << std::endl; - c->flush_and_write(chn, SnapshotView()); + c->flush_and_write(chn, SnapshotView()); - return tbuf; - } + return tbuf; + } } // switch (policy) return 0; } - void process_snapshot_cb(Caliper* c, Channel* chn, SnapshotView rec) { + void process_snapshot_cb(Caliper* c, Channel* chn, SnapshotView rec) + { TraceBuffer* tbuf = acquire_tbuf(c, chn, !c->is_signal()); if (!tbuf || tbuf->stopped.load()) { @@ -155,15 +152,14 @@ class Trace tbuf->chunks->save_snapshot(rec); } - void flush_cb(Caliper* c, Channel* chn, SnapshotFlushFn proc_fn) { - std::lock_guard - g(flush_lock); + void flush_cb(Caliper* c, Channel* chn, SnapshotFlushFn proc_fn) + { + std::lock_guard g(flush_lock); TraceBuffer* tbuf = nullptr; { - std::lock_guard - g(tbuf_lock); + std::lock_guard g(tbuf_lock); tbuf = tbuf_list; } @@ -183,15 +179,14 @@ class Trace Log(1).stream() << chn->name() << ": Trace: Flushed " << num_written << " snapshots." << std::endl; } - void clear_cb(Caliper* c, Channel* chn) { - std::lock_guard - g(flush_lock); + void clear_cb(Caliper* c, Channel* chn) + { + std::lock_guard g(flush_lock); TraceBuffer* tbuf = nullptr; { - std::lock_guard - g(tbuf_lock); + std::lock_guard g(tbuf_lock); tbuf = tbuf_list; } @@ -204,9 +199,9 @@ class Trace // Accumulate usage statistics before they're reset TraceBufferChunk::UsageInfo info = tbuf->chunks->info(); - aggregate_info.nchunks += info.nchunks; + aggregate_info.nchunks += info.nchunks; aggregate_info.reserved += info.reserved; - aggregate_info.used += info.used; + aggregate_info.used += info.used; tbuf->chunks->reset(); @@ -217,8 +212,7 @@ class Trace TraceBuffer* tmp = tbuf->next; { - std::lock_guard - g(tbuf_lock); + std::lock_guard g(tbuf_lock); tbuf->unlink(); @@ -236,25 +230,20 @@ class Trace } if (Log::verbosity() > 1) { - unitfmt_result bytes_reserved - = unitfmt(aggregate_info.reserved, unitfmt_bytes); - unitfmt_result bytes_used - = unitfmt(aggregate_info.used, unitfmt_bytes); - - Log(2).stream() << chn->name() << ": Trace: " - << bytes_reserved.val << " " - << bytes_reserved.symbol << " reserved, " - << bytes_used.val << " " - << bytes_used.symbol << " used, " - << aggregate_info.nchunks << " chunks." << std::endl; + unitfmt_result bytes_reserved = unitfmt(aggregate_info.reserved, unitfmt_bytes); + unitfmt_result bytes_used = unitfmt(aggregate_info.used, unitfmt_bytes); + + Log(2).stream() << chn->name() << ": Trace: " << bytes_reserved.val << " " << bytes_reserved.symbol + << " reserved, " << bytes_used.val << " " << bytes_used.symbol << " used, " + << aggregate_info.nchunks << " chunks." << std::endl; } } - void init_overflow_policy(const std::string& polname) { - const std::map polmap { - { "grow", BufferPolicy::Grow }, - { "flush", BufferPolicy::Flush }, - { "stop", BufferPolicy::Stop } }; + void init_overflow_policy(const std::string& polname) + { + const std::map polmap { { "grow", BufferPolicy::Grow }, + { "flush", BufferPolicy::Flush }, + { "stop", BufferPolicy::Stop } }; auto it = polmap.find(polname); @@ -264,100 +253,89 @@ class Trace Log(0).stream() << "Trace: error: unknown buffer policy \"" << polname << "\"" << std::endl; } - void create_thread_cb(Caliper* c, Channel* chn) { + void create_thread_cb(Caliper* c, Channel* chn) + { // init trace buffer on new threads acquire_tbuf(c, chn, true); } - void release_thread_cb(Caliper* c, Channel* chn) { + void release_thread_cb(Caliper* c, Channel* chn) + { TraceBuffer* tbuf = acquire_tbuf(c, chn, false); if (tbuf) { tbuf->retired.store(true); - std::lock_guard - g(tbuf_lock); + std::lock_guard g(tbuf_lock); ++num_retired; } } - void finish_cb(Caliper* c, Channel* chn) { + void finish_cb(Caliper* c, Channel* chn) + { if (dropped_snapshots > 0) - Log(1).stream() << chn->name() << ": Trace: dropped " - << dropped_snapshots << " snapshots." << std::endl; + Log(1).stream() << chn->name() << ": Trace: dropped " << dropped_snapshots << " snapshots." << std::endl; if (Log::verbosity() >= 2) - Log(2).stream() << chn->name() << ": Trace: " - << num_acquired << " thread trace buffers acquired, " - << num_retired << " retired, " - << num_released << " released." << std::endl; + Log(2).stream() << chn->name() << ": Trace: " << num_acquired << " thread trace buffers acquired, " + << num_retired << " retired, " << num_released << " released." << std::endl; } - Trace(Caliper* c, Channel* chn) - : dropped_snapshots(0) - { - tbuf_lock.unlock(); - flush_lock.unlock(); + Trace(Caliper* c, Channel* chn) : dropped_snapshots(0) + { + tbuf_lock.unlock(); + flush_lock.unlock(); - ConfigSet cfg = services::init_config_from_spec(chn->config(), s_spec); + ConfigSet cfg = services::init_config_from_spec(chn->config(), s_spec); - init_overflow_policy(cfg.get("buffer_policy").to_string()); - buffersize = cfg.get("buffer_size").to_uint() * 1024 * 1024; + init_overflow_policy(cfg.get("buffer_policy").to_string()); + buffersize = cfg.get("buffer_size").to_uint() * 1024 * 1024; - tbuf_attr = - c->create_attribute(std::string("trace.tbuf.")+std::to_string(chn->id()), - CALI_TYPE_PTR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_HIDDEN); - } + tbuf_attr = c->create_attribute( + std::string("trace.tbuf.") + std::to_string(chn->id()), + CALI_TYPE_PTR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN + ); + } public: static const char* s_spec; ~Trace() - { - // clear all trace buffers - for (TraceBuffer* tbuf = tbuf_list, *tmp = nullptr; tbuf; tbuf = tmp) { - tmp = tbuf->next; - delete tbuf; - } - - tbuf_list = nullptr; + { + // clear all trace buffers + for (TraceBuffer *tbuf = tbuf_list, *tmp = nullptr; tbuf; tbuf = tmp) { + tmp = tbuf->next; + delete tbuf; } - static void trace_register(Caliper* c, Channel* chn) { + tbuf_list = nullptr; + } + + static void trace_register(Caliper* c, Channel* chn) + { Trace* instance = new Trace(c, chn); - chn->events().create_thread_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->create_thread_cb(c, chn); - }); - chn->events().release_thread_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->release_thread_cb(c, chn); - }); - chn->events().process_snapshot.connect( - [instance](Caliper* c, Channel* chn, SnapshotView, SnapshotView rec){ - instance->process_snapshot_cb(c, chn, rec); - }); - chn->events().flush_evt.connect( - [instance](Caliper* c, Channel* chn, SnapshotView, SnapshotFlushFn fn){ - instance->flush_cb(c, chn, fn); - }); - chn->events().clear_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->clear_cb(c, chn); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - // sT.deactivate_chn(chn); - instance->clear_cb(c, chn); - instance->finish_cb(c, chn); - delete instance; - }); + chn->events().create_thread_evt.connect([instance](Caliper* c, Channel* chn) { + instance->create_thread_cb(c, chn); + }); + chn->events().release_thread_evt.connect([instance](Caliper* c, Channel* chn) { + instance->release_thread_cb(c, chn); + }); + chn->events().process_snapshot.connect([instance](Caliper* c, Channel* chn, SnapshotView, SnapshotView rec) { + instance->process_snapshot_cb(c, chn, rec); + }); + chn->events().flush_evt.connect([instance](Caliper* c, Channel* chn, SnapshotView, SnapshotFlushFn fn) { + instance->flush_cb(c, chn, fn); + }); + chn->events().clear_evt.connect([instance](Caliper* c, Channel* chn) { instance->clear_cb(c, chn); }); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + // sT.deactivate_chn(chn); + instance->clear_cb(c, chn); + instance->finish_cb(c, chn); + delete instance; + }); // Initialize trace buffer on master thread instance->acquire_tbuf(c, chn, true); diff --git a/src/services/trace/TraceBufferChunk.cpp b/src/services/trace/TraceBufferChunk.cpp index 26d0dff0e..4f8082bce 100644 --- a/src/services/trace/TraceBufferChunk.cpp +++ b/src/services/trace/TraceBufferChunk.cpp @@ -14,7 +14,6 @@ using namespace trace; using namespace cali; - TraceBufferChunk::~TraceBufferChunk() { delete[] m_data; @@ -23,7 +22,6 @@ TraceBufferChunk::~TraceBufferChunk() delete m_next; } - void TraceBufferChunk::append(TraceBufferChunk* chunk) { if (m_next) @@ -32,7 +30,6 @@ void TraceBufferChunk::append(TraceBufferChunk* chunk) m_next = chunk; } - void TraceBufferChunk::reset() { m_pos = 0; @@ -44,7 +41,6 @@ void TraceBufferChunk::reset() m_next = 0; } - size_t TraceBufferChunk::flush(Caliper* c, SnapshotFlushFn proc_fn) { size_t written = 0; @@ -81,7 +77,6 @@ size_t TraceBufferChunk::flush(Caliper* c, SnapshotFlushFn proc_fn) return written; } - void TraceBufferChunk::save_snapshot(SnapshotView s) { if (s.empty()) @@ -95,7 +90,6 @@ void TraceBufferChunk::save_snapshot(SnapshotView s) ++m_nrec; } - bool TraceBufferChunk::fits(SnapshotView rec) const { // get worst-case estimate of packed snapshot size: @@ -107,7 +101,6 @@ bool TraceBufferChunk::fits(SnapshotView rec) const return (m_pos + max) < m_size; } - TraceBufferChunk::UsageInfo TraceBufferChunk::info() const { UsageInfo info { 0, 0, 0 }; @@ -117,7 +110,7 @@ TraceBufferChunk::UsageInfo TraceBufferChunk::info() const info.nchunks++; info.reserved += m_size; - info.used += m_pos; + info.used += m_pos; return info; } diff --git a/src/services/trace/TraceBufferChunk.h b/src/services/trace/TraceBufferChunk.h index 53c5158b4..1fefae8fd 100644 --- a/src/services/trace/TraceBufferChunk.h +++ b/src/services/trace/TraceBufferChunk.h @@ -10,37 +10,36 @@ namespace trace { - class TraceBufferChunk { - size_t m_size; - size_t m_pos; - size_t m_nrec; - - unsigned char* m_data; +class TraceBufferChunk +{ + size_t m_size; + size_t m_pos; + size_t m_nrec; - TraceBufferChunk* m_next; + unsigned char* m_data; - public: + TraceBufferChunk* m_next; - TraceBufferChunk(size_t s) - : m_size(s), m_pos(0), m_nrec(0), m_data(new unsigned char[s]), m_next(0) - { } +public: - ~TraceBufferChunk(); + TraceBufferChunk(size_t s) : m_size(s), m_pos(0), m_nrec(0), m_data(new unsigned char[s]), m_next(0) {} - void append(TraceBufferChunk* chunk); - void reset(); + ~TraceBufferChunk(); - size_t flush(cali::Caliper* c, cali::SnapshotFlushFn proc_fn); + void append(TraceBufferChunk* chunk); + void reset(); - void save_snapshot(cali::SnapshotView s); - bool fits(cali::SnapshotView s) const; + size_t flush(cali::Caliper* c, cali::SnapshotFlushFn proc_fn); - struct UsageInfo { - size_t nchunks; - size_t reserved; - size_t used; - }; + void save_snapshot(cali::SnapshotView s); + bool fits(cali::SnapshotView s) const; - UsageInfo info() const; + struct UsageInfo { + size_t nchunks; + size_t reserved; + size_t used; }; + + UsageInfo info() const; +}; } // namespace trace diff --git a/src/services/umpire/UmpireStatistics.cpp b/src/services/umpire/UmpireStatistics.cpp index c3f5d59e2..bb80bcad3 100644 --- a/src/services/umpire/UmpireStatistics.cpp +++ b/src/services/umpire/UmpireStatistics.cpp @@ -16,7 +16,6 @@ using namespace cali; - namespace { @@ -33,38 +32,41 @@ class UmpireService Attribute m_timestamp_attr; - Node m_root_node; + Node m_root_node; - bool m_per_allocator_stats; - bool m_record_global_hwm; + bool m_per_allocator_stats; + bool m_record_global_hwm; std::vector m_filter; - bool is_tracked_allocator(const std::string& s) { - return m_filter.empty() || - std::find(m_filter.begin(), m_filter.end(), s) != m_filter.end(); + bool is_tracked_allocator(const std::string& s) + { + return m_filter.empty() || std::find(m_filter.begin(), m_filter.end(), s) != m_filter.end(); } - void process_allocator(Caliper* c, Channel* channel, const std::string& name, umpire::Allocator& alloc, SnapshotView context) { - uint64_t actual_size = alloc.getActualSize(); + void process_allocator( + Caliper* c, + Channel* channel, + const std::string& name, + umpire::Allocator& alloc, + SnapshotView context + ) + { + uint64_t actual_size = alloc.getActualSize(); uint64_t current_size = alloc.getCurrentSize(); - uint64_t hwm = alloc.getHighWatermark(); - uint64_t count = alloc.getAllocationCount(); - - Attribute attr[] = { - m_alloc_name_attr, - m_alloc_actual_size_attr, - m_alloc_current_size_attr, - m_alloc_hwm_attr, - m_alloc_count_attr - }; - Variant data[] = { - Variant(CALI_TYPE_STRING, name.data(), name.size()), - Variant(actual_size), - Variant(current_size), - Variant(hwm), - Variant(count) - }; + uint64_t hwm = alloc.getHighWatermark(); + uint64_t count = alloc.getAllocationCount(); + + Attribute attr[] = { m_alloc_name_attr, + m_alloc_actual_size_attr, + m_alloc_current_size_attr, + m_alloc_hwm_attr, + m_alloc_count_attr }; + Variant data[] = { Variant(CALI_TYPE_STRING, name.data(), name.size()), + Variant(actual_size), + Variant(current_size), + Variant(hwm), + Variant(count) }; FixedSizeSnapshotRecord<64> rec; rec.builder().append(context); @@ -73,7 +75,8 @@ class UmpireService channel->events().process_snapshot(c, channel, SnapshotView(), rec.view()); } - void snapshot(Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec) { + void snapshot(Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec) + { // Bit of a hack: We create one record for each allocator for // allocator-specific info. This way we can use generic allocator.name // and allocator.size attributes. To avoid issues with repeated @@ -108,28 +111,29 @@ class UmpireService auto alloc = rm.getAllocator(s); - total_size += alloc.getCurrentSize(); + total_size += alloc.getCurrentSize(); total_count += alloc.getAllocationCount(); - total_hwm += alloc.getHighWatermark(); + total_hwm += alloc.getHighWatermark(); if (m_per_allocator_stats) process_allocator(c, channel, s, alloc, context.view()); } - rec.append(m_total_size_attr, Variant(total_size)); + rec.append(m_total_size_attr, Variant(total_size)); rec.append(m_total_count_attr, Variant(total_count)); - rec.append(m_total_hwm_attr, Variant(total_hwm)); + rec.append(m_total_hwm_attr, Variant(total_hwm)); } - void record_global_highwatermarks(Caliper* c, Channel* channel) { + void record_global_highwatermarks(Caliper* c, Channel* channel) + { auto& rm = umpire::ResourceManager::getInstance(); for (const auto& s : rm.getAllocatorNames()) { - Attribute attr = - c->create_attribute(std::string("umpire.highwatermark.")+s, - CALI_TYPE_UINT, - CALI_ATTR_GLOBAL | - CALI_ATTR_SKIP_EVENTS); + Attribute attr = c->create_attribute( + std::string("umpire.highwatermark.") + s, + CALI_TYPE_UINT, + CALI_ATTR_GLOBAL | CALI_ATTR_SKIP_EVENTS + ); uint64_t hwm = rm.getAllocator(s).getHighWatermark(); @@ -137,103 +141,89 @@ class UmpireService } } - void finish_cb(Caliper* c, Channel* channel) { - Log(1).stream() << channel->name() << ": Finished Umpire service" - << std::endl; + void finish_cb(Caliper* c, Channel* channel) + { + Log(1).stream() << channel->name() << ": Finished Umpire service" << std::endl; } - void create_attributes(Caliper* c) { - m_alloc_name_attr = - c->create_attribute("umpire.alloc.name", CALI_TYPE_STRING, - CALI_ATTR_SKIP_EVENTS); - m_alloc_current_size_attr = - c->create_attribute("umpire.alloc.current.size", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_alloc_actual_size_attr = - c->create_attribute("umpire.alloc.actual.size", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_alloc_hwm_attr = - c->create_attribute("umpire.alloc.highwatermark", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_alloc_count_attr = - c->create_attribute("umpire.alloc.count", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_total_size_attr = - c->create_attribute("umpire.total.size", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_total_count_attr = - c->create_attribute("umpire.total.count", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); - m_total_hwm_attr = - c->create_attribute("umpire.total.hwm", - CALI_TYPE_UINT, - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); + void create_attributes(Caliper* c) + { + m_alloc_name_attr = c->create_attribute("umpire.alloc.name", CALI_TYPE_STRING, CALI_ATTR_SKIP_EVENTS); + m_alloc_current_size_attr = c->create_attribute( + "umpire.alloc.current.size", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_alloc_actual_size_attr = c->create_attribute( + "umpire.alloc.actual.size", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_alloc_hwm_attr = c->create_attribute( + "umpire.alloc.highwatermark", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_alloc_count_attr = c->create_attribute( + "umpire.alloc.count", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_total_size_attr = c->create_attribute( + "umpire.total.size", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_total_count_attr = c->create_attribute( + "umpire.total.count", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); + m_total_hwm_attr = c->create_attribute( + "umpire.total.hwm", + CALI_TYPE_UINT, + CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); } - UmpireService(Caliper* c, Channel* channel) - : m_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) - { - auto config = services::init_config_from_spec(channel->config(), s_spec); + UmpireService(Caliper* c, Channel* channel) : m_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) + { + auto config = services::init_config_from_spec(channel->config(), s_spec); - m_per_allocator_stats = - config.get("per_allocator_statistics").to_bool(); - m_record_global_hwm = - config.get("record_highwatermarks").to_bool(); - m_filter = - config.get("allocator_filter").to_stringlist(); + m_per_allocator_stats = config.get("per_allocator_statistics").to_bool(); + m_record_global_hwm = config.get("record_highwatermarks").to_bool(); + m_filter = config.get("allocator_filter").to_stringlist(); - create_attributes(c); - } + create_attributes(c); + } public: static const char* s_spec; - static void umpire_register(Caliper* c, Channel* channel) { + static void umpire_register(Caliper* c, Channel* channel) + { UmpireService* instance = new UmpireService(c, channel); - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel*){ - instance->m_timestamp_attr = c->get_attribute("time.offset"); - }); + channel->events().post_init_evt.connect([instance](Caliper* c, Channel*) { + instance->m_timestamp_attr = c->get_attribute("time.offset"); + }); channel->events().snapshot.connect( - [instance](Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec){ + [instance](Caliper* c, Channel* channel, SnapshotView info, SnapshotBuilder& rec) { instance->snapshot(c, channel, info, rec); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->finish_cb(c, channel); - delete instance; - }); + } + ); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + instance->finish_cb(c, channel); + delete instance; + }); if (instance->m_record_global_hwm) - channel->events().pre_flush_evt.connect( - [instance](Caliper* c, Channel* channel, SnapshotView){ - instance->record_global_highwatermarks(c, channel); - }); + channel->events().pre_flush_evt.connect([instance](Caliper* c, Channel* channel, SnapshotView) { + instance->record_global_highwatermarks(c, channel); + }); - Log(1).stream() << channel->name() << ": Registered umpire service" - << std::endl; + Log(1).stream() << channel->name() << ": Registered umpire service" << std::endl; } }; @@ -260,7 +250,7 @@ const char* UmpireService::s_spec = R"json( } )json"; -} // namespace [anonymous] +} // namespace namespace cali { diff --git a/src/services/util/ChannelList.hpp b/src/services/util/ChannelList.hpp index 5e99453b0..33605cdd4 100644 --- a/src/services/util/ChannelList.hpp +++ b/src/services/util/ChannelList.hpp @@ -24,24 +24,27 @@ struct ChannelList { ChannelList* next; ChannelList* prev; - inline void unlink() { + inline void unlink() + { if (next) next->prev = prev; if (prev) prev->next = next; } - inline static void add(ChannelList** head, cali::Channel& chn) { + inline static void add(ChannelList** head, cali::Channel& chn) + { ChannelList* ret = new ChannelList { chn, nullptr, nullptr }; if (*head) (*head)->prev = ret; ret->next = *head; - *head = ret; + *head = ret; } - inline static void remove(ChannelList** head, cali::Channel& chn) { + inline static void remove(ChannelList** head, cali::Channel& chn) + { ChannelList* p = *head; while (p && p->channel != chn) diff --git a/src/services/validator/validator.cpp b/src/services/validator/validator.cpp index 29b5d382c..90771cec5 100644 --- a/src/services/validator/validator.cpp +++ b/src/services/validator/validator.cpp @@ -24,7 +24,6 @@ using namespace cali; - namespace { @@ -51,11 +50,12 @@ class ValidatorService class StackValidator { - std::map< Attribute, std::vector > m_region_stack; - bool m_error_found; - Node m_root_node; + std::map> m_region_stack; + bool m_error_found; + Node m_root_node; - void push(Caliper* c, const Attribute& attr, const Variant& value) { + void push(Caliper* c, const Attribute& attr, const Variant& value) + { Variant v_copy = value; if (!attr.store_as_value()) @@ -64,7 +64,8 @@ class ValidatorService m_region_stack[attr].push_back(v_copy); } - Variant pop(const Attribute& attr) { + Variant pop(const Attribute& attr) + { Variant ret; auto it = m_region_stack.find(attr); @@ -84,7 +85,8 @@ class ValidatorService public: - bool check_begin(Caliper* c, const Attribute& attr, const Variant& value) { + bool check_begin(Caliper* c, const Attribute& attr, const Variant& value) + { if (m_error_found) return true; @@ -96,7 +98,8 @@ class ValidatorService return false; } - bool check_end(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + bool check_end(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if (m_error_found) return true; @@ -109,11 +112,12 @@ class ValidatorService m_error_found = true; - print_snapshot(c, chn, - Log(0).stream() << "validator: end(\"" - << attr.name() << "\"=\"" << value.to_string() << "\") " - << " has no matching begin().\n context: " ) - << std::endl; + print_snapshot( + c, + chn, + Log(0).stream() << "validator: end(\"" << attr.name() << "\"=\"" << value.to_string() << "\") " + << " has no matching begin().\n context: " + ) << std::endl; } else { Variant v_stack_attr; @@ -123,38 +127,39 @@ class ValidatorService if (attr.is_nested() && attr.id() != v_stack_attr.to_id()) { m_error_found = true; - print_snapshot(c, chn, - Log(0).stream() << "validator: incorrect nesting: trying to end \"" - << attr.name() << "\"=\"" << value.to_string() - << "\" but current attribute is \"" - << c->get_attribute(v_stack_attr.to_id()).name() - << "\".\n context: " ) - << std::endl; + print_snapshot( + c, + chn, + Log(0).stream() << "validator: incorrect nesting: trying to end \"" << attr.name() << "\"=\"" + << value.to_string() << "\" but current attribute is \"" + << c->get_attribute(v_stack_attr.to_id()).name() << "\".\n context: " + ) << std::endl; } else if (!(value == v_stack_val)) { m_error_found = true; - print_snapshot(c, chn, - Log(0).stream() << "validator: incorrect nesting: trying to end \"" - << attr.name() << "\"=\"" << value.to_string() - << "\" but current value is \"" - << v_stack_val.to_string() << "\".\n context: " ) - << std::endl; + print_snapshot( + c, + chn, + Log(0).stream() << "validator: incorrect nesting: trying to end \"" << attr.name() << "\"=\"" + << value.to_string() << "\" but current value is \"" << v_stack_val.to_string() + << "\".\n context: " + ) << std::endl; } } return m_error_found; } - bool check_final() { - for (auto const &p : m_region_stack) { + bool check_final() + { + for (auto const& p : m_region_stack) { if (p.first == s_class_nested_attr) continue; if (!p.second.empty()) { std::ostringstream os; - os << "validator: Regions not closed: " - << p.first.name() << "="; + os << "validator: Regions not closed: " << p.first.name() << "="; int cv = 0; for (const Variant& v : p.second) @@ -169,34 +174,29 @@ class ValidatorService return m_error_found; } - StackValidator() - : m_error_found(false), m_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) - { } + StackValidator() : m_error_found(false), m_root_node(CALI_INV_ID, CALI_INV_ID, Variant()) {} }; // class StackValidator + StackValidator* proc_stack = nullptr; + std::mutex proc_stack_mutex; - StackValidator* proc_stack = nullptr; - std::mutex proc_stack_mutex; + std::atomic global_errors; - std::atomic global_errors; - - Attribute thread_stack_attr; + Attribute thread_stack_attr; std::vector thread_stacks; - std::mutex thread_stacks_mutex; - + std::mutex thread_stacks_mutex; - StackValidator* aquire_thread_stack(Caliper* c, Channel* chn) { - StackValidator* tstack = - static_cast(c->get(thread_stack_attr).value().get_ptr()); + StackValidator* aquire_thread_stack(Caliper* c, Channel* chn) + { + StackValidator* tstack = static_cast(c->get(thread_stack_attr).value().get_ptr()); if (!tstack) { tstack = new StackValidator; c->set(thread_stack_attr, Variant(cali_make_variant_from_ptr(tstack))); - std::lock_guard - g(thread_stacks_mutex); + std::lock_guard g(thread_stacks_mutex); thread_stacks.push_back(tstack); } @@ -204,10 +204,10 @@ class ValidatorService return tstack; } - void finalize_cb(Caliper*, Channel* chn) { + void finalize_cb(Caliper*, Channel* chn) + { { - std::lock_guard - g(proc_stack_mutex); + std::lock_guard g(proc_stack_mutex); if (proc_stack->check_final()) ++global_errors; @@ -217,8 +217,7 @@ class ValidatorService } { - std::lock_guard - g(thread_stacks_mutex); + std::lock_guard g(thread_stacks_mutex); for (StackValidator* v : thread_stacks) { if (v->check_final()) @@ -231,17 +230,15 @@ class ValidatorService } if (global_errors.load() > 0) - Log(0).stream() << "validator: Annotation nesting errors found" - << std::endl; + Log(0).stream() << "validator: Annotation nesting errors found" << std::endl; else - Log(1).stream() << "validator: No annotation nesting errors found" - << std::endl; + Log(1).stream() << "validator: No annotation nesting errors found" << std::endl; } - void begin_cb(Caliper* c, Channel* chn, const Attribute &attr, const Variant& value) { + void begin_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if ((attr.properties() & CALI_ATTR_SCOPE_MASK) == CALI_ATTR_SCOPE_PROCESS) { - std::lock_guard - g(proc_stack_mutex); + std::lock_guard g(proc_stack_mutex); if (proc_stack->check_begin(c, attr, value)) ++global_errors; @@ -253,10 +250,10 @@ class ValidatorService } } - void end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void end_cb(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if ((attr.properties() & CALI_ATTR_SCOPE_MASK) == CALI_ATTR_SCOPE_PROCESS) { - std::lock_guard - g(proc_stack_mutex); + std::lock_guard g(proc_stack_mutex); if (proc_stack->check_end(c, chn, attr, value)) ++global_errors; @@ -268,21 +265,19 @@ class ValidatorService } } - ValidatorService(Caliper* c, Channel* chn) - : proc_stack(new StackValidator), global_errors(0) - { - thread_stack_attr = - c->create_attribute(std::string("validator.stack.")+std::to_string(chn->id()), - CALI_TYPE_PTR, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_HIDDEN); - } + ValidatorService(Caliper* c, Channel* chn) : proc_stack(new StackValidator), global_errors(0) + { + thread_stack_attr = c->create_attribute( + std::string("validator.stack.") + std::to_string(chn->id()), + CALI_TYPE_PTR, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_HIDDEN + ); + } public: - static void validator_register(Caliper* c, Channel* chn) { + static void validator_register(Caliper* c, Channel* chn) + { if (!s_class_nested_attr) s_class_nested_attr = c->create_attribute("validator.nested", CALI_TYPE_UINT, CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN); @@ -290,28 +285,27 @@ class ValidatorService ValidatorService* instance = new ValidatorService(c, chn); chn->events().pre_begin_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ + [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { instance->begin_cb(c, chn, attr, value); - }); + } + ); chn->events().pre_end_evt.connect( - [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value){ + [instance](Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { instance->end_cb(c, chn, attr, value); - }); - chn->events().finish_evt.connect( - [instance](Caliper* c, Channel* chn){ - instance->finalize_cb(c, chn); - delete instance; - }); + } + ); + chn->events().finish_evt.connect([instance](Caliper* c, Channel* chn) { + instance->finalize_cb(c, chn); + delete instance; + }); Log(1).stream() << chn->name() << ": Registered validator service." << std::endl; } - }; Attribute ValidatorService::s_class_nested_attr { Attribute() }; -} // namespace [anonymous] - +} // namespace namespace cali { diff --git a/src/services/variorum/Variorum.cpp b/src/services/variorum/Variorum.cpp index 05106cd79..484713a05 100644 --- a/src/services/variorum/Variorum.cpp +++ b/src/services/variorum/Variorum.cpp @@ -14,7 +14,8 @@ #include "caliper/common/RuntimeConfig.h" #include "caliper/common/Log.h" -extern "C" { +extern "C" +{ #include #include } @@ -26,22 +27,20 @@ extern "C" { using namespace cali; - namespace { // Power measurement function std::tuple measure(const std::string& name) { - double power_watts; - json_t *power_obj = NULL; - char *s = NULL; + double power_watts; + json_t* power_obj = NULL; + char* s = NULL; - s = (char *) malloc(800 * sizeof(char)); + s = (char*) malloc(800 * sizeof(char)); int ret = variorum_get_node_power_json(&s); - if (ret != 0) - { + if (ret != 0) { std::cout << "Variorum JSON API failed" << std::endl; uint64_t val; return std::make_tuple(false, val); @@ -51,10 +50,10 @@ std::tuple measure(const std::string& name) // TODO: Assume 1 rank/node for aggregation // Extract and print values from JSON object - power_obj = json_loads(s, JSON_DECODE_ANY, NULL); + power_obj = json_loads(s, JSON_DECODE_ANY, NULL); power_watts = json_real_value(json_object_get(power_obj, name.c_str())); - uint64_t val = (uint64_t)power_watts; + uint64_t val = (uint64_t) power_watts; // Deallocate the string free(s); @@ -76,13 +75,12 @@ class VariorumService // Configuration variables for the variorum service static const ConfigSet::Entry s_configdata[]; - struct MeasurementInfo - { + struct MeasurementInfo { std::string domain; // Measurement name / ID Attribute value_attr; // Attribute for the measurement value Attribute delta_attr; // Attribute for the delta value (difference // since last snapshot) - Attribute prval_attr; // A hidden attribute to store the previous + Attribute prval_attr; // A hidden attribute to store the previous // measurement value on the Caliper blackboard }; @@ -92,10 +90,7 @@ class VariorumService // Number of measurement errors encountered at runtime unsigned m_num_errors; - void snapshot_cb(Caliper* c, - Channel* /*channel*/, - SnapshotView /*trigger_info*/, - SnapshotBuilder& rec) + void snapshot_cb(Caliper* c, Channel* /*channel*/, SnapshotView /*trigger_info*/, SnapshotBuilder& rec) { // The snapshot callback triggers performance measurements. // Measurement services should make measurements and add them to the @@ -107,17 +102,15 @@ class VariorumService // handler. // Make measurements for all configured variables - for (const MeasurementInfo& m : m_info) - { - bool success; + for (const MeasurementInfo& m : m_info) { + bool success; uint64_t val; std::tie(success, val) = measure(m.domain); // Check for measurement errors. Best practice is to count and // report them at the end rather than printing error messages at // runtime. - if (!success) - { + if (!success) { ++m_num_errors; continue; } @@ -134,7 +127,7 @@ class VariorumService // TODO: For aggregation, we use average power instead of // difference. Variant v_prev = c->exchange(m.prval_attr, v_val); - rec.append(m.delta_attr, cali_make_variant_from_uint((val + v_prev.to_uint())/2)); + rec.append(m.delta_attr, cali_make_variant_from_uint((val + v_prev.to_uint()) / 2)); } } @@ -153,11 +146,8 @@ class VariorumService // and print any debug output. Do NOT use Caliper API calls here, as // the services they rely on may already be destroyed. - if (m_num_errors > 0) - { - Log(0).stream() << channel->name() << ": variorum: " - << m_num_errors << " measurement errors!" - << std::endl; + if (m_num_errors > 0) { + Log(0).stream() << channel->name() << ": variorum: " << m_num_errors << " measurement errors!" << std::endl; } } @@ -180,30 +170,24 @@ class VariorumService // the Caliper context tree). Use SKIP_EVENTS to avoid triggering // events when using set/begin/end on this attribute. This attribute // is for absolute measurement values for . - auto domainList = - channel->config().init("variorum", s_configdata).get("domains").to_stringlist(","); - - for (auto &domain : domainList) - { - m.value_attr = - c->create_attribute(std::string("variorum.val.") + domain, - CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); + auto domainList = channel->config().init("variorum", s_configdata).get("domains").to_stringlist(","); + + for (auto& domain : domainList) { + m.value_attr = c->create_attribute( + std::string("variorum.val.") + domain, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); // The delta attribute stores the difference of the measurement // value since the last snapshot. We add the "aggregatable" // property here, which lets Caliper aggregate these values // automatically. - m.delta_attr = - c->create_attribute(std::string("variorum.") + domain, - CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_SKIP_EVENTS | - CALI_ATTR_AGGREGATABLE); + m.delta_attr = c->create_attribute( + std::string("variorum.") + domain, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_SKIP_EVENTS | CALI_ATTR_AGGREGATABLE + ); // We use a hidden attribute to store the previous measurement // for on Caliper's per-thread blackboard. This is a @@ -213,20 +197,17 @@ class VariorumService // In case more thread-specific information must be stored, it is // better to combine them in a structure and create a CALI_TYPE_PTR // attribute for this thread info in the service instance. - m.prval_attr = - c->create_attribute(std::string("variorum.pv.") + std::to_string(channel->id()) + domain, - CALI_TYPE_UINT, - CALI_ATTR_SCOPE_THREAD | - CALI_ATTR_ASVALUE | - CALI_ATTR_HIDDEN | - CALI_ATTR_SKIP_EVENTS); - } + m.prval_attr = c->create_attribute( + std::string("variorum.pv.") + std::to_string(channel->id()) + domain, + CALI_TYPE_UINT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE | CALI_ATTR_HIDDEN | CALI_ATTR_SKIP_EVENTS + ); + } return m; } - VariorumService(Caliper* c, Channel* channel) - : m_num_errors(0) + VariorumService(Caliper* c, Channel* channel) : m_num_errors(0) { // Get the service configuration. This reads the configuration // variables defined in s_configdata from the environment, config @@ -240,19 +221,16 @@ class VariorumService // (comma-separated list). Returns a std::vector. auto domainList = config.get("domains").to_stringlist(","); - if (domainList.empty()) - { - Log(1).stream() << channel->name() - << ": variorum: No domains specified, dropping variorum service" + if (domainList.empty()) { + Log(1).stream() << channel->name() << ": variorum: No domains specified, dropping variorum service" << std::endl; return; } // Create a MeasurementInfo entry for each of the "measurement // variables" in the configuration. - for (const std::string& domain : domainList) - { - m_info.push_back( create_measurement_info(c, channel, domain) ); + for (const std::string& domain : domainList) { + m_info.push_back(create_measurement_info(c, channel, domain)); } } @@ -276,13 +254,10 @@ class VariorumService static void register_variorum(Caliper* c, Channel* channel) { - auto domainList = - channel->config().init("variorum", s_configdata).get("domains").to_stringlist(","); + auto domainList = channel->config().init("variorum", s_configdata).get("domains").to_stringlist(","); - if (domainList.empty()) - { - Log(1).stream() << channel->name() - << ": variorum: No domains specified, dropping variorum service" + if (domainList.empty()) { + Log(1).stream() << channel->name() << ": variorum: No domains specified, dropping variorum service" << std::endl; return; } @@ -291,38 +266,34 @@ class VariorumService VariorumService* instance = new VariorumService(c, channel); // Register callback functions using lambdas - channel->events().post_init_evt.connect( - [instance](Caliper* c, Channel* channel){ - instance->post_init_cb(c, channel); - }); + channel->events().post_init_evt.connect([instance](Caliper* c, Channel* channel) { + instance->post_init_cb(c, channel); + }); channel->events().snapshot.connect( - [instance](Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec){ + [instance](Caliper* c, Channel* channel, SnapshotView trigger_info, SnapshotBuilder& rec) { instance->snapshot_cb(c, channel, trigger_info, rec); - }); - channel->events().finish_evt.connect( - [instance](Caliper* c, Channel* channel){ - // This callback is invoked when the channel is destroyed. - // No other callback will be invoked afterwards. - // Delete the channel's service instance here! - instance->finish_cb(c, channel); - delete instance; - }); - - Log(1).stream() << channel->name() << ": Registered variorum service" - << std::endl; + } + ); + channel->events().finish_evt.connect([instance](Caliper* c, Channel* channel) { + // This callback is invoked when the channel is destroyed. + // No other callback will be invoked afterwards. + // Delete the channel's service instance here! + instance->finish_cb(c, channel); + delete instance; + }); + + Log(1).stream() << channel->name() << ": Registered variorum service" << std::endl; } }; const ConfigSet::Entry VariorumService::s_configdata[] = { - { - "domains", // config variable name - CALI_TYPE_STRING, // datatype - "", // default value + { "domains", // config variable name + CALI_TYPE_STRING, // datatype + "", // default value "List of domains to record", // short description // long description "List of domains to record (separated by ',')\n" - "Example: power_node_watts, power_socket_watts, power_gpu_watts, power_mem_watts" - }, + "Example: power_node_watts, power_socket_watts, power_gpu_watts, power_mem_watts" }, ConfigSet::Terminator }; @@ -331,8 +302,6 @@ const ConfigSet::Entry VariorumService::s_configdata[] = { namespace cali { -CaliperService variorum_service = { - "variorum", ::VariorumService::register_variorum -}; +CaliperService variorum_service = { "variorum", ::VariorumService::register_variorum }; } // namespace cali diff --git a/src/services/vtune/VTuneBindings.cpp b/src/services/vtune/VTuneBindings.cpp index 43db85d26..443ac01da 100644 --- a/src/services/vtune/VTuneBindings.cpp +++ b/src/services/vtune/VTuneBindings.cpp @@ -26,9 +26,10 @@ class ITTBinding : public cali::AnnotationBinding // Caliper gets its string storage. static thread_local std::map s_itt_strings; - inline __itt_domain* get_itt_domain(const Attribute& attr) { - __itt_domain* domain = nullptr; - cali_id_t attr_id = attr.id(); + inline __itt_domain* get_itt_domain(const Attribute& attr) + { + __itt_domain* domain = nullptr; + cali_id_t attr_id = attr.id(); { auto it = s_itt_domains.lower_bound(attr_id); @@ -44,12 +45,13 @@ class ITTBinding : public cali::AnnotationBinding return domain; } - inline __itt_string_handle* get_itt_string(const Variant& val) { + inline __itt_string_handle* get_itt_string(const Variant& val) + { __itt_string_handle* itt_str = nullptr; { const char* str = static_cast(val.data()); - auto it = s_itt_strings.lower_bound(str); + auto it = s_itt_strings.lower_bound(str); if (it == s_itt_strings.end() || it->first != str) { itt_str = __itt_string_handle_create(str); @@ -62,27 +64,27 @@ class ITTBinding : public cali::AnnotationBinding return itt_str; } - public: const char* service_tag() const { return "vtune"; }; - void on_begin(Caliper* c, Channel*, const Attribute& attr, const Variant& value) { + void on_begin(Caliper* c, Channel*, const Attribute& attr, const Variant& value) + { if (attr.type() == CALI_TYPE_STRING) __itt_task_begin(get_itt_domain(attr), __itt_null, __itt_null, get_itt_string(value)); } - void on_end(Caliper* c, Channel*, const Attribute& attr, const Variant& value) { + void on_end(Caliper* c, Channel*, const Attribute& attr, const Variant& value) + { if (attr.type() == CALI_TYPE_STRING) __itt_task_end(get_itt_domain(attr)); } }; -thread_local std::map ITTBinding::s_itt_domains; +thread_local std::map ITTBinding::s_itt_domains; thread_local std::map ITTBinding::s_itt_strings; -} // namespace [anonymous] - +} // namespace namespace cali { diff --git a/src/tools/cali-query/AttributeExtract.cpp b/src/tools/cali-query/AttributeExtract.cpp index e58ccfeba..cc15b7626 100644 --- a/src/tools/cali-query/AttributeExtract.cpp +++ b/src/tools/cali-query/AttributeExtract.cpp @@ -8,14 +8,14 @@ using namespace cali; -struct AttributeExtract::AttributeExtractImpl -{ - SnapshotProcessFn m_snap_fn; - Attribute m_id_attr; +struct AttributeExtract::AttributeExtractImpl { + SnapshotProcessFn m_snap_fn; + Attribute m_id_attr; static const cali_id_t s_attr_id; // The "attribute" attribute id - void process_node(CaliperMetadataAccessInterface& db, const Node* node) { + void process_node(CaliperMetadataAccessInterface& db, const Node* node) + { if (node->attribute() != s_attr_id) return; @@ -24,29 +24,26 @@ struct AttributeExtract::AttributeExtractImpl EntryList rec { Entry(m_id_attr, node->id()) }; - for ( ; node && node->id() != CALI_INV_ID; node = node->parent()) + for (; node && node->id() != CALI_INV_ID; node = node->parent()) rec.push_back(Entry(db.get_attribute(node->attribute()), node->data())); m_snap_fn(db, rec); } - AttributeExtractImpl(SnapshotProcessFn snap_fn) - : m_snap_fn(snap_fn) - { } + AttributeExtractImpl(SnapshotProcessFn snap_fn) : m_snap_fn(snap_fn) {} }; const cali_id_t AttributeExtract::AttributeExtractImpl::s_attr_id = 8; -AttributeExtract::AttributeExtract(SnapshotProcessFn snap_fn) - : mP { new AttributeExtractImpl(snap_fn) } -{ } +AttributeExtract::AttributeExtract(SnapshotProcessFn snap_fn) : mP { new AttributeExtractImpl(snap_fn) } +{} AttributeExtract::~AttributeExtract() { mP.reset(); } -void AttributeExtract::operator()(CaliperMetadataAccessInterface& db, const Node* node) +void AttributeExtract::operator() (CaliperMetadataAccessInterface& db, const Node* node) { mP->process_node(db, node); } diff --git a/src/tools/cali-query/AttributeExtract.h b/src/tools/cali-query/AttributeExtract.h index c89174d84..c39ed8ee8 100644 --- a/src/tools/cali-query/AttributeExtract.h +++ b/src/tools/cali-query/AttributeExtract.h @@ -26,7 +26,7 @@ class AttributeExtract ~AttributeExtract(); - void operator()(CaliperMetadataAccessInterface&, const Node*); + void operator() (CaliperMetadataAccessInterface&, const Node*); }; -} +} // namespace cali diff --git a/src/tools/cali-query/cali-query.cpp b/src/tools/cali-query/cali-query.cpp index 31b6de1f4..332ed8418 100644 --- a/src/tools/cali-query/cali-query.cpp +++ b/src/tools/cali-query/cali-query.cpp @@ -37,136 +37,122 @@ using namespace cali; using namespace std; using namespace util; - namespace { - const char* usage = "cali-query [OPTION]... [FILE]..." - "\n Read, merge, and filter caliper streams"; - - const Args::Table option_table[] = { - // name, longopt name, shortopt char, has argument, info, argument info - { "select", "select", 's', true, - "Filter records by selected attributes: [-]attribute[(<|>|=)value][:...]", - "QUERY_STRING" - }, - { "aggregate", "aggregate", 0, true, - "Aggregate snapshots using the given aggregation operators: (sum(attribute)|count)[:...]", - "AGGREGATION_OPS" - }, - { "aggregate-key", "aggregate-key", 0, true, - "List of attributes to aggregate over (collapses all other attributes): attribute[:...]", - "ATTRIBUTES" - }, - { "expand", "expand", 'e', false, - "Print records as comma-separated key=value lists", - nullptr - }, - { "attributes", "print-attributes", 0, true, - "Select attributes to print (or hide) in expanded output: [-]attribute[:...]", - "ATTRIBUTES" - }, - { "sort", "sort-by", 'S', true, - "Sort rows in table format: attribute[:...]", - "SORT_ATTRIBUTES" - }, - { "format", "format", 'f', true, - "Format output according to format string: %[]attr_name%...", - "FORMAT_STRING" - }, - { "title", "title", 0, true, - "Set the title row for formatted output", - "STRING" - }, - { "table", "table", 't', false, - "Print records in human-readable table form", - nullptr - }, - { "tree" , "tree", 'T', false, - "Print records in a tree based on the hierarchy of the selected path attributes", - nullptr - }, - { "path-attributes", "path-attributes", 0, true, - "Select the path attributes for tree printers", - "ATTRIBUTES" - }, - { "json", "json", 'j', false, - "Print given attributes in web-friendly json format", - "ATTRIBUTES" - }, - { "threads", "threads", 0, true, - "Use this many threads (applicable only with multiple files)", - "THREADS" - }, - { "query", "query", 'q', true, - "Execute a query in CalQL format", - "QUERY STRING" - }, - { "query-file", "query-file", 'Q', true, - "Read a CalQL query from a file", - "FILENAME" - }, - { "caliper-config", "caliper-config", 'P', true, - "Set Caliper configuration for profiling cali-query", - "CALIPER-CONFIG" - }, - { "verbose", "verbose", 'v', false, "Be verbose.", nullptr }, - { "version", "version", 'V', false, "Print version number", nullptr }, - { "output", "output", 'o', true, "Set the output file name", "FILE" }, - { "help", "help", 'h', true, "Print help message", nullptr }, - { "list-attributes", "list-attributes", 0, false, - "List attribute info. Use with -j, -t, etc. to select output format.", - nullptr - }, - { "list-globals", "list-globals", 'G', false, - "List global run metadata. Use with -j, -t, etc. to select output format.", - nullptr - }, - Args::Terminator - }; - - /// A node record filter that filters redundant identical node records. - /// Redundant node records can occur when merging/unifying two streams. - class FilterDuplicateNodes { - cali_id_t m_max_node; - - public: +const char* usage = + "cali-query [OPTION]... [FILE]..." + "\n Read, merge, and filter caliper streams"; + +const Args::Table option_table[] = { + // name, longopt name, shortopt char, has argument, info, argument info + { "select", + "select", + 's', + true, + "Filter records by selected attributes: [-]attribute[(<|>|=)value][:...]", + "QUERY_STRING" }, + { "aggregate", + "aggregate", + 0, + true, + "Aggregate snapshots using the given aggregation operators: (sum(attribute)|count)[:...]", + "AGGREGATION_OPS" }, + { "aggregate-key", + "aggregate-key", + 0, + true, + "List of attributes to aggregate over (collapses all other attributes): attribute[:...]", + "ATTRIBUTES" }, + { "expand", "expand", 'e', false, "Print records as comma-separated key=value lists", nullptr }, + { "attributes", + "print-attributes", + 0, + true, + "Select attributes to print (or hide) in expanded output: [-]attribute[:...]", + "ATTRIBUTES" }, + { "sort", "sort-by", 'S', true, "Sort rows in table format: attribute[:...]", "SORT_ATTRIBUTES" }, + { "format", + "format", + 'f', + true, + "Format output according to format string: %[]attr_name%...", + "FORMAT_STRING" }, + { "title", "title", 0, true, "Set the title row for formatted output", "STRING" }, + { "table", "table", 't', false, "Print records in human-readable table form", nullptr }, + { "tree", + "tree", + 'T', + false, + "Print records in a tree based on the hierarchy of the selected path attributes", + nullptr }, + { "path-attributes", "path-attributes", 0, true, "Select the path attributes for tree printers", "ATTRIBUTES" }, + { "json", "json", 'j', false, "Print given attributes in web-friendly json format", "ATTRIBUTES" }, + { "threads", "threads", 0, true, "Use this many threads (applicable only with multiple files)", "THREADS" }, + { "query", "query", 'q', true, "Execute a query in CalQL format", "QUERY STRING" }, + { "query-file", "query-file", 'Q', true, "Read a CalQL query from a file", "FILENAME" }, + { "caliper-config", + "caliper-config", + 'P', + true, + "Set Caliper configuration for profiling cali-query", + "CALIPER-CONFIG" }, + { "verbose", "verbose", 'v', false, "Be verbose.", nullptr }, + { "version", "version", 'V', false, "Print version number", nullptr }, + { "output", "output", 'o', true, "Set the output file name", "FILE" }, + { "help", "help", 'h', true, "Print help message", nullptr }, + { "list-attributes", + "list-attributes", + 0, + false, + "List attribute info. Use with -j, -t, etc. to select output format.", + nullptr }, + { "list-globals", + "list-globals", + 'G', + false, + "List global run metadata. Use with -j, -t, etc. to select output format.", + nullptr }, + Args::Terminator +}; + +/// A node record filter that filters redundant identical node records. +/// Redundant node records can occur when merging/unifying two streams. +class FilterDuplicateNodes +{ + cali_id_t m_max_node; - FilterDuplicateNodes() - : m_max_node { 0 } - { } +public: - void operator()(CaliperMetadataAccessInterface& db, const Node* node, NodeProcessFn push) { - cali_id_t id = node->id(); + FilterDuplicateNodes() : m_max_node { 0 } {} - if (id != CALI_INV_ID) { - if (id < m_max_node) { - return; - } else - m_max_node = id; - } + void operator() (CaliperMetadataAccessInterface& db, const Node* node, NodeProcessFn push) + { + cali_id_t id = node->id(); - push(db, node); + if (id != CALI_INV_ID) { + if (id < m_max_node) { + return; + } else + m_max_node = id; } - }; + push(db, node); + } +}; - /// NodeFilterStep helper struct - /// Basically the chain link in the processing chain. - /// Passes result of @param m_filter_fn to @param m_push_fn - struct NodeFilterStep { - NodeFilterFn m_filter_fn; ///< This processing step - NodeProcessFn m_push_fn; ///< Next processing step +/// NodeFilterStep helper struct +/// Basically the chain link in the processing chain. +/// Passes result of @param m_filter_fn to @param m_push_fn +struct NodeFilterStep { + NodeFilterFn m_filter_fn; ///< This processing step + NodeProcessFn m_push_fn; ///< Next processing step - NodeFilterStep(NodeFilterFn filter_fn, NodeProcessFn push_fn) - : m_filter_fn { filter_fn }, m_push_fn { push_fn } - { } + NodeFilterStep(NodeFilterFn filter_fn, NodeProcessFn push_fn) : m_filter_fn { filter_fn }, m_push_fn { push_fn } {} - void operator ()(CaliperMetadataAccessInterface& db, const Node* node) { - m_filter_fn(db, node, m_push_fn); - } - }; + void operator() (CaliperMetadataAccessInterface& db, const Node* node) { m_filter_fn(db, node, m_push_fn); } +}; -} +} // namespace const char* progress_config_spec = "{" @@ -222,8 +208,7 @@ int main(int argc, const char* argv[]) int i = args.parse(argc, argv); if (i < argc) { - cerr << "cali-query: error: unknown option: " << argv[i] << '\n' - << " Available options: "; + cerr << "cali-query: error: unknown option: " << argv[i] << '\n' << " Available options: "; args.print_available_options(cerr); @@ -246,8 +231,7 @@ int main(int argc, const char* argv[]) mgr.add(args.get("caliper-config").c_str()); if (mgr.error()) { - std::cerr << "cali-query: Caliper config parse error: " - << mgr.error_msg() << std::endl; + std::cerr << "cali-query: Caliper config parse error: " << mgr.error_msg() << std::endl; return -1; } @@ -276,23 +260,27 @@ int main(int argc, const char* argv[]) // --- Build up processing chain (from back to front) // - QueryArgsParser query_parser; + QueryArgsParser query_parser; if (!query_parser.parse_args(args)) { cerr << "cali-query: Invalid query: " << query_parser.error_msg() << std::endl; return -2; } - QuerySpec spec = query_parser.spec(); + QuerySpec spec = query_parser.spec(); // setup format spec - FormatProcessor format(spec, stream); + FormatProcessor format(spec, stream); - NodeProcessFn node_proc = [](CaliperMetadataAccessInterface&,const Node*) { return; }; - SnapshotProcessFn snap_proc = [](CaliperMetadataAccessInterface&,const EntryList&){ return; }; + NodeProcessFn node_proc = [](CaliperMetadataAccessInterface&, const Node*) { + return; + }; + SnapshotProcessFn snap_proc = [](CaliperMetadataAccessInterface&, const EntryList&) { + return; + }; - Aggregator aggregate(spec); + Aggregator aggregate(spec); if (!args.is_set("list-globals")) { if (spec.aggregate.selection == QuerySpec::AggregationSelection::None) @@ -303,11 +291,13 @@ int main(int argc, const char* argv[]) if (spec.filter.selection == QuerySpec::FilterSelection::List) snap_proc = SnapshotFilterStep(RecordSelector(spec), snap_proc); if (!spec.preprocess_ops.empty()) - snap_proc = SnapshotFilterStep(Preprocessor(spec), snap_proc); + snap_proc = SnapshotFilterStep(Preprocessor(spec), snap_proc); if (args.is_set("list-attributes")) { node_proc = AttributeExtract(snap_proc); - snap_proc = [](CaliperMetadataAccessInterface&,const EntryList&){ return; }; + snap_proc = [](CaliperMetadataAccessInterface&, const EntryList&) { + return; + }; } } @@ -318,14 +308,11 @@ int main(int argc, const char* argv[]) if (files.empty()) files.push_back(""); // read from stdin if no files are given - unsigned num_threads = - std::min(files.size(), std::stoul(args.get("threads", "4"))); + unsigned num_threads = std::min(files.size(), std::stoul(args.get("threads", "4"))); if (verbose) - std::cerr << "cali-query: Processing " << files.size() - << " files using " - << num_threads << " thread" << (num_threads == 1 ? "." : "s.") - << std::endl; + std::cerr << "cali-query: Processing " << files.size() << " files using " << num_threads << " thread" + << (num_threads == 1 ? "." : "s.") << std::endl; cali_set_global_int_byname("cali-query.num-threads", num_threads); @@ -345,18 +332,15 @@ int main(int argc, const char* argv[]) metadb.add_attribute_units(spec.units); auto thread_fn = [&](unsigned t) { - Annotation::Guard - g_t(Annotation("thread", CALI_ATTR_SCOPE_THREAD).begin(static_cast(t))); + Annotation::Guard g_t(Annotation("thread", CALI_ATTR_SCOPE_THREAD).begin(static_cast(t))); for (unsigned i = index++; i < files.size(); i = index++) { // "index++" is atomic read-mod-write const char* filename = (files[i].empty() ? "stdin" : files[i].c_str()); - Annotation::Guard - g_s(Annotation("cali-query.stream", CALI_ATTR_SCOPE_THREAD).begin(filename)); + Annotation::Guard g_s(Annotation("cali-query.stream", CALI_ATTR_SCOPE_THREAD).begin(filename)); if (verbose) { - std::lock_guard - g(msgmutex); + std::lock_guard g(msgmutex); std::cerr << "cali-query: Reading " << filename << std::endl; } @@ -365,11 +349,9 @@ int main(int argc, const char* argv[]) reader.read(files[i], metadb, node_proc, snap_proc); if (reader.error()) { - std::lock_guard - g(msgmutex); + std::lock_guard g(msgmutex); - std::cerr << "cali-query: Error reading " - << filename << ": " << reader.error_msg() << std::endl; + std::cerr << "cali-query: Error reading " << filename << ": " << reader.error_msg() << std::endl; } } }; @@ -383,7 +365,7 @@ int main(int argc, const char* argv[]) for (unsigned t = 0; t < num_threads; ++t) threads.emplace_back(thread_fn, t); - for (auto &t : threads) + for (auto& t : threads) t.join(); CALI_MARK_END("Processing"); diff --git a/src/tools/cali-query/query_common.cpp b/src/tools/cali-query/query_common.cpp index 62f782d06..d8cb1b633 100644 --- a/src/tools/cali-query/query_common.cpp +++ b/src/tools/cali-query/query_common.cpp @@ -33,11 +33,10 @@ namespace { /// \brief Parse "(arg1, arg2, ...)" argument list, ignoring whitespace -std::vector -parse_arglist(std::istream& is) +std::vector parse_arglist(std::istream& is) { std::vector ret; - std::string word; + std::string word; char c = util::read_char(is); @@ -51,7 +50,7 @@ parse_arglist(std::istream& is) do { std::string str = util::read_word(is, ",()"); - c = util::read_char(is); + c = util::read_char(is); if (!str.empty() && (c == ',' || c == ')')) ret.push_back(str); @@ -65,8 +64,7 @@ parse_arglist(std::istream& is) return ret; } -std::pair< int, std::vector > -parse_functioncall(std::istream& is, const QuerySpec::FunctionSignature* defs) +std::pair> parse_functioncall(std::istream& is, const QuerySpec::FunctionSignature* defs) { // read function name std::string fname = util::read_word(is, ",()"); @@ -77,7 +75,7 @@ parse_functioncall(std::istream& is, const QuerySpec::FunctionSignature* defs) // find function among given signatures int retid = 0; - for ( ; defs && defs[retid].name && (fname != defs[retid].name); ++retid) + for (; defs && defs[retid].name && (fname != defs[retid].name); ++retid) ; if (!defs || !defs[retid].name) { @@ -86,13 +84,12 @@ parse_functioncall(std::istream& is, const QuerySpec::FunctionSignature* defs) } // read argument list - std::vector args = parse_arglist(is); - int argsize = static_cast(args.size()); + std::vector args = parse_arglist(is); + int argsize = static_cast(args.size()); if (argsize < defs[retid].min_args || argsize > defs[retid].max_args) { - Log(0).stream() << "Error: Expected " << defs[retid].min_args - << " arguments for function \"" << defs[retid].name - << "\" but got " << argsize << std::endl; + Log(0).stream() << "Error: Expected " << defs[retid].min_args << " arguments for function \"" + << defs[retid].name << "\" but got " << argsize << std::endl; return std::make_pair(-1, std::vector()); } @@ -207,13 +204,12 @@ std::ostream& print_function_signature(std::ostream& os, const QuerySpec::Functi return os; } -} +} // namespace namespace cali { -bool -QueryArgsParser::parse_args(const Args& args) +bool QueryArgsParser::parse_args(const Args& args) { m_spec.filter.selection = QuerySpec::FilterSelection::Default; m_spec.select.selection = QuerySpec::AttributeSelection::Default; @@ -238,11 +234,11 @@ QueryArgsParser::parse_args(const Args& args) } else m_spec = p.spec(); } else if (args.is_set("query-file")) { - std::string filename = args.get("query-file"); + std::string filename = args.get("query-file"); std::ifstream in { filename.c_str() }; if (!in) { - m_error = true; + m_error = true; m_error_msg = "cannot open query file " + filename; return false; } @@ -250,7 +246,7 @@ QueryArgsParser::parse_args(const Args& args) CalQLParser p(in); if (p.error()) { - m_error = true; + m_error = true; m_error_msg = p.error_msg(); return false; } else @@ -283,7 +279,7 @@ QueryArgsParser::parse_args(const Args& args) m_spec.aggregate.selection = QuerySpec::AggregationSelection::List; std::istringstream is(opstr); - char c; + char c; const QuerySpec::FunctionSignature* defs = Aggregator::aggregation_defs(); @@ -312,14 +308,12 @@ QueryArgsParser::parse_args(const Args& args) m_spec.groupby.use_path = false; - auto it = std::find(m_spec.groupby.list.begin(), m_spec.groupby.list.end(), - "path"); + auto it = std::find(m_spec.groupby.list.begin(), m_spec.groupby.list.end(), "path"); if (it != m_spec.groupby.list.end()) { m_spec.groupby.use_path = true; m_spec.groupby.list.erase(it); } - it = std::find(m_spec.groupby.list.begin(), m_spec.groupby.list.end(), - "prop:nested"); + it = std::find(m_spec.groupby.list.begin(), m_spec.groupby.list.end(), "prop:nested"); if (it != m_spec.groupby.list.end()) { m_spec.groupby.use_path = true; m_spec.groupby.list.erase(it); @@ -341,7 +335,8 @@ QueryArgsParser::parse_args(const Args& args) // setup formatter - for (const QuerySpec::FunctionSignature* fmtsig = FormatProcessor::formatter_defs(); fmtsig && fmtsig->name; ++fmtsig) { + for (const QuerySpec::FunctionSignature* fmtsig = FormatProcessor::formatter_defs(); fmtsig && fmtsig->name; + ++fmtsig) { // see if a formatting option is set if (args.is_set(fmtsig->name)) { m_spec.format.opt = QuerySpec::FormatSpec::User; @@ -373,22 +368,22 @@ void print_caliquery_help(const Args& args, const char* usage, const ConfigManag if (helpopt == "configs" || helpopt == "recipes") { std::cout << "Available config recipes:\n"; - auto list = mgr.available_config_specs(); - size_t len = 0; - for (const auto &s : list) + auto list = mgr.available_config_specs(); + size_t len = 0; + for (const auto& s : list) len = std::max(len, s.size()); - for (const auto &s : list) { + for (const auto& s : list) { std::string descr = mgr.get_description_for_spec(s.c_str()); util::pad_right(std::cout << " ", s, len) << descr << "\n"; } } else if (helpopt == "services") { std::cout << "Available services:\n"; services::add_default_service_specs(); - auto list = services::get_available_services(); - size_t len = 0; - for (const auto &s : list) + auto list = services::get_available_services(); + size_t len = 0; + for (const auto& s : list) len = std::max(len, s.size()); - for (const auto &s : list) { + for (const auto& s : list) { std::string descr = services::get_service_description(s); util::pad_right(std::cout << " ", s, len) << descr << "\n"; } @@ -404,7 +399,7 @@ void print_caliquery_help(const Args& args, const char* usage, const ConfigManag const QuerySpec::FunctionSignature* ops = Aggregator::aggregation_defs(); for (const auto* p = ops; p && p->name; ++p) { print_function_signature(std::cout, *p) << " -> "; - std::vector args(p->args, p->args+p->max_args); + std::vector args(p->args, p->args + p->max_args); const QuerySpec::AggregationOp op(*p, args); std::cout << Aggregator::get_aggregation_attribute_name(op) << "\n"; } @@ -418,7 +413,7 @@ void print_caliquery_help(const Args& args, const char* usage, const ConfigManag } else if (!helpopt.empty()) { { auto cfgs = mgr.available_config_specs(); - auto it = std::find(cfgs.begin(), cfgs.end(), helpopt); + auto it = std::find(cfgs.begin(), cfgs.end(), helpopt); if (it != cfgs.end()) { std::cout << mgr.get_documentation_for_spec(helpopt.c_str()) << "\n"; return; @@ -428,7 +423,7 @@ void print_caliquery_help(const Args& args, const char* usage, const ConfigManag { services::add_default_service_specs(); auto srvs = services::get_available_services(); - auto it = std::find(srvs.begin(), srvs.end(), helpopt); + auto it = std::find(srvs.begin(), srvs.end(), helpopt); if (it != srvs.end()) { services::print_service_documentation(std::cout << *it << " service:\n", helpopt); return; @@ -436,22 +431,20 @@ void print_caliquery_help(const Args& args, const char* usage, const ConfigManag } std::cerr << "Unknown help option \"" << helpopt << "\". Available options: " - << "\n [none]: Describe cali-query usage (default)" - << "\n configs: Describe all Caliper profiling configurations" - << "\n [config or service name]: Describe profiling configuration or service" - << "\n services: List available services" - << std::endl; + << "\n [none]: Describe cali-query usage (default)" + << "\n configs: Describe all Caliper profiling configurations" + << "\n [config or service name]: Describe profiling configuration or service" + << "\n services: List available services" << std::endl; } else { std::cout << usage << "\n\n"; args.print_available_options(std::cout); - std::cout << - "\n Use \"--help configs\" to list all config recipes." - "\n Use \"--help services\" to list all available services." - "\n Use \"--help [recipe name]\" to get help for a config recipe." - "\n Use \"--help [service name]\" to get help for a service." - "\n Use \"--help calql\" to get help for the CalQL query language." - "\n Use \"--help [let,select,where,groupby,format]\" to get help for CalQL statements.\n"; + std::cout << "\n Use \"--help configs\" to list all config recipes." + "\n Use \"--help services\" to list all available services." + "\n Use \"--help [recipe name]\" to get help for a config recipe." + "\n Use \"--help [service name]\" to get help for a service." + "\n Use \"--help calql\" to get help for the CalQL query language." + "\n Use \"--help [let,select,where,groupby,format]\" to get help for CalQL statements.\n"; } } -} +} // namespace cali diff --git a/src/tools/cali-query/query_common.h b/src/tools/cali-query/query_common.h index abb3fd2c6..21dd7ba77 100644 --- a/src/tools/cali-query/query_common.h +++ b/src/tools/cali-query/query_common.h @@ -23,45 +23,42 @@ class CaliperMetadataAccessInterface; class ConfigManager; /// \brief Create QuerySpec from command-line arguments -class QueryArgsParser { +class QueryArgsParser +{ bool m_error; std::string m_error_msg; QuerySpec m_spec; public: - QueryArgsParser() - : m_error(true), - m_error_msg("query not read") - { } + QueryArgsParser() : m_error(true), m_error_msg("query not read") {} /// \brief Get query spec from cali-query command-line arguments. /// \return Returns \a true if successful, `false` in case of error. bool parse_args(const util::Args& args); - bool error() const { return m_error; } + bool error() const { return m_error; } + std::string error_msg() const { return m_error_msg; } - QuerySpec spec() const { return m_spec; } + QuerySpec spec() const { return m_spec; } }; /// \class SnapshotFilterStep /// \brief Basically the chain link in the processing chain. /// Passes result of \a m_filter_fn to \a m_push_fn. struct SnapshotFilterStep { - SnapshotFilterFn m_filter_fn; ///< This processing step - SnapshotProcessFn m_push_fn; ///< Next processing step + SnapshotFilterFn m_filter_fn; ///< This processing step + SnapshotProcessFn m_push_fn; ///< Next processing step SnapshotFilterStep(SnapshotFilterFn filter_fn, SnapshotProcessFn push_fn) : m_filter_fn { filter_fn }, m_push_fn { push_fn } - { } + {} - void operator ()(CaliperMetadataAccessInterface& db, const EntryList& list) { - m_filter_fn(db, list, m_push_fn); - } + void operator() (CaliperMetadataAccessInterface& db, const EntryList& list) { m_filter_fn(db, list, m_push_fn); } }; /// \brief Process --help for cali-query and mpi-caliquery void print_caliquery_help(const util::Args& args, const char* usage, const ConfigManager&); -} +} // namespace cali diff --git a/src/tools/cali-stat/cali-stat.cpp b/src/tools/cali-stat/cali-stat.cpp index 3f25f35ba..72d9496d9 100644 --- a/src/tools/cali-stat/cali-stat.cpp +++ b/src/tools/cali-stat/cali-stat.cpp @@ -28,234 +28,223 @@ using namespace util; namespace { - const char* usage = "cali-stat [OPTION]... [FILE]..." - "\n Collect and print statistics about data elements in Caliper streams"; - - const Args::Table option_table[] = { - // name, longopt name, shortopt char, has argument, info, argument info - { "reuse", "reuse-statistics", 'r', false, - "Print tree data reuse statistics", nullptr - }, - { "output", "output", 'o', true, "Set the output file name", "FILE" }, - { "help", "help", 'h', false, "Print help message", nullptr }, - Args::Terminator +const char* usage = + "cali-stat [OPTION]... [FILE]..." + "\n Collect and print statistics about data elements in Caliper streams"; + +const Args::Table option_table[] = { + // name, longopt name, shortopt char, has argument, info, argument info + { "reuse", "reuse-statistics", 'r', false, "Print tree data reuse statistics", nullptr }, + { "output", "output", 'o', true, "Set the output file name", "FILE" }, + { "help", "help", 'h', false, "Print help message", nullptr }, + Args::Terminator +}; + +class ReuseStat +{ + struct S { + struct ReuseInfo { + int nodes; // number of nodes with this attribute + map data; // different data elements + }; + + std::map reuse; }; - class ReuseStat { - struct S { - struct ReuseInfo { - int nodes; // number of nodes with this attribute - map data; // different data elements - }; - - std::map< cali_id_t, ReuseInfo > reuse; - }; + std::shared_ptr mS; - std::shared_ptr mS; +public: - public: + ReuseStat() : mS(new S) {} - ReuseStat() - : mS(new S) - { } + void print_results(CaliperMetadataAccessInterface& db, ostream& os) + { + os << "\nReuse statistics:\n" + << "Attribute #nodes #elem #uses #uses/elem #uses/node\n"; - void print_results(CaliperMetadataAccessInterface& db, ostream& os) { - os << "\nReuse statistics:\n" - << "Attribute #nodes #elem #uses #uses/elem #uses/node\n"; + for (auto& p : mS->reuse) { + int nelem = static_cast(p.second.data.size()); - for (auto &p : mS->reuse) { - int nelem = static_cast(p.second.data.size()); - - os << std::setw(32) << db.get_attribute(p.first).name() - << std::setw(12) << p.second.nodes - << std::setw(12) << nelem; + os << std::setw(32) << db.get_attribute(p.first).name() << std::setw(12) << p.second.nodes << std::setw(12) + << nelem; - double total_uses = 0.0; + double total_uses = 0.0; - for ( auto &dp : p.second.data ) - total_uses += dp.second; + for (auto& dp : p.second.data) + total_uses += dp.second; - os << std::setw(12) << total_uses - << std::setw(12) << (nelem > 0 ? total_uses / nelem : 0.0) - << std::setw(12) << (total_uses / p.second.nodes) - << endl; - } - + os << std::setw(12) << total_uses << std::setw(12) << (nelem > 0 ? total_uses / nelem : 0.0) + << std::setw(12) << (total_uses / p.second.nodes) << endl; } + } - void process_node(CaliperMetadataAccessInterface& db, const Node* node) { - auto it = mS->reuse.find(node->attribute()); + void process_node(CaliperMetadataAccessInterface& db, const Node* node) + { + auto it = mS->reuse.find(node->attribute()); - if (it == mS->reuse.end()) { - S::ReuseInfo info; + if (it == mS->reuse.end()) { + S::ReuseInfo info; - info.nodes = 1; - info.data[node->data().to_string()] = 1; + info.nodes = 1; + info.data[node->data().to_string()] = 1; - mS->reuse.insert(make_pair(node->attribute(), info)); - } else { - ++(it->second.nodes); - ++(it->second.data[node->data().to_string()]); - } - } - - void process_rec(const CaliperMetadataAccessInterface& db, const EntryList& rec) { - for (const Entry& e : rec) - if (e.is_reference()) - for (const Node* node = e.node(); node; node = node->parent()) { - auto it = mS->reuse.find(node->attribute()); - - if (it != mS->reuse.end()) - ++(it->second.data[node->data().to_string()]); - } + mS->reuse.insert(make_pair(node->attribute(), info)); + } else { + ++(it->second.nodes); + ++(it->second.data[node->data().to_string()]); } + } + + void process_rec(const CaliperMetadataAccessInterface& db, const EntryList& rec) + { + for (const Entry& e : rec) + if (e.is_reference()) + for (const Node* node = e.node(); node; node = node->parent()) { + auto it = mS->reuse.find(node->attribute()); + + if (it != mS->reuse.end()) + ++(it->second.data[node->data().to_string()]); + } + } +}; + +class CaliStreamStat +{ + struct S { + int n_snapshots; + int n_nodes; + + int n_max_snapshot; // max number of elements in snapshot record + int n_min_snapshot; // min number of elements in snapshot record + + int n_ref; // number of tree reference elements + int n_val; // number of immediate data elements + int n_tot; // number of total data elements in ctx records + + int n_attr_refs; // number of attributes in snapshot records + + int size_nodes; // (est.) size of all node records + int size_snapshots; // (est.) size of all snapshot records }; - - class CaliStreamStat { - struct S { - int n_snapshots; - int n_nodes; - - int n_max_snapshot; // max number of elements in snapshot record - int n_min_snapshot; // min number of elements in snapshot record - - int n_ref; // number of tree reference elements - int n_val; // number of immediate data elements - int n_tot; // number of total data elements in ctx records - - int n_attr_refs; // number of attributes in snapshot records - - int size_nodes; // (est.) size of all node records - int size_snapshots; // (est.) size of all snapshot records - }; - std::shared_ptr mS; + std::shared_ptr mS; - string format_size(double size) { - ostringstream os; + string format_size(double size) + { + ostringstream os; - const char* postfix[] = { "", "KiB", "MiB", "GiB" }; - int p = 0; - - for ( ; size > 1024 && p < 3; ++p) - size /= 1024; + const char* postfix[] = { "", "KiB", "MiB", "GiB" }; + int p = 0; - os << size << postfix[p]; + for (; size > 1024 && p < 3; ++p) + size /= 1024; - return os.str(); - } - - public: - - CaliStreamStat() - : mS(new S { 0, 0, 0, std::numeric_limits::max(), 0, 0, 0, 0, 0, 0 } ) - { } - - void print_results(ostream& os) { - os << "Number of records\n" - << "Total Nodes Snapshots\n" - << std::left - << std::setw(15) << mS->n_snapshots + mS->n_nodes - << std::setw(15) << mS->n_nodes - << std::setw(15) << mS->n_snapshots - << endl; - - os << "\nNumber of elements\n" - << "Total Nodes Tree refs Direct val\n" - << std::setw(15) << mS->n_tot + 4 * mS->n_nodes - << std::setw(15) << 4 * mS->n_nodes - << std::setw(15) << mS->n_ref - << std::setw(15) << 2 * mS->n_val - << endl; - - os << "\nData size (est.)\n" - << "Total Nodes Snapshots\n" - << std::setw(15) << format_size(mS->size_nodes + mS->size_snapshots) - << std::setw(15) << format_size(mS->size_nodes) - << std::setw(15) << format_size(mS->size_snapshots) - << endl; - - if (mS->n_snapshots < 1) - return; - - os << "\nElements/snapshot\n" - << "Min Max Average\n" - << std::setw(15) << mS->n_min_snapshot - << std::setw(15) << mS->n_max_snapshot - << std::setw(15) << static_cast(mS->n_tot) / mS->n_snapshots - << endl; - - os << "\nAttributes referenced in snapshot records\n" - << "Total Average Refs/Elem\n" - << std::setw(15) << mS->n_attr_refs - << std::setw(15) << static_cast(mS->n_attr_refs) / mS->n_snapshots - << std::setw(15) << static_cast(mS->n_attr_refs) / (mS->n_tot + 4 * mS->n_nodes) - << endl; - } + os << size << postfix[p]; - void process_node(CaliperMetadataAccessInterface& db, const Node* node) { - ++mS->n_nodes; + return os.str(); + } - // Get string size for usr and string nodes, otherwise assume 8 bytes - cali_attr_type type = db.get_attribute(node->attribute()).type(); - - mS->size_nodes += 3 * 8 + - (type == CALI_TYPE_USR || type == CALI_TYPE_STRING ? node->data().size() : 8); - } +public: - void process_rec(const CaliperMetadataAccessInterface& db, const EntryList& rec) { - ++mS->n_snapshots; - - int ref = 0; - int imm = 0; - - int ref_attr = 0; - - for (const Entry& e : rec) { - if (e.is_immediate()) { - ++imm; - } else if (e.is_reference()) { - ++ref; - for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) - ++ref_attr; - } - } - - mS->n_ref += ref; - mS->n_val += imm; - mS->n_tot += ref + 2*imm; + CaliStreamStat() : mS(new S { 0, 0, 0, std::numeric_limits::max(), 0, 0, 0, 0, 0, 0 }) {} - mS->n_min_snapshot = std::min(mS->n_min_snapshot, ref + 2*imm); - mS->n_max_snapshot = std::max(mS->n_max_snapshot, ref + 2*imm); + void print_results(ostream& os) + { + os << "Number of records\n" + << "Total Nodes Snapshots\n" + << std::left << std::setw(15) << mS->n_snapshots + mS->n_nodes << std::setw(15) << mS->n_nodes + << std::setw(15) << mS->n_snapshots << endl; + + os << "\nNumber of elements\n" + << "Total Nodes Tree refs Direct val\n" + << std::setw(15) << mS->n_tot + 4 * mS->n_nodes << std::setw(15) << 4 * mS->n_nodes << std::setw(15) + << mS->n_ref << std::setw(15) << 2 * mS->n_val << endl; + + os << "\nData size (est.)\n" + << "Total Nodes Snapshots\n" + << std::setw(15) << format_size(mS->size_nodes + mS->size_snapshots) << std::setw(15) + << format_size(mS->size_nodes) << std::setw(15) << format_size(mS->size_snapshots) << endl; + + if (mS->n_snapshots < 1) + return; + + os << "\nElements/snapshot\n" + << "Min Max Average\n" + << std::setw(15) << mS->n_min_snapshot << std::setw(15) << mS->n_max_snapshot << std::setw(15) + << static_cast(mS->n_tot) / mS->n_snapshots << endl; + + os << "\nAttributes referenced in snapshot records\n" + << "Total Average Refs/Elem\n" + << std::setw(15) << mS->n_attr_refs << std::setw(15) + << static_cast(mS->n_attr_refs) / mS->n_snapshots << std::setw(15) + << static_cast(mS->n_attr_refs) / (mS->n_tot + 4 * mS->n_nodes) << endl; + } + + void process_node(CaliperMetadataAccessInterface& db, const Node* node) + { + ++mS->n_nodes; - mS->n_attr_refs += ref_attr + imm; + // Get string size for usr and string nodes, otherwise assume 8 bytes + cali_attr_type type = db.get_attribute(node->attribute()).type(); - mS->size_snapshots += ref * 8; - } - }; + mS->size_nodes += 3 * 8 + (type == CALI_TYPE_USR || type == CALI_TYPE_STRING ? node->data().size() : 8); + } - struct Processor { - CaliStreamStat stream_stat; - ReuseStat reuse_stat; + void process_rec(const CaliperMetadataAccessInterface& db, const EntryList& rec) + { + ++mS->n_snapshots; - bool do_reuse_stat; - - void operator()(CaliperMetadataAccessInterface& db, const Node* node) { - stream_stat.process_node(db, node); + int ref = 0; + int imm = 0; - if (do_reuse_stat) - reuse_stat.process_node(db, node); - } - - void operator()(CaliperMetadataAccessInterface& db, const EntryList& rec) { - stream_stat.process_rec(db, rec); + int ref_attr = 0; - if (do_reuse_stat) - reuse_stat.process_rec(db, rec); + for (const Entry& e : rec) { + if (e.is_immediate()) { + ++imm; + } else if (e.is_reference()) { + ++ref; + for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent()) + ++ref_attr; + } } - }; -} + mS->n_ref += ref; + mS->n_val += imm; + mS->n_tot += ref + 2 * imm; + + mS->n_min_snapshot = std::min(mS->n_min_snapshot, ref + 2 * imm); + mS->n_max_snapshot = std::max(mS->n_max_snapshot, ref + 2 * imm); + + mS->n_attr_refs += ref_attr + imm; + + mS->size_snapshots += ref * 8; + } +}; + +struct Processor { + CaliStreamStat stream_stat; + ReuseStat reuse_stat; + + bool do_reuse_stat; + + void operator() (CaliperMetadataAccessInterface& db, const Node* node) + { + stream_stat.process_node(db, node); + + if (do_reuse_stat) + reuse_stat.process_node(db, node); + } + + void operator() (CaliperMetadataAccessInterface& db, const EntryList& rec) + { + stream_stat.process_rec(db, rec); + + if (do_reuse_stat) + reuse_stat.process_rec(db, rec); + } +}; +} // namespace // // --- main() @@ -270,20 +259,19 @@ int main(int argc, const char* argv[]) a_phase.set("init"); Args args(::option_table); - + // // --- Parse command line arguments // - + { int i = args.parse(argc, argv); if (i < argc) { - cerr << "cali-stat: error: unknown option: " << argv[i] << '\n' - << " Available options: "; + cerr << "cali-stat: error: unknown option: " << argv[i] << '\n' << " Available options: "; args.print_available_options(cerr); - + return -1; } @@ -308,11 +296,10 @@ int main(int argc, const char* argv[]) fs.open(filename.c_str()); if (!fs) { - cerr << "cali-stat: error: could not open output file " - << filename << endl; + cerr << "cali-stat: error: could not open output file " << filename << endl; return -2; - } + } } // @@ -320,7 +307,7 @@ int main(int argc, const char* argv[]) // Processor processor; - + processor.do_reuse_stat = args.is_set("reuse"); // @@ -332,9 +319,8 @@ int main(int argc, const char* argv[]) CaliperMetadataDB metadb; for (const string& file : args.arguments()) { - Annotation::Guard - g_s(Annotation("cali-stat.stream").set(file.c_str())); - + Annotation::Guard g_s(Annotation("cali-stat.stream").set(file.c_str())); + CaliReader reader; reader.read(file, metadb, processor, processor); diff --git a/src/tools/mpi-caliquery/mpi-caliquery.cpp b/src/tools/mpi-caliquery/mpi-caliquery.cpp index ad49c8aba..d2c04e273 100644 --- a/src/tools/mpi-caliquery/mpi-caliquery.cpp +++ b/src/tools/mpi-caliquery/mpi-caliquery.cpp @@ -30,79 +30,74 @@ using namespace util; namespace { -const char* usage = "mpi-caliquery [OPTION]... " +const char* usage = + "mpi-caliquery [OPTION]... " "\n Read, merge, and filter caliper streams in parallel." "\n Reads data from .cali on each MPI rank (i.e., 0.cali, 1.cali, ...)"; const Args::Table option_table[] = { // name, longopt name, shortopt char, has argument, info, argument info - { "select", "select", 's', true, + { "select", + "select", + 's', + true, "Filter records by selected attributes: [-]attribute[(<|>|=)value][:...]", - "QUERY_STRING" - }, - { "aggregate", "aggregate", 'a', true, + "QUERY_STRING" }, + { "aggregate", + "aggregate", + 'a', + true, "Aggregate snapshots using the given aggregation operators: (sum(attribute)|count)[:...]", - "AGGREGATION_OPS" - }, - { "aggregate-key", "aggregate-key", 0, true, + "AGGREGATION_OPS" }, + { "aggregate-key", + "aggregate-key", + 0, + true, "List of attributes to aggregate over (collapses all other attributes): attribute[:...]", - "ATTRIBUTES" - }, - { "attributes", "print-attributes", 0, true, + "ATTRIBUTES" }, + { "attributes", + "print-attributes", + 0, + true, "Select attributes to print (or hide) in expanded output: [-]attribute[:...]", - "ATTRIBUTES" - }, - { "sort", "sort-by", 'S', true, - "Sort rows in table format: attribute[:...]", - "SORT_ATTRIBUTES" - }, - { "format", "format", 'f', true, + "ATTRIBUTES" }, + { "sort", "sort-by", 'S', true, "Sort rows in table format: attribute[:...]", "SORT_ATTRIBUTES" }, + { "format", + "format", + 'f', + true, "Format output according to format string: %[]attr_name%...", - "FORMAT_STRING" - }, - { "title", "title", 0, true, - "Set the title row for formatted output", - "STRING" - }, - { "table", "table", 't', false, - "Print given attributes in human-readable table form", - "ATTRIBUTES" - }, - { "tree" , "tree", 'T', false, + "FORMAT_STRING" }, + { "title", "title", 0, true, "Set the title row for formatted output", "STRING" }, + { "table", "table", 't', false, "Print given attributes in human-readable table form", "ATTRIBUTES" }, + { "tree", + "tree", + 'T', + false, "Print records in a tree based on the hierarchy of the selected path attributes", - nullptr - }, - { "path-attributes", "path-attributes", 0, true, - "Select the path attributes for tree printers", - "ATTRIBUTES" - }, - { "json", "json", 'j', false, - "Print given attributes in web-friendly json format", - "ATTRIBUTES" - }, - { "query", "query", 'q', true, - "Execute a query in CalQL format", - "QUERY STRING" - }, - { "query-file", "query-file", 'Q', true, - "Read a CalQL query from a file", - "FILENAME" - }, - { "caliper-config", "caliper-config", 'P', true, + nullptr }, + { "path-attributes", "path-attributes", 0, true, "Select the path attributes for tree printers", "ATTRIBUTES" }, + { "json", "json", 'j', false, "Print given attributes in web-friendly json format", "ATTRIBUTES" }, + { "query", "query", 'q', true, "Execute a query in CalQL format", "QUERY STRING" }, + { "query-file", "query-file", 'Q', true, "Read a CalQL query from a file", "FILENAME" }, + { "caliper-config", + "caliper-config", + 'P', + true, "Set Caliper configuration for profiling mpi-caliquery", - "CALIPER-CONFIG" - }, - { "caliper-config-vars", "caliper-config-vars", 0, true, + "CALIPER-CONFIG" }, + { "caliper-config-vars", + "caliper-config-vars", + 0, + true, "Caliper configuration flags (for cali-query profiling)", - "KEY=VALUE,..." - }, - { "verbose", "verbose", 'v', false, "Be verbose.", nullptr }, - { "help", "help", 'h', true, "Print help message", nullptr }, - { "output", "output", 'o', true, "Set the output file name", "FILE" }, + "KEY=VALUE,..." }, + { "verbose", "verbose", 'v', false, "Be verbose.", nullptr }, + { "help", "help", 'h', true, "Print help message", nullptr }, + { "output", "output", 'o', true, "Set the output file name", "FILE" }, Args::Terminator }; - void format_output(const Args& args, const QuerySpec& spec, CaliperMetadataAccessInterface& db, Aggregator& aggregate) { CALI_CXX_MARK_FUNCTION; @@ -130,11 +125,13 @@ void process_my_input(int rank, const Args& args, const QuerySpec& spec, Caliper if (!args.arguments().front().empty()) filename = args.arguments().front() + "/" + filename; - NodeProcessFn node_proc = [](CaliperMetadataAccessInterface&,const Node*) { return; }; + NodeProcessFn node_proc = [](CaliperMetadataAccessInterface&, const Node*) { + return; + }; SnapshotProcessFn snap_proc = aggregate; if (!spec.preprocess_ops.empty()) - snap_proc = SnapshotFilterStep(Preprocessor(spec), snap_proc); + snap_proc = SnapshotFilterStep(Preprocessor(spec), snap_proc); if (spec.filter.selection == QuerySpec::FilterSelection::List) snap_proc = SnapshotFilterStep(RecordSelector(spec), snap_proc); @@ -142,8 +139,7 @@ void process_my_input(int rank, const Args& args, const QuerySpec& spec, Caliper reader.read(filename, db, node_proc, snap_proc); if (reader.error()) - std::cerr << "mpi-caliquery (" << rank << "): error " - << filename << ": " << reader.error_msg() << std::endl; + std::cerr << "mpi-caliquery (" << rank << "): error " << filename << ": " << reader.error_msg() << std::endl; } void setup_caliper_config(const Args& args) @@ -158,24 +154,22 @@ void setup_caliper_config(const Args& args) if (args.is_set("verbose")) cali_config_preset("CALI_LOG_VERBOSITY", "2"); - std::vector config_list = - StringConverter(args.get("caliper-config-vars")).to_stringlist(); + std::vector config_list = StringConverter(args.get("caliper-config-vars")).to_stringlist(); for (const std::string entry : config_list) { auto p = entry.find('='); if (p == std::string::npos) { - std::cerr << "cali-query: error: invalid Caliper configuration flag format \"" - << entry << "\" (missing \"=\")" << std::endl; + std::cerr << "cali-query: error: invalid Caliper configuration flag format \"" << entry + << "\" (missing \"=\")" << std::endl; continue; } - cali_config_set(entry.substr(0, p).c_str(), entry.substr(p+1).c_str()); + cali_config_set(entry.substr(0, p).c_str(), entry.substr(p + 1).c_str()); } } -} // namespace [anonymous] - +} // namespace int main(int argc, char* argv[]) { @@ -185,7 +179,7 @@ int main(int argc, char* argv[]) // Args args(::option_table); - int first_unknown_arg = args.parse(argc, argv); + int first_unknown_arg = args.parse(argc, argv); // must be done before Caliper initialization in MPI_Init wrapper ::setup_caliper_config(args); @@ -214,8 +208,7 @@ int main(int argc, char* argv[]) if (!query_parser.parse_args(args)) { if (rank == 0) - std::cerr << "mpi-caliquery: Invalid query: " << query_parser.error_msg() - << std::endl; + std::cerr << "mpi-caliquery: Invalid query: " << query_parser.error_msg() << std::endl; MPI_Abort(MPI_COMM_WORLD, -2); } @@ -238,9 +231,9 @@ int main(int argc, char* argv[]) CALI_MARK_FUNCTION_BEGIN; - QuerySpec spec = query_parser.spec(); + QuerySpec spec = query_parser.spec(); - Aggregator aggregate(spec); + Aggregator aggregate(spec); CaliperMetadataDB metadb; // --- Process our own input diff --git a/src/tools/util/Args.cpp b/src/tools/util/Args.cpp index 3594806c9..c918d6f23 100644 --- a/src/tools/util/Args.cpp +++ b/src/tools/util/Args.cpp @@ -12,27 +12,27 @@ using namespace std; using namespace util; -struct Args::ArgsImpl -{ - vector m_options; ///< Option list +struct Args::ArgsImpl { + vector m_options; ///< Option list - map::size_type> m_option_map; ///< Option key -> option index + map::size_type> m_option_map; ///< Option key -> option index - map::size_type> m_long_options; ///< Long option name -> option index - map::size_type> m_short_options; ///< Short option name -> option index + map::size_type> m_long_options; ///< Long option name -> option index + map::size_type> m_short_options; ///< Short option name -> option index map::size_type, string> m_active_options; ///< Option index -> option argument - vector m_arguments; ///< Non-option arguments (filenames etc.) - string m_programname; ///< argv[0] + vector m_arguments; ///< Non-option arguments (filenames etc.) + string m_programname; ///< argv[0] - bool m_fail; ///< Fail when parsing unknown option + bool m_fail; ///< Fail when parsing unknown option - string m_shortopt_prefix { "-" }; - string m_longopt_prefix { "--" }; - string m_options_end { "--" }; + string m_shortopt_prefix { "-" }; + string m_longopt_prefix { "--" }; + string m_options_end { "--" }; - void add_table(const Table table[]) { + void add_table(const Table table[]) + { for (const Table* t = table; t->name; ++t) { auto n = m_options.size(); @@ -47,7 +47,8 @@ struct Args::ArgsImpl } } - int parse(int argc, const char* const argv[], int pos) { + int parse(int argc, const char* const argv[], int pos) + { if (argc <= pos) return pos; @@ -70,16 +71,16 @@ struct Args::ArgsImpl if (!m_longopt_prefix.empty() && arg.find(m_longopt_prefix) == 0) { string::size_type dpos = arg.find('='); - auto it = m_long_options.find(arg.substr(lps, dpos == string::npos ? dpos : dpos-lps)); + auto it = m_long_options.find(arg.substr(lps, dpos == string::npos ? dpos : dpos - lps)); if (it == m_long_options.end()) return i; string optarg; - if (dpos != string::npos && dpos+1 < arg.size()) - optarg.assign(arg, dpos+1, string::npos); - if (m_options[it->second].has_argument && optarg.empty() && i+1 < argc) + if (dpos != string::npos && dpos + 1 < arg.size()) + optarg.assign(arg, dpos + 1, string::npos); + if (m_options[it->second].has_argument && optarg.empty() && i + 1 < argc) optarg.assign(argv[++i]); m_active_options.insert(make_pair(it->second, optarg)); @@ -94,8 +95,8 @@ struct Args::ArgsImpl if (!m_shortopt_prefix.empty() && arg.find(m_shortopt_prefix) == 0) { string::size_type dpos = arg.find('='); - // handle characters in the shortopt string; last one may have an option argument - for (auto n = sps ; n < arg.size() && n < dpos; ++n) { + // handle characters in the shortopt string; last one may have an option argument + for (auto n = sps; n < arg.size() && n < dpos; ++n) { auto it = m_short_options.find(arg[n]); if (it == m_short_options.end()) @@ -103,9 +104,9 @@ struct Args::ArgsImpl string optarg; - if (dpos+1 < arg.size() && n == dpos-1) - optarg.assign(arg, dpos+1, string::npos); - if (n == arg.size()-1 && m_options[it->second].has_argument && (i+1) < argc) + if (dpos + 1 < arg.size() && n == dpos - 1) + optarg.assign(arg, dpos + 1, string::npos); + if (n == arg.size() - 1 && m_options[it->second].has_argument && (i + 1) < argc) optarg.assign(string(argv[++i])); m_active_options.insert(make_pair(it->second, optarg)); @@ -126,9 +127,10 @@ struct Args::ArgsImpl return i; } - string get(const string& name, const string& def) const { + string get(const string& name, const string& def) const + { auto opt_it = m_option_map.find(name); - + if (opt_it == m_option_map.end()) return def; @@ -137,9 +139,10 @@ struct Args::ArgsImpl return it == m_active_options.end() ? def : it->second; } - bool is_set(const string& name) const { + bool is_set(const string& name) const + { auto opt_it = m_option_map.find(name); - + if (opt_it == m_option_map.end()) return false; @@ -148,34 +151,34 @@ struct Args::ArgsImpl return it != m_active_options.end(); } - vector options() const { + vector options() const + { vector out; - for (auto const &p : m_active_options) + for (auto const& p : m_active_options) out.emplace_back(m_options[p.first].name); return out; } - vector arguments() const { - return m_arguments; - } + vector arguments() const { return m_arguments; } - void print_available_options(ostream& os) const { + void print_available_options(ostream& os) const + { const string::size_type pad = 2; string::size_type max_longopt = m_longopt_prefix.size(); // Get max longopt+argument info size for padding for (auto const& l : m_long_options) { - string::size_type s = m_longopt_prefix.size() - + l.first.size() + string::size_type s = + m_longopt_prefix.size() + l.first.size() + (m_options[l.second].argument_info ? strlen(m_options[l.second].argument_info) + 1 : 0); max_longopt = max(max_longopt, s); } - const string opt_sep(", "); - const char* whitespace = " "; + const string opt_sep(", "); + const char* whitespace = " "; const string::size_type maxwhitespace = strlen(whitespace); for (const Table& opt : m_options) { @@ -183,8 +186,8 @@ struct Args::ArgsImpl if (opt.shortopt) os << m_shortopt_prefix << opt.shortopt << opt_sep; - else - os.write(whitespace, min(m_shortopt_prefix.size()+1+opt_sep.size(), maxwhitespace)); + else + os.write(whitespace, min(m_shortopt_prefix.size() + 1 + opt_sep.size(), maxwhitespace)); if (opt.longopt) { os << m_longopt_prefix << opt.longopt; @@ -196,9 +199,9 @@ struct Args::ArgsImpl s += strlen(opt.argument_info) + 1; } - os.write(whitespace, min(max_longopt+pad-s, maxwhitespace)); - } else - os.write(whitespace, min(max_longopt+pad+opt_sep.size(), maxwhitespace)); + os.write(whitespace, min(max_longopt + pad - s, maxwhitespace)); + } else + os.write(whitespace, min(max_longopt + pad + opt_sep.size(), maxwhitespace)); if (opt.info) os << opt.info; @@ -207,26 +210,20 @@ struct Args::ArgsImpl } } - ArgsImpl() - { } + ArgsImpl() {} - ArgsImpl(const Args::Table table[]) { - add_table(table); - } + ArgsImpl(const Args::Table table[]) { add_table(table); } }; - -// +// // --- Public interface // -Args::Args() - : mP { new ArgsImpl } -{ } +Args::Args() : mP { new ArgsImpl } +{} -Args::Args(const Args::Table table[]) - : mP { new ArgsImpl(table) } -{ } +Args::Args(const Args::Table table[]) : mP { new ArgsImpl(table) } +{} Args::~Args() { diff --git a/test/cali-annotation-perftest.cpp b/test/cali-annotation-perftest.cpp index d052505e6..caa4d8fb9 100644 --- a/test/cali-annotation-perftest.cpp +++ b/test/cali-annotation-perftest.cpp @@ -46,8 +46,7 @@ std::vector annotation_strings; extern const char* cali_perftest_build_metadata[][2]; -struct Config -{ +struct Config { int tree_width; int tree_depth; @@ -56,23 +55,21 @@ struct Config int channels; }; - int foo(int d, int w, const Config& cfg) { if (d <= 0) return 0; - cali::Annotation::Guard - g_a(test_annotation.begin(annotation_strings[d*cfg.tree_width+w].c_str())); + cali::Annotation::Guard g_a(test_annotation.begin(annotation_strings[d * cfg.tree_width + w].c_str())); - return 2 + foo(d-1, w, cfg); + return 2 + foo(d - 1, w, cfg); } int run(const Config& cfg) { int n_updates = 0; -#pragma omp parallel for schedule(static) reduction(+:n_updates) +#pragma omp parallel for schedule(static) reduction(+ : n_updates) for (int i = 0; i < cfg.iter; ++i) { n_updates += foo(cfg.tree_depth, i % cfg.tree_width, cfg); } @@ -97,7 +94,7 @@ void make_strings(const Config& cfg) str.append("."); str.append(std::to_string(w)); - annotation_strings[d*width+w] = std::move(str); + annotation_strings[d * width + w] = std::move(str); } } @@ -107,11 +104,10 @@ void record_globals(const Config& cfg, int threads, const cali::ConfigManager::a adiak::value("perftest.tree_width", cfg.tree_width); adiak::value("perftest.tree_depth", cfg.tree_depth); adiak::value("perftest.iterations", cfg.iter); - adiak::value("perftest.threads", threads); - adiak::value("perftest.channels", cfg.channels); + adiak::value("perftest.threads", threads); + adiak::value("perftest.channels", cfg.channels); - adiak::value("perftest.services", - cali::RuntimeConfig::get_default_config().get("services", "enable").to_string()); + adiak::value("perftest.services", cali::RuntimeConfig::get_default_config().get("services", "enable").to_string()); adiak::user(); adiak::launchdate(); @@ -121,7 +117,7 @@ void record_globals(const Config& cfg, int threads, const cali::ConfigManager::a adiak::clustername(); adiak::hostname(); - for (auto &p : extra_kv) + for (auto& p : extra_kv) adiak::value(p.first, p.second); for (size_t p = 0; cali_perftest_build_metadata[p][0]; ++p) @@ -131,36 +127,31 @@ void record_globals(const Config& cfg, int threads, const cali::ConfigManager::a cali_set_global_int_byname("perftest.tree_width", cfg.tree_width); cali_set_global_int_byname("perftest.tree_depth", cfg.tree_depth); cali_set_global_int_byname("perftest.iterations", cfg.iter); - cali_set_global_int_byname("perftest.threads", threads); - cali_set_global_int_byname("perftest.channels", cfg.channels); + cali_set_global_int_byname("perftest.threads", threads); + cali_set_global_int_byname("perftest.channels", cfg.channels); #endif } const util::Args::Table option_table[] = { - { "width", "tree-width", 'w', true, - "Context tree width", "WIDTH" - }, - { "depth", "tree-width", 'd', true, - "Context tree depth", "DEPTH" - }, - { "iterations", "iterations", 'i', true, - "Iterations", "ITERATIONS" - }, - { "csv", "print-csv", 'c', false, - "CSV output. Fields: Tree depth, tree width, number of updates, threads, total runtime.", - nullptr - }, - { "channels", "channels", 'x', true, - "Number of replicated channel instances", - "CHANNELS" - }, - { "profile", "profile", 'P', true, - "Caliper profiling config (for profiling cali-annotation-perftest)", - "CONFIGSTRING" - }, + { "width", "tree-width", 'w', true, "Context tree width", "WIDTH" }, + { "depth", "tree-width", 'd', true, "Context tree depth", "DEPTH" }, + { "iterations", "iterations", 'i', true, "Iterations", "ITERATIONS" }, + { "csv", + "print-csv", + 'c', + false, + "CSV output. Fields: Tree depth, tree width, number of updates, threads, total runtime.", + nullptr }, + { "channels", "channels", 'x', true, "Number of replicated channel instances", "CHANNELS" }, + { "profile", + "profile", + 'P', + true, + "Caliper profiling config (for profiling cali-annotation-perftest)", + "CONFIGSTRING" }, { "quiet", "quiet", 'q', false, "Don't print output", nullptr }, - { "help", "help", 'h', false, "Print help", nullptr }, + { "help", "help", 'h', false, "Print help", nullptr }, util::Args::Terminator }; @@ -176,8 +167,7 @@ int main(int argc, char* argv[]) int lastarg = args.parse(argc, argv); if (lastarg < argc) { - std::cerr << "cali-annotation-perftest: unknown option: " << argv[lastarg] << '\n' - << "Available options: "; + std::cerr << "cali-annotation-perftest: unknown option: " << argv[lastarg] << '\n' << "Available options: "; args.print_available_options(std::cerr); @@ -209,10 +199,10 @@ int main(int argc, char* argv[]) Config cfg; - cfg.tree_width = std::stoi(args.get("width", "20")); - cfg.tree_depth = std::stoi(args.get("depth", "10")); - cfg.iter = std::stoi(args.get("iterations", "100000")); - cfg.channels = std::max(std::stoi(args.get("channels", "1")), 1); + cfg.tree_width = std::stoi(args.get("width", "20")); + cfg.tree_depth = std::stoi(args.get("depth", "10")); + cfg.iter = std::stoi(args.get("iterations", "100000")); + cfg.channels = std::max(std::stoi(args.get("channels", "1")), 1); // set global attributes before other Caliper initialization record_globals(cfg, threads, extra_kv); @@ -226,10 +216,8 @@ int main(int argc, char* argv[]) if (!quiet && !print_csv) std::cout << "cali-annotation-perftest:" - << "\n Channels: " << cfg.channels - << "\n Tree width: " << cfg.tree_width - << "\n Tree depth: " << cfg.tree_depth - << "\n Iterations: " << cfg.iter + << "\n Channels: " << cfg.channels << "\n Tree width: " << cfg.tree_width + << "\n Tree depth: " << cfg.tree_depth << "\n Iterations: " << cfg.iter #ifdef _OPENMP << "\n Threads: " << omp_get_max_threads() #endif @@ -280,10 +268,10 @@ int main(int argc, char* argv[]) CALI_MARK_END("perftest.timing"); - auto msec = std::chrono::duration_cast(etime-stime).count(); + auto msec = std::chrono::duration_cast(etime - stime).count(); - double usec_per_update = (updates > 0 ? (1000.0*msec*threads)/updates : 0.0); - double updates_per_sec = (msec > 0 ? 1000.0*updates/msec : 0.0); + double usec_per_update = (updates > 0 ? (1000.0 * msec * threads) / updates : 0.0); + double updates_per_sec = (msec > 0 ? 1000.0 * updates / msec : 0.0); #ifdef CALIPER_HAVE_ADIAK adiak::value("perftest.usec_per_update", usec_per_update); @@ -293,20 +281,12 @@ int main(int argc, char* argv[]) if (!quiet) { if (print_csv) - std::cout << cfg.channels - << "," << cfg.tree_depth - << "," << cfg.tree_width - << "," << updates - << "," << threads - << "," << msec/1000.0 - << std::endl; + std::cout << cfg.channels << "," << cfg.tree_depth << "," << cfg.tree_width << "," << updates << "," + << threads << "," << msec / 1000.0 << std::endl; else - std::cout << " " << updates << " annotation updates in " - << msec/1000.0 << " sec (" - << updates/threads << " per thread), " - << updates_per_sec << " updates/sec, " - << usec_per_update << " usec/update" - << std::endl; + std::cout << " " << updates << " annotation updates in " << msec / 1000.0 << " sec (" << updates / threads + << " per thread), " << updates_per_sec << " updates/sec, " << usec_per_update << " usec/update" + << std::endl; } CALI_MARK_FUNCTION_END; diff --git a/test/cali-flush-perftest.cpp b/test/cali-flush-perftest.cpp index 1bd5a3e4e..ecf030dd0 100644 --- a/test/cali-flush-perftest.cpp +++ b/test/cali-flush-perftest.cpp @@ -26,8 +26,7 @@ #include #endif -struct Config -{ +struct Config { int iter; int nxtra; bool write; @@ -50,9 +49,9 @@ void run(const Config& cfg) cali::Caliper c; for (int x = 0, div = 10; x < cfg.nxtra; ++x, div *= 10) - c.begin(cfg.xtra_attrs[x], cali::Variant(i/div)); + c.begin(cfg.xtra_attrs[x], cali::Variant(i / div)); for (int x = 0; x < cfg.nxtra; ++x) - c.end(cfg.xtra_attrs[cfg.nxtra-(1+x)]); + c.end(cfg.xtra_attrs[cfg.nxtra - (1 + x)]); } CALI_CXX_MARK_LOOP_END(testloop); @@ -60,16 +59,12 @@ void run(const Config& cfg) } const util::Args::Table option_table[] = { - { "iterations", "iterations", 'i', true, - "Number of loop iterations", "ITERATIONS" }, - { "xtra", "xtra", 'x', true, - "Number of extra attributes", "XTRA" }, + { "iterations", "iterations", 'i', true, "Number of loop iterations", "ITERATIONS" }, + { "xtra", "xtra", 'x', true, "Number of extra attributes", "XTRA" }, - { "channels", "channels", 'c', true, - "Number of replicated channels", "CHANNELS" }, + { "channels", "channels", 'c', true, "Number of replicated channels", "CHANNELS" }, - { "write", "write", 'w', false, - "Write to output service in addition to flush", nullptr }, + { "write", "write", 'w', false, "Write to output service in addition to flush", nullptr }, { "help", "help", 'h', false, "Print help", nullptr }, @@ -88,8 +83,7 @@ int main(int argc, char* argv[]) int lastarg = args.parse(argc, argv); if (lastarg < argc) { - std::cerr << "cali-flush-perftest: unknown option: " << argv[lastarg] << '\n' - << "Available options: "; + std::cerr << "cali-flush-perftest: unknown option: " << argv[lastarg] << '\n' << "Available options: "; args.print_available_options(std::cerr); @@ -109,8 +103,8 @@ int main(int argc, char* argv[]) Config cfg; cfg.iter = std::stoi(args.get("iterations", "100000")); - cfg.nxtra = std::stoi(args.get("xtra", "2")); - cfg.channels = std::stoi(args.get("channels", "1")); + cfg.nxtra = std::stoi(args.get("xtra", "2")); + cfg.channels = std::stoi(args.get("channels", "1")); cfg.write = args.is_set("write"); cali_set_global_int_byname("flush-perftest.iterations", cfg.iter); @@ -121,8 +115,7 @@ int main(int argc, char* argv[]) // --- print info std::cout << "cali-flush-perftest:" - << "\n Channels: " << cfg.channels - << "\n Iterations: " << cfg.iter + << "\n Channels: " << cfg.channels << "\n Iterations: " << cfg.iter << "\n Xtra: " << cfg.nxtra #ifdef _OPENMP << "\n Threads: " << threads @@ -134,13 +127,15 @@ int main(int argc, char* argv[]) cali::Caliper c; for (int i = 0, div = 10; i < cfg.nxtra; ++i, div *= 10) - cfg.xtra_attrs.push_back(c.create_attribute(std::string("x.")+std::to_string(div), - CALI_TYPE_INT, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE)); + cfg.xtra_attrs.push_back(c.create_attribute( + std::string("x.") + std::to_string(div), + CALI_TYPE_INT, + CALI_ATTR_SCOPE_THREAD | CALI_ATTR_ASVALUE + )); // --- set up channels std::vector channels; - auto default_channel = c.get_channel(0); + auto default_channel = c.get_channel(0); if (default_channel) channels.push_back(default_channel); @@ -161,26 +156,28 @@ int main(int argc, char* argv[]) // --- Timed flush - int snapshots = 3 + cfg.channels * (2*threads + (cfg.iter * (2 + 2*cfg.nxtra))); + int snapshots = 3 + cfg.channels * (2 * threads + (cfg.iter * (2 + 2 * cfg.nxtra))); CALI_MARK_BEGIN("flush"); auto stime = std::chrono::system_clock::now(); - for (auto &chn : channels) + for (auto& chn : channels) if (cfg.write) c.flush_and_write(&chn, cali::SnapshotView()); else - c.flush(&chn, cali::SnapshotView(), [](cali::CaliperMetadataAccessInterface&, const std::vector&) { }); + c.flush( + &chn, + cali::SnapshotView(), + [](cali::CaliperMetadataAccessInterface&, const std::vector&) {} + ); auto etime = std::chrono::system_clock::now(); CALI_MARK_END("flush"); - auto msec = std::chrono::duration_cast(etime-stime).count(); + auto msec = std::chrono::duration_cast(etime - stime).count(); - std::cout << " " << snapshots << " snapshots flushed in " - << msec/1000.0 << " sec, " - << 1000.0*msec/snapshots << " usec/snapshot" - << std::endl; + std::cout << " " << snapshots << " snapshots flushed in " << msec / 1000.0 << " sec, " << 1000.0 * msec / snapshots + << " usec/snapshot" << std::endl; } diff --git a/test/cali-functional-debugging.cpp b/test/cali-functional-debugging.cpp index 9b491ec7a..72d77ca6e 100644 --- a/test/cali-functional-debugging.cpp +++ b/test/cali-functional-debugging.cpp @@ -17,45 +17,50 @@ #include #include - #include -struct dog{ - int num_dogs; +struct dog { + int num_dogs; }; -struct special_dog : public dog { -}; +struct special_dog : public dog {}; cali::Annotation int_annotator("int_value"); -namespace cali{ + +namespace cali +{ cali::Annotation dog_annotator("dog"); -template<> -struct Recordable{ - static void record(dog in){ - dog_annotator.begin(in.num_dogs); - dog_annotator.end(); - } + +template <> +struct Recordable { + static void record(dog in) + { + dog_annotator.begin(in.num_dogs); + dog_annotator.end(); + } }; -template<> -struct Recordable{ - static void record(int instance){ - int_annotator.begin(instance); - int_annotator.end(); - } +template <> +struct Recordable { + static void record(int instance) + { + int_annotator.begin(instance); + int_annotator.end(); + } }; } //end namespace cali -void original_adder(int* in, dog dog_instance){} -auto adder = cali::make_recorded_function("adder",original_adder); +void original_adder(int* in, dog dog_instance) +{} + +auto adder = cali::make_recorded_function("adder", original_adder); int main(int argc, char* argv[]) { int doggo = 6; dog goodest; goodest.num_dogs = 9; - adder(&doggo,goodest); + adder(&doggo, goodest); return 0; } diff --git a/test/cali-test.cpp b/test/cali-test.cpp index 9308e7d83..b06fdc139 100644 --- a/test/cali-test.cpp +++ b/test/cali-test.cpp @@ -17,7 +17,6 @@ #include #include - void begin_foo_op() { // Begin "foo"->"fooing" and keep it alive past the end of the current C++ scope @@ -40,8 +39,9 @@ void test_blob() float f = 42.42; } my_weird_elem; - cali::Annotation::Guard - g_mydata( cali::Annotation("mydata").begin(CALI_TYPE_USR, &my_weird_elem, sizeof(my_weird_elem)) ); + cali::Annotation::Guard g_mydata( + cali::Annotation("mydata").begin(CALI_TYPE_USR, &my_weird_elem, sizeof(my_weird_elem)) + ); } void test_annotation_copy() @@ -67,20 +67,14 @@ void test_annotation_copy() void test_attribute_metadata() { - cali::Caliper c; + cali::Caliper c; - cali::Attribute meta_attr[2] = { - c.create_attribute("meta-string", CALI_TYPE_STRING), - c.create_attribute("meta-int", CALI_TYPE_INT) - }; - cali::Variant meta_data[2] = { - cali::Variant(CALI_TYPE_STRING, "metatest", 8), - cali::Variant(42) - }; + cali::Attribute meta_attr[2] = { c.create_attribute("meta-string", CALI_TYPE_STRING), + c.create_attribute("meta-int", CALI_TYPE_INT) }; + cali::Variant meta_data[2] = { cali::Variant(CALI_TYPE_STRING, "metatest", 8), cali::Variant(42) }; cali::Attribute attr = - c.create_attribute("metadata-test-attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT, - 2, meta_attr, meta_data); + c.create_attribute("metadata-test-attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT, 2, meta_attr, meta_data); c.set(attr, cali::Variant(1337)); @@ -128,8 +122,7 @@ void test_cross_scope() void test_attr_prop_preset() { - cali::Annotation::Guard - g( cali::Annotation("test-prop-preset").set(true) ); + cali::Annotation::Guard g(cali::Annotation("test-prop-preset").set(true)); } void test_aggr_warnings() @@ -137,11 +130,11 @@ void test_aggr_warnings() cali::Caliper c; // create an immediate attribute with double type: should create warning if used in aggregation key - cali::Attribute d = c.create_attribute("aw.dbl", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); + cali::Attribute d = c.create_attribute("aw.dbl", CALI_TYPE_DOUBLE, CALI_ATTR_ASVALUE); - cali::Attribute i1 = c.create_attribute("aw.int.1", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - cali::Attribute i2 = c.create_attribute("aw.int.2", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - cali::Attribute i3 = c.create_attribute("aw.int.3", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + cali::Attribute i1 = c.create_attribute("aw.int.1", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + cali::Attribute i2 = c.create_attribute("aw.int.2", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + cali::Attribute i3 = c.create_attribute("aw.int.3", CALI_TYPE_INT, CALI_ATTR_ASVALUE); cali::Attribute i4 = c.create_attribute("aw.int.4", CALI_TYPE_UINT, CALI_ATTR_ASVALUE); cali::Attribute i5 = c.create_attribute("aw.int.5", CALI_TYPE_UINT, CALI_ATTR_ASVALUE); @@ -150,28 +143,20 @@ void test_aggr_warnings() // make a snapshot with "-1, -2, -3" entries. this should cause the aggregation key // getting too long, as negative values aren't be compressed well currently - cali_id_t attr[6] = { - d.id(), i1.id(), i2.id(), - i3.id(), i4.id(), i5.id() - }; - cali_variant_t data[6] = { - cali_make_variant_from_double(1.0), - cali_make_variant_from_int(-1), - cali_make_variant_from_int(-2), - cali_make_variant_from_int(-3), - cali_make_variant_from_uint(largeval), - cali_make_variant_from_uint(largeval) - }; - - cali_id_t chn_id = - cali::create_channel("test_aggregate_warnings", 0, { - { "CALI_SERVICES_ENABLE", "aggregate" }, - { "CALI_AGGREGATE_KEY", "function,aw.dbl,aw.int.1,aw.int.2,aw.int.3,aw.int.4,aw.int.5" }, - { "CALI_CHANNEL_CONFIG_CHECK", "false" } - }); - - cali_channel_push_snapshot(chn_id, CALI_SCOPE_THREAD | CALI_SCOPE_PROCESS, - 6, attr, data); + cali_id_t attr[6] = { d.id(), i1.id(), i2.id(), i3.id(), i4.id(), i5.id() }; + cali_variant_t data[6] = { cali_make_variant_from_double(1.0), cali_make_variant_from_int(-1), + cali_make_variant_from_int(-2), cali_make_variant_from_int(-3), + cali_make_variant_from_uint(largeval), cali_make_variant_from_uint(largeval) }; + + cali_id_t chn_id = cali::create_channel( + "test_aggregate_warnings", + 0, + { { "CALI_SERVICES_ENABLE", "aggregate" }, + { "CALI_AGGREGATE_KEY", "function,aw.dbl,aw.int.1,aw.int.2,aw.int.3,aw.int.4,aw.int.5" }, + { "CALI_CHANNEL_CONFIG_CHECK", "false" } } + ); + + cali_channel_push_snapshot(chn_id, CALI_SCOPE_THREAD | CALI_SCOPE_PROCESS, 6, attr, data); cali_delete_channel(chn_id); } @@ -188,7 +173,7 @@ std::ostream& print_padded(std::ostream& os, const char* string, int fieldlen) os << string; if (slen < fieldlen) - os << whitespace + (120 - std::min(120, fieldlen-slen)); + os << whitespace + (120 - std::min(120, fieldlen - slen)); return os; } @@ -196,26 +181,22 @@ std::ostream& print_padded(std::ostream& os, const char* string, int fieldlen) void test_instance() { if (cali::Caliper::is_initialized() == true) { - std::cout << "cali-test: Caliper::is_initialized() failed uninitialized condition" - << std::endl; + std::cout << "cali-test: Caliper::is_initialized() failed uninitialized condition" << std::endl; return; } if (cali_is_initialized() != 0) { - std::cout << "cali-test: cali_is_initialized() failed uninitialized condition " - << std::endl; + std::cout << "cali-test: cali_is_initialized() failed uninitialized condition " << std::endl; return; } cali_init(); if (cali::Caliper::is_initialized() == false) { - std::cout << "cali-test: Caliper::is_initialized() failed initialized condition" - << std::endl; + std::cout << "cali-test: Caliper::is_initialized() failed initialized condition" << std::endl; return; } if (cali_is_initialized() == 0) { - std::cout << "cali-test: cali_is_initialized() failed initialized condition " - << std::endl; + std::cout << "cali-test: cali_is_initialized() failed initialized condition " << std::endl; return; } @@ -261,34 +242,31 @@ int main(int argc, char* argv[]) test_instance(); const struct testcase_info_t { - const char* name; - void (*fn)(); - } testcases[] = { - { "blob", test_blob }, - { "annotation-copy", test_annotation_copy }, - { "attribute-metadata", test_attribute_metadata }, - { "uninitialized-annotation", test_uninitialized }, - { "end-mismatch", test_end_mismatch }, - { "escaping", test_escaping }, - { "aggr-warnings", test_aggr_warnings }, - { "cross-scope", test_cross_scope }, - { "attribute-prop-preset", test_attr_prop_preset }, - { "config-after-init", test_config_after_init }, - { "nesting-error", test_nesting_error }, - { "unclosed-region", test_unclosed_region }, - { "empty-stack", test_empty_stack }, - { 0, 0 } - }; + const char* name; + void (*fn)(); + } testcases[] = { { "blob", test_blob }, + { "annotation-copy", test_annotation_copy }, + { "attribute-metadata", test_attribute_metadata }, + { "uninitialized-annotation", test_uninitialized }, + { "end-mismatch", test_end_mismatch }, + { "escaping", test_escaping }, + { "aggr-warnings", test_aggr_warnings }, + { "cross-scope", test_cross_scope }, + { "attribute-prop-preset", test_attr_prop_preset }, + { "config-after-init", test_config_after_init }, + { "nesting-error", test_nesting_error }, + { "unclosed-region", test_unclosed_region }, + { "empty-stack", test_empty_stack }, + { 0, 0 } }; { - cali::Annotation::Guard - g( cali::Annotation("cali-test", CALI_ATTR_NOMERGE).begin("checking") ); + cali::Annotation::Guard g(cali::Annotation("cali-test", CALI_ATTR_NOMERGE).begin("checking")); // check for missing/misspelled command line test cases for (int a = 1; a < argc; ++a) { const testcase_info_t* t = testcases; - for ( ; t->name && 0 != strcmp(t->name, argv[a]); ++t) + for (; t->name && 0 != strcmp(t->name, argv[a]); ++t) ; if (!t->name) @@ -296,22 +274,20 @@ int main(int argc, char* argv[]) } } - cali::Annotation::Guard - g( cali::Annotation("cali-test", CALI_ATTR_NOMERGE).begin("testing") ); + cali::Annotation::Guard g(cali::Annotation("cali-test", CALI_ATTR_NOMERGE).begin("testing")); for (const testcase_info_t* t = testcases; t->fn; ++t) { if (argc > 1) { int a = 1; - for ( ; a < argc && 0 != strcmp(t->name, argv[a]); ++a) + for (; a < argc && 0 != strcmp(t->name, argv[a]); ++a) ; if (a == argc) continue; } - cali::Annotation::Guard - g( cali::Annotation("cali-test.test", CALI_ATTR_NOMERGE).begin(t->name) ); + cali::Annotation::Guard g(cali::Annotation("cali-test.test", CALI_ATTR_NOMERGE).begin(t->name)); print_padded(std::cout, t->name, 28) << " ... "; (*(t->fn))(); diff --git a/test/cali-wrap.cpp b/test/cali-wrap.cpp index 8f2abb88d..37401c6a0 100644 --- a/test/cali-wrap.cpp +++ b/test/cali-wrap.cpp @@ -1,25 +1,27 @@ // Copyright (c) 2019, Lawrence Livermore National Security, LLC. // See top-level LICENSE file for details. -// A minimal Caliper instrumentation demo +// A minimal Caliper instrumentation demo #include #include #include -int doWork(int* inArray, int size){ +int doWork(int* inArray, int size) +{ int reduce_var = 0; - for(int i=0; i -T* initialize(size_t data_size, T initial_value){ - T* array = (T*)malloc(sizeof(T)*data_size); - for(size_t i=0;i +T* initialize(size_t data_size, T initial_value) +{ + T* array = (T*) malloc(sizeof(T) * data_size); + for (size_t i = 0; i < data_size; i++) { array[i] = initial_value; } return array; @@ -27,20 +29,18 @@ T* initialize(size_t data_size, T initial_value){ auto doWorkWrapped = cali::wrap_function_and_args("doWork", doWork); - - int main(int argc, char* argv[]) { - constexpr int data_size = 1000000; - constexpr int iterations = 10; - int data_size_increment = data_size/iterations; - cali::wrap("Program",[&](){ - int * inArray; - cali::wrap("Initialization",[&](){ - inArray = cali::wrap_with_args("initializer",initialize,data_size, 0); + constexpr int data_size = 1000000; + constexpr int iterations = 10; + int data_size_increment = data_size / iterations; + cali::wrap("Program", [&]() { + int* inArray; + cali::wrap("Initialization", [&]() { + inArray = cali::wrap_with_args("initializer", initialize, data_size, 0); }); - for(int size = data_size_increment; size<=data_size; size+=data_size_increment){ - doWorkWrapped(inArray,size); + for (int size = data_size_increment; size <= data_size; size += data_size_increment) { + doWorkWrapped(inArray, size); } }); } diff --git a/test/ci_app_tests/ci_test_aggregate.cpp b/test/ci_app_tests/ci_test_aggregate.cpp index 5995415c9..2090b826d 100644 --- a/test/ci_app_tests/ci_test_aggregate.cpp +++ b/test/ci_app_tests/ci_test_aggregate.cpp @@ -1,32 +1,29 @@ #include "caliper/cali.h" -void foo(int c) { +void foo(int c) +{ CALI_CXX_MARK_FUNCTION; // ... } int main() { - { // "A" loop - cali::Annotation::Guard - g( cali::Annotation("loop.id", CALI_ATTR_NESTED).begin("A") ); + { // "A" loop + cali::Annotation::Guard g(cali::Annotation("loop.id", CALI_ATTR_NESTED).begin("A")); for (int i = 0; i < 3; ++i) { - cali::Annotation::Guard - g( cali::Annotation("iteration", CALI_ATTR_ASVALUE).begin(i) ); + cali::Annotation::Guard g(cali::Annotation("iteration", CALI_ATTR_ASVALUE).begin(i)); foo(1); foo(2); } } - { // "B" loop - cali::Annotation::Guard - g( cali::Annotation("loop.id", CALI_ATTR_NESTED).begin("B") ); + { // "B" loop + cali::Annotation::Guard g(cali::Annotation("loop.id", CALI_ATTR_NESTED).begin("B")); for (int i = 0; i < 4; ++i) { - cali::Annotation::Guard - g( cali::Annotation("iteration", CALI_ATTR_ASVALUE).begin(i) ); + cali::Annotation::Guard g(cali::Annotation("iteration", CALI_ATTR_ASVALUE).begin(i)); foo(1); } diff --git a/test/ci_app_tests/ci_test_alloc.c b/test/ci_app_tests/ci_test_alloc.c index eafd59290..87d93879f 100644 --- a/test/ci_app_tests/ci_test_alloc.c +++ b/test/ci_app_tests/ci_test_alloc.c @@ -12,64 +12,72 @@ extern cali_id_t cali_class_memoryaddress_attr_id; void ci_test_alloc() { - CALI_MARK_FUNCTION_BEGIN; - - cali_variant_t v_true = cali_make_variant_from_bool(true); - - cali_id_t ptr_in_attr = - cali_create_attribute_with_metadata("ptr_in", CALI_TYPE_ADDR, CALI_ATTR_ASVALUE, - 1, &cali_class_memoryaddress_attr_id, &v_true); - cali_id_t ptr_out_attr = - cali_create_attribute_with_metadata("ptr_out", CALI_TYPE_ADDR, CALI_ATTR_ASVALUE, - 1, &cali_class_memoryaddress_attr_id, &v_true); - - int *A = (int*) malloc(42 * sizeof(int)); - cali_datatracker_track_dimensional(A, "test_alloc_A", sizeof(int), (const size_t[]) { 42 }, 1); - - cali_id_t attrs[2] = { ptr_in_attr, ptr_out_attr }; - int scope = CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD; - - cali_begin_byname("test_alloc.allocated.0"); - int* A_inside = A; - int* A_outside = A-1; - - cali_variant_t v_p_i = - cali_make_variant(CALI_TYPE_ADDR, &A_inside, sizeof(int*)); - cali_variant_t v_p_o = - cali_make_variant(CALI_TYPE_ADDR, &A_outside, sizeof(int*)); - - cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); - cali_end_byname("test_alloc.allocated.0"); - - cali_begin_byname("test_alloc.allocated.1"); - A_inside = A+41; - /* int* A_outside = A+42; this should fail but doesn't: needs fix */ - A_outside = A+43; - - v_p_i = cali_make_variant(CALI_TYPE_ADDR, &A_inside, sizeof(int*)); - v_p_o = cali_make_variant(CALI_TYPE_ADDR, &A_outside, sizeof(int*)); - - cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); - cali_end_byname("test_alloc.allocated.1"); - - cali_datatracker_untrack(A); - free(A); - A = NULL; - - cali_begin_byname("test_alloc.freed"); - cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); - cali_end_byname("test_alloc.freed"); - - CALI_MARK_FUNCTION_END; + CALI_MARK_FUNCTION_BEGIN; + + cali_variant_t v_true = cali_make_variant_from_bool(true); + + cali_id_t ptr_in_attr = cali_create_attribute_with_metadata( + "ptr_in", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE, + 1, + &cali_class_memoryaddress_attr_id, + &v_true + ); + cali_id_t ptr_out_attr = cali_create_attribute_with_metadata( + "ptr_out", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE, + 1, + &cali_class_memoryaddress_attr_id, + &v_true + ); + + int* A = (int*) malloc(42 * sizeof(int)); + cali_datatracker_track_dimensional(A, "test_alloc_A", sizeof(int), (const size_t[]) { 42 }, 1); + + cali_id_t attrs[2] = { ptr_in_attr, ptr_out_attr }; + int scope = CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD; + + cali_begin_byname("test_alloc.allocated.0"); + int* A_inside = A; + int* A_outside = A - 1; + + cali_variant_t v_p_i = cali_make_variant(CALI_TYPE_ADDR, &A_inside, sizeof(int*)); + cali_variant_t v_p_o = cali_make_variant(CALI_TYPE_ADDR, &A_outside, sizeof(int*)); + + cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); + cali_end_byname("test_alloc.allocated.0"); + + cali_begin_byname("test_alloc.allocated.1"); + A_inside = A + 41; + /* int* A_outside = A+42; this should fail but doesn't: needs fix */ + A_outside = A + 43; + + v_p_i = cali_make_variant(CALI_TYPE_ADDR, &A_inside, sizeof(int*)); + v_p_o = cali_make_variant(CALI_TYPE_ADDR, &A_outside, sizeof(int*)); + + cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); + cali_end_byname("test_alloc.allocated.1"); + + cali_datatracker_untrack(A); + free(A); + A = NULL; + + cali_begin_byname("test_alloc.freed"); + cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); + cali_end_byname("test_alloc.freed"); + + CALI_MARK_FUNCTION_END; } int main() { - CALI_MARK_FUNCTION_BEGIN; + CALI_MARK_FUNCTION_BEGIN; - ci_test_alloc(); + ci_test_alloc(); - CALI_MARK_FUNCTION_END; + CALI_MARK_FUNCTION_END; - return 0; + return 0; } diff --git a/test/ci_app_tests/ci_test_alloc_hooks.c b/test/ci_app_tests/ci_test_alloc_hooks.c index 17c9535b7..abfe68f66 100644 --- a/test/ci_app_tests/ci_test_alloc_hooks.c +++ b/test/ci_app_tests/ci_test_alloc_hooks.c @@ -10,96 +10,102 @@ extern cali_id_t cali_class_memoryaddress_attr_id; -cali_id_t attrs[2]; -int scope = CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD; +cali_id_t attrs[2]; +int scope = CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD; -void test_allocation(void *ptr, size_t size) +void test_allocation(void* ptr, size_t size) { - cali_begin_byname("test_alloc.allocated.0"); - - int* ptr_inside = ptr; - int* ptr_outside = ptr-1; - cali_variant_t v_p_i = - cali_make_variant(CALI_TYPE_ADDR, &ptr_inside, sizeof(void*)); - cali_variant_t v_p_o = - cali_make_variant(CALI_TYPE_ADDR, &ptr_outside, sizeof(void*)); - - cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); - cali_end_byname("test_alloc.allocated.0"); - - cali_begin_byname("test_alloc.allocated.1"); - ptr_inside = ptr+size-1; - ptr_outside = ptr+size; - - v_p_i = cali_make_variant(CALI_TYPE_ADDR, &ptr_inside, sizeof(void*)); - v_p_o = cali_make_variant(CALI_TYPE_ADDR, &ptr_outside, sizeof(void*)); - - cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); - cali_end_byname("test_alloc.allocated.1"); -} - -void test_free(void *ptr) -{ - cali_begin_byname("test_alloc.freed"); - free(ptr); - - int* ptr_inside = ptr; - int* ptr_outside = ptr-1; - cali_variant_t v_p_i = - cali_make_variant(CALI_TYPE_ADDR, &ptr_inside, sizeof(void*)); - cali_variant_t v_p_o = - cali_make_variant(CALI_TYPE_ADDR, &ptr_outside, sizeof(void*)); - - cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); - cali_end_byname("test_alloc.freed"); -} + cali_begin_byname("test_alloc.allocated.0"); -void ci_test_alloc() -{ - CALI_MARK_FUNCTION_BEGIN; + int* ptr_inside = ptr; + int* ptr_outside = ptr - 1; + cali_variant_t v_p_i = cali_make_variant(CALI_TYPE_ADDR, &ptr_inside, sizeof(void*)); + cali_variant_t v_p_o = cali_make_variant(CALI_TYPE_ADDR, &ptr_outside, sizeof(void*)); - cali_variant_t v_true = cali_make_variant_from_bool(true); + cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); + cali_end_byname("test_alloc.allocated.0"); - cali_id_t ptr_in_attr = - cali_create_attribute_with_metadata("ptr_in", CALI_TYPE_ADDR, CALI_ATTR_ASVALUE, - 1, &cali_class_memoryaddress_attr_id, &v_true); - cali_id_t ptr_out_attr = - cali_create_attribute_with_metadata("ptr_out", CALI_TYPE_ADDR, CALI_ATTR_ASVALUE, - 1, &cali_class_memoryaddress_attr_id, &v_true); + cali_begin_byname("test_alloc.allocated.1"); + ptr_inside = ptr + size - 1; + ptr_outside = ptr + size; - attrs[0] = ptr_in_attr; - attrs[1] = ptr_out_attr; + v_p_i = cali_make_variant(CALI_TYPE_ADDR, &ptr_inside, sizeof(void*)); + v_p_o = cali_make_variant(CALI_TYPE_ADDR, &ptr_outside, sizeof(void*)); - int *A = (int*)malloc(sizeof(int)*42); - int *C = (int*)calloc(42, sizeof(int)); - int *R = (int*)malloc(sizeof(int)*100); - R = (int*)realloc(R, sizeof(int)*42); + cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); + cali_end_byname("test_alloc.allocated.1"); +} - cali_begin_byname("test_alloc.malloc_hook"); - test_allocation(A, sizeof(int)*42); - test_free(A); - cali_end_byname("test_alloc.malloc_hook"); +void test_free(void* ptr) +{ + cali_begin_byname("test_alloc.freed"); + free(ptr); - cali_begin_byname("test_alloc.calloc_hook"); - test_allocation(C, sizeof(int)*42); - test_free(C); - cali_end_byname("test_alloc.calloc_hook"); + int* ptr_inside = ptr; + int* ptr_outside = ptr - 1; + cali_variant_t v_p_i = cali_make_variant(CALI_TYPE_ADDR, &ptr_inside, sizeof(void*)); + cali_variant_t v_p_o = cali_make_variant(CALI_TYPE_ADDR, &ptr_outside, sizeof(void*)); - cali_begin_byname("test_alloc.realloc_hook"); - test_allocation(R, sizeof(int)*42); - test_free(R); - cali_end_byname("test_alloc.realloc_hook"); + cali_push_snapshot(scope, 2, attrs, (const cali_variant_t[]) { v_p_i, v_p_o }); + cali_end_byname("test_alloc.freed"); +} - CALI_MARK_FUNCTION_END; +void ci_test_alloc() +{ + CALI_MARK_FUNCTION_BEGIN; + + cali_variant_t v_true = cali_make_variant_from_bool(true); + + cali_id_t ptr_in_attr = cali_create_attribute_with_metadata( + "ptr_in", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE, + 1, + &cali_class_memoryaddress_attr_id, + &v_true + ); + cali_id_t ptr_out_attr = cali_create_attribute_with_metadata( + "ptr_out", + CALI_TYPE_ADDR, + CALI_ATTR_ASVALUE, + 1, + &cali_class_memoryaddress_attr_id, + &v_true + ); + + attrs[0] = ptr_in_attr; + attrs[1] = ptr_out_attr; + + int* A = (int*) malloc(sizeof(int) * 42); + int* C = (int*) calloc(42, sizeof(int)); + int* R = (int*) malloc(sizeof(int) * 100); + R = (int*) realloc(R, sizeof(int) * 42); + + cali_begin_byname("test_alloc.malloc_hook"); + test_allocation(A, sizeof(int) * 42); + test_free(A); + cali_end_byname("test_alloc.malloc_hook"); + + cali_begin_byname("test_alloc.calloc_hook"); + test_allocation(C, sizeof(int) * 42); + test_free(C); + cali_end_byname("test_alloc.calloc_hook"); + + cali_begin_byname("test_alloc.realloc_hook"); + test_allocation(R, sizeof(int) * 42); + test_free(R); + cali_end_byname("test_alloc.realloc_hook"); + + CALI_MARK_FUNCTION_END; } int main() { - CALI_MARK_FUNCTION_BEGIN; + CALI_MARK_FUNCTION_BEGIN; - ci_test_alloc(); + ci_test_alloc(); - CALI_MARK_FUNCTION_END; + CALI_MARK_FUNCTION_END; - return 0; + return 0; } diff --git a/test/ci_app_tests/ci_test_basic.cpp b/test/ci_app_tests/ci_test_basic.cpp index 61de9cb6d..e2a3ff308 100644 --- a/test/ci_app_tests/ci_test_basic.cpp +++ b/test/ci_app_tests/ci_test_basic.cpp @@ -6,9 +6,7 @@ int main(int argc, char* argv[]) { - std::map metadata = { - { "meta.int", cali::Variant(42) } - }; + std::map metadata = { { "meta.int", cali::Variant(42) } }; // Test proper escaping cali_set_string_byname(" =\\weird \"\"attribute\"= ", " \\\\ weird,\" name\","); @@ -18,7 +16,7 @@ int main(int argc, char* argv[]) cali_set_string_byname("newline", "A newline:\n!"); cali::Annotation phase_ann("myphase", metadata); - std::size_t size = 8; + std::size_t size = 8; cali::Annotation size_annot("dgs"); size_annot.begin(size); phase_ann.begin("initialization"); @@ -31,8 +29,7 @@ int main(int argc, char* argv[]) cali::Annotation iter_ann("iteration", CALI_ATTR_ASVALUE); iter_ann.begin(uint64_t(5)); for (int i = 0; i < count; ++i) { - cali::Annotation::Guard - g_iter_ann(iter_ann.begin(i)); + cali::Annotation::Guard g_iter_ann(iter_ann.begin(i)); } phase_ann = copy_ann; diff --git a/test/ci_app_tests/ci_test_binding.cpp b/test/ci_app_tests/ci_test_binding.cpp index f5f044084..81cf950c2 100644 --- a/test/ci_app_tests/ci_test_binding.cpp +++ b/test/ci_app_tests/ci_test_binding.cpp @@ -18,25 +18,24 @@ class TestBinding : public AnnotationBinding public: - const char* service_tag() const { - return "testbinding"; - } + const char* service_tag() const { return "testbinding"; } - void initialize(Caliper* c, Channel*) { - m_my_attr = - c->create_attribute("testbinding", CALI_TYPE_STRING, CALI_ATTR_UNALIGNED); - m_prop_attr = - c->create_attribute("testproperty", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + void initialize(Caliper* c, Channel*) + { + m_my_attr = c->create_attribute("testbinding", CALI_TYPE_STRING, CALI_ATTR_UNALIGNED); + m_prop_attr = c->create_attribute("testproperty", CALI_TYPE_INT, CALI_ATTR_DEFAULT); } - void on_mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) { + void on_mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) + { if (s_verbose) std::cout << "TestBinding::on_mark_attribute(" << attr.name() << ")" << std::endl; c->make_tree_entry(m_prop_attr, Variant(4242), c->node(attr.id())); } - void on_begin(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void on_begin(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if (attr == m_my_attr) return; @@ -51,7 +50,8 @@ class TestBinding : public AnnotationBinding std::cout << "begin " << s << std::endl; } - void on_end(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) { + void on_end(Caliper* c, Channel* chn, const Attribute& attr, const Variant& value) + { if (attr == m_my_attr) return; @@ -61,14 +61,11 @@ class TestBinding : public AnnotationBinding std::cout << "end " << attr.name() << "=" << value.to_string() << std::endl; } - static void set_verbose(bool v) { - s_verbose = v; - } + static void set_verbose(bool v) { s_verbose = v; } }; bool TestBinding::s_verbose = false; - int main(int argc, const char** argv) { if (argc > 1 && 0 == strcmp(argv[1], "--verbose")) @@ -79,13 +76,11 @@ int main(int argc, const char** argv) AnnotationBinding::make_binding(&c, &channel); - Attribute nested_attr = - c.create_attribute("binding.nested", CALI_TYPE_STRING, CALI_ATTR_NESTED); - Attribute default_attr = - c.create_attribute("binding.default", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + Attribute nested_attr = c.create_attribute("binding.nested", CALI_TYPE_STRING, CALI_ATTR_NESTED); + Attribute default_attr = c.create_attribute("binding.default", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - c.begin(nested_attr, Variant(CALI_TYPE_STRING, "outer", 6)); - c.begin(nested_attr, Variant(CALI_TYPE_STRING, "inner", 6)); + c.begin(nested_attr, Variant(CALI_TYPE_STRING, "outer", 6)); + c.begin(nested_attr, Variant(CALI_TYPE_STRING, "inner", 6)); c.begin(default_attr, Variant(CALI_TYPE_STRING, "default", 8)); c.end(default_attr); c.end(nested_attr); diff --git a/test/ci_app_tests/ci_test_c_ann.c b/test/ci_app_tests/ci_test_c_ann.c index 8eeb80086..771025a4c 100644 --- a/test/ci_app_tests/ci_test_c_ann.c +++ b/test/ci_app_tests/ci_test_c_ann.c @@ -8,68 +8,70 @@ int main(int argc, char* argv[]) { - cali_config_preset("CALI_CHANNEL_FLUSH_ON_EXIT", "false"); - - cali_ConfigManager mgr; - cali_ConfigManager_new(&mgr); - - if (argc > 1) - cali_ConfigManager_add(&mgr, argv[1]); - if (cali_ConfigManager_error(&mgr)) { - cali_SHROUD_array errmsg; - cali_ConfigManager_error_msg_bufferify(&mgr, &errmsg); - fprintf(stderr, "Caliper config error: %s\n", errmsg.addr.ccharp); - cali_SHROUD_memory_destructor(&errmsg.cxx); - cali_ConfigManager_delete(&mgr); - return -1; - } + cali_config_preset("CALI_CHANNEL_FLUSH_ON_EXIT", "false"); + + cali_ConfigManager mgr; + cali_ConfigManager_new(&mgr); - cali_ConfigManager_start(&mgr); + if (argc > 1) + cali_ConfigManager_add(&mgr, argv[1]); + if (cali_ConfigManager_error(&mgr)) { + cali_SHROUD_array errmsg; + cali_ConfigManager_error_msg_bufferify(&mgr, &errmsg); + fprintf(stderr, "Caliper config error: %s\n", errmsg.addr.ccharp); + cali_SHROUD_memory_destructor(&errmsg.cxx); + cali_ConfigManager_delete(&mgr); + return -1; + } - cali_set_global_double_byname("global.double", 42.42); - cali_set_global_int_byname("global.int", 1337); - cali_set_global_string_byname("global.string", "my global string"); - cali_set_global_uint_byname("global.uint", 42); + cali_ConfigManager_start(&mgr); - cali_id_t iter_attr = - cali_create_attribute("iteration", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + cali_set_global_double_byname("global.double", 42.42); + cali_set_global_int_byname("global.int", 1337); + cali_set_global_string_byname("global.string", "my global string"); + cali_set_global_uint_byname("global.uint", 42); - cali_begin_string_byname("phase", "loop"); + cali_id_t iter_attr = cali_create_attribute("iteration", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - for (int i = 0; i < 4; ++i) { - cali_begin_int(iter_attr, i); - cali_end(iter_attr); - } + cali_begin_string_byname("phase", "loop"); - cali_end_byname("phase"); + for (int i = 0; i < 4; ++i) { + cali_begin_int(iter_attr, i); + cali_end(iter_attr); + } - cali_begin_byname("ci_test_c_ann.meta-attr"); + cali_end_byname("phase"); - cali_id_t meta_attr = - cali_create_attribute("meta-attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT); - cali_variant_t meta_val = - cali_make_variant_from_int(47); + cali_begin_byname("ci_test_c_ann.meta-attr"); - cali_id_t test_attr = - cali_create_attribute_with_metadata("test-attr-with-metadata", CALI_TYPE_STRING, CALI_ATTR_UNALIGNED, - 1, &meta_attr, &meta_val); + cali_id_t meta_attr = cali_create_attribute("meta-attr", CALI_TYPE_INT, CALI_ATTR_DEFAULT); + cali_variant_t meta_val = cali_make_variant_from_int(47); - cali_set_string(test_attr, "abracadabra"); + cali_id_t test_attr = cali_create_attribute_with_metadata( + "test-attr-with-metadata", + CALI_TYPE_STRING, + CALI_ATTR_UNALIGNED, + 1, + &meta_attr, + &meta_val + ); - cali_end_byname("ci_test_c_ann.meta-attr"); + cali_set_string(test_attr, "abracadabra"); - cali_begin_byname("ci_test_c_ann.setbyname"); + cali_end_byname("ci_test_c_ann.meta-attr"); - cali_set_int_byname("attr.int", 20); - cali_set_double_byname("attr.dbl", 1.25); - cali_set_string_byname("attr.str", "fidibus"); + cali_begin_byname("ci_test_c_ann.setbyname"); - cali_end_byname("ci_test_c_ann.setbyname"); + cali_set_int_byname("attr.int", 20); + cali_set_double_byname("attr.dbl", 1.25); + cali_set_string_byname("attr.str", "fidibus"); - cali_ConfigManager_flush(&mgr); - cali_ConfigManager_delete(&mgr); + cali_end_byname("ci_test_c_ann.setbyname"); + + cali_ConfigManager_flush(&mgr); + cali_ConfigManager_delete(&mgr); - cali_flush(CALI_FLUSH_CLEAR_BUFFERS); + cali_flush(CALI_FLUSH_CLEAR_BUFFERS); - return 0; + return 0; } diff --git a/test/ci_app_tests/ci_test_c_snapshot.c b/test/ci_app_tests/ci_test_c_snapshot.c index 4626b33c0..1fc0395c4 100644 --- a/test/ci_app_tests/ci_test_c_snapshot.c +++ b/test/ci_app_tests/ci_test_c_snapshot.c @@ -6,30 +6,24 @@ int main() { - cali_begin_string_byname("ci_test_c", "snapshot"); - - /* Test w/o event trigger info */ - cali_push_snapshot(CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD, - 0, NULL, NULL); - - cali_id_t event_str_attr = - cali_create_attribute("string_arg", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); - cali_id_t event_val_attr = - cali_create_attribute("int_arg", CALI_TYPE_INT, CALI_ATTR_ASVALUE); - - int event_val = 42; - const char* event_str = "teststring"; - - cali_id_t event_attr[2] = { event_str_attr, event_val_attr }; - cali_variant_t event_data[2] = { - cali_make_variant(CALI_TYPE_STRING, event_str, strlen(event_str)), - cali_make_variant_from_int(event_val) - }; - - cali_push_snapshot(CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD, - 2, event_attr, event_data); - - cali_end_byname("ci_test_c"); - - return 0; + cali_begin_string_byname("ci_test_c", "snapshot"); + + /* Test w/o event trigger info */ + cali_push_snapshot(CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD, 0, NULL, NULL); + + cali_id_t event_str_attr = cali_create_attribute("string_arg", CALI_TYPE_STRING, CALI_ATTR_DEFAULT); + cali_id_t event_val_attr = cali_create_attribute("int_arg", CALI_TYPE_INT, CALI_ATTR_ASVALUE); + + int event_val = 42; + const char* event_str = "teststring"; + + cali_id_t event_attr[2] = { event_str_attr, event_val_attr }; + cali_variant_t event_data[2] = { cali_make_variant(CALI_TYPE_STRING, event_str, strlen(event_str)), + cali_make_variant_from_int(event_val) }; + + cali_push_snapshot(CALI_SCOPE_PROCESS | CALI_SCOPE_THREAD, 2, event_attr, event_data); + + cali_end_byname("ci_test_c"); + + return 0; } diff --git a/test/ci_app_tests/ci_test_cali_before_mpi.cpp b/test/ci_app_tests/ci_test_cali_before_mpi.cpp index a14b4ccf1..11ebff71b 100644 --- a/test/ci_app_tests/ci_test_cali_before_mpi.cpp +++ b/test/ci_app_tests/ci_test_cali_before_mpi.cpp @@ -8,22 +8,22 @@ int main(int argc, char* argv[]) { cali_mpi_init(); - + CALI_CXX_MARK_FUNCTION; - + MPI_Init(&argc, &argv); MPI_Barrier(MPI_COMM_WORLD); // some MPI functions to test the wrapper blacklist/whitelist int val = 42; - + MPI_Bcast(&val, 1, MPI_INT, 0, MPI_COMM_WORLD); int in = val, out; MPI_Reduce(&in, &out, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); - + MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); diff --git a/test/ci_app_tests/ci_test_channel_api.c b/test/ci_app_tests/ci_test_channel_api.c index 2c2ee90d2..915e02c8b 100644 --- a/test/ci_app_tests/ci_test_channel_api.c +++ b/test/ci_app_tests/ci_test_channel_api.c @@ -1,21 +1,17 @@ #include - int main() { - const char* channel_cfg[][2] = { - { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, - { NULL, NULL } - }; + const char* channel_cfg[][2] = { { "CALI_CHANNEL_FLUSH_ON_EXIT", "false" }, { NULL, NULL } }; cali_configset_t cfg = cali_create_configset(channel_cfg); cali_configset_set(cfg, "CALI_SERVICES_ENABLE", "event,trace,recorder"); cali_configset_set(cfg, "CALI_RECORDER_FILENAME", "stdout"); - + cali_id_t chn_a = cali_create_channel("channel.a", 0, cfg); cali_id_t chn_b = cali_create_channel("channel.b", 0, cfg); - + cali_delete_configset(cfg); CALI_MARK_BEGIN("foo"); @@ -26,7 +22,7 @@ int main() cali_set_int_byname("a", 2); cali_end_byname("a"); cali_activate_channel(chn_b); - + cali_deactivate_channel(chn_a); cali_set_int_byname("c", 8); cali_end_byname("c"); diff --git a/test/ci_app_tests/ci_test_collective_output_channel.cpp b/test/ci_app_tests/ci_test_collective_output_channel.cpp index f9e64af42..5aa356cc7 100644 --- a/test/ci_app_tests/ci_test_collective_output_channel.cpp +++ b/test/ci_app_tests/ci_test_collective_output_channel.cpp @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) MPI_Comm_rank(MPI_COMM_WORLD, &rank); std::shared_ptr channel; - std::string errmsg; + std::string errmsg; std::tie(channel, errmsg) = cali::make_collective_output_channel(argc > 1 ? argv[1] : ""); diff --git a/test/ci_app_tests/ci_test_mpi_before_cali.cpp b/test/ci_app_tests/ci_test_mpi_before_cali.cpp index 4bba64e2c..bb91008bf 100644 --- a/test/ci_app_tests/ci_test_mpi_before_cali.cpp +++ b/test/ci_app_tests/ci_test_mpi_before_cali.cpp @@ -39,7 +39,7 @@ int main(int argc, char* argv[]) MPI_Bcast(&val, 1, MPI_INT, 0, MPI_COMM_WORLD); - int in = val, out; + int in = val, out; CALI_MARK_COMM_REGION_BEGIN("reduction"); MPI_Reduce(&in, &out, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); diff --git a/test/ci_app_tests/ci_test_mpi_channel_manager.cpp b/test/ci_app_tests/ci_test_mpi_channel_manager.cpp index 626f9b1c0..086c0b027 100644 --- a/test/ci_app_tests/ci_test_mpi_channel_manager.cpp +++ b/test/ci_app_tests/ci_test_mpi_channel_manager.cpp @@ -16,7 +16,7 @@ int main(int argc, char* argv[]) { MPI_Init(&argc, &argv); - int worldrank = 0; + int worldrank = 0; MPI_Comm subcomm; MPI_Comm_rank(MPI_COMM_WORLD, &worldrank); MPI_Comm_split(MPI_COMM_WORLD, worldrank % 2, worldrank, &subcomm); diff --git a/test/ci_app_tests/ci_test_nesting.cpp b/test/ci_app_tests/ci_test_nesting.cpp index 9efb1ee25..e3211ac0f 100644 --- a/test/ci_app_tests/ci_test_nesting.cpp +++ b/test/ci_app_tests/ci_test_nesting.cpp @@ -8,22 +8,16 @@ using namespace cali; - -namespace +namespace { void* mismatch_thread_fn(void*) { Caliper c; - Attribute tA = - c.create_attribute("mismatch-thread.A", CALI_TYPE_INT, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_NESTED); - Attribute tN = - c.create_attribute("mismatch-thread.N", CALI_TYPE_INT, CALI_ATTR_SCOPE_THREAD); - Attribute tB = - c.create_attribute("mismatch-thread.B", CALI_TYPE_INT, - CALI_ATTR_SCOPE_THREAD | CALI_ATTR_NESTED); + Attribute tA = c.create_attribute("mismatch-thread.A", CALI_TYPE_INT, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_NESTED); + Attribute tN = c.create_attribute("mismatch-thread.N", CALI_TYPE_INT, CALI_ATTR_SCOPE_THREAD); + Attribute tB = c.create_attribute("mismatch-thread.B", CALI_TYPE_INT, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_NESTED); c.begin(tA, Variant(16)); c.begin(tN, Variant(25)); @@ -35,7 +29,7 @@ void* mismatch_thread_fn(void*) return NULL; } -} // namespace +} // namespace void test_nesting_threadscope() { @@ -50,36 +44,29 @@ void test_nesting_procscope() Caliper c; Variant v_true(true); - Attribute tA = - c.create_attribute("mismatch-process.A", CALI_TYPE_INT, - CALI_SCOPE_PROCESS | CALI_ATTR_NESTED); - Attribute tN = - c.create_attribute("mismatch-process.N", CALI_TYPE_INT, CALI_SCOPE_PROCESS); - Attribute tB = - c.create_attribute("mismatch-process.B", CALI_TYPE_INT, - CALI_SCOPE_PROCESS | CALI_ATTR_NESTED); + Attribute tA = c.create_attribute("mismatch-process.A", CALI_TYPE_INT, CALI_SCOPE_PROCESS | CALI_ATTR_NESTED); + Attribute tN = c.create_attribute("mismatch-process.N", CALI_TYPE_INT, CALI_SCOPE_PROCESS); + Attribute tB = c.create_attribute("mismatch-process.B", CALI_TYPE_INT, CALI_SCOPE_PROCESS | CALI_ATTR_NESTED); c.begin(tA, Variant(16)); c.begin(tN, Variant(25)); c.begin(tB, Variant(32)); c.end(tN); // this should work: non-nested attribute c.end(tA); // error: incorrect nesting! - c.end(tB); + c.end(tB); } void test_nesting_end_missing() { Caliper c; - Attribute tA = - c.create_attribute("missing-end.A", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD); - Attribute tN = - c.create_attribute("missing-end.N", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD); + Attribute tA = c.create_attribute("missing-end.A", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD); + Attribute tN = c.create_attribute("missing-end.N", CALI_TYPE_STRING, CALI_ATTR_SCOPE_THREAD); c.begin(tN, Variant(CALI_TYPE_STRING, "no.error.0", 11)); - c.begin(tA, Variant(CALI_TYPE_STRING, "missing.0", 10)); + c.begin(tA, Variant(CALI_TYPE_STRING, "missing.0", 10)); c.begin(tN, Variant(CALI_TYPE_STRING, "no.error.1", 11)); - c.begin(tA, Variant(CALI_TYPE_STRING, "missing.1", 10)); + c.begin(tA, Variant(CALI_TYPE_STRING, "missing.1", 10)); c.begin(tA, Variant(CALI_TYPE_STRING, "no.error.2", 11)); c.end(tN); @@ -87,18 +74,15 @@ void test_nesting_end_missing() c.end(tN); } - int main(int argc, char* argv[]) { const struct test_info_t { const char* name; void (*fn)(void); - } test_info[] = { - { "nesting_threadscope", test_nesting_threadscope }, - { "nesting_procscope", test_nesting_procscope }, - { "nesting_end_missing", test_nesting_end_missing }, - { 0, 0 } - }; + } test_info[] = { { "nesting_threadscope", test_nesting_threadscope }, + { "nesting_procscope", test_nesting_procscope }, + { "nesting_end_missing", test_nesting_end_missing }, + { 0, 0 } }; Caliper c; @@ -106,7 +90,7 @@ int main(int argc, char* argv[]) if (argc > 1) { const test_info_t* t = test_info; - for ( ; t->name && 0 != strcmp(argv[1], t->name); ++t) + for (; t->name && 0 != strcmp(argv[1], t->name); ++t) ; if (!t->name) { diff --git a/test/ci_app_tests/ci_test_openmp.cpp b/test/ci_app_tests/ci_test_openmp.cpp index 3be318255..a3c16b14a 100644 --- a/test/ci_app_tests/ci_test_openmp.cpp +++ b/test/ci_app_tests/ci_test_openmp.cpp @@ -27,7 +27,7 @@ int main(int argc, char* argv[]) int sum = 0; - #pragma omp parallel for reduction(+:sum) +#pragma omp parallel for reduction(+ : sum) for (int i = 0; i < 42; ++i) { sum += i; } diff --git a/test/ci_app_tests/ci_test_thread.cpp b/test/ci_app_tests/ci_test_thread.cpp index 4428c3eb5..eb6afdc14 100644 --- a/test/ci_app_tests/ci_test_thread.cpp +++ b/test/ci_app_tests/ci_test_thread.cpp @@ -23,30 +23,27 @@ int main() { CALI_CXX_MARK_FUNCTION; - thread_attr_id = - cali_create_attribute("my_thread_id", CALI_TYPE_INT, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED); + thread_attr_id = cali_create_attribute("my_thread_id", CALI_TYPE_INT, CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED); - cali::Caliper c; + cali::Caliper c; cali::RuntimeConfig exp_nopthread_cfg; exp_nopthread_cfg.set("CALI_SERVICES_ENABLE", "event,trace,recorder"); exp_nopthread_cfg.set("CALI_RECORDER_FILENAME", "stdout"); - cali::Channel exp_nopthread = - c.create_channel("exp_nopthread", exp_nopthread_cfg); + cali::Channel exp_nopthread = c.create_channel("exp_nopthread", exp_nopthread_cfg); cali::RuntimeConfig exp_pthread_cfg; exp_pthread_cfg.set("CALI_SERVICES_ENABLE", "event,trace,pthread,recorder"); exp_pthread_cfg.set("CALI_RECORDER_FILENAME", "stdout"); - cali::Channel exp_pthread = - c.create_channel("exp_pthread", exp_pthread_cfg); + cali::Channel exp_pthread = c.create_channel("exp_pthread", exp_pthread_cfg); int thread_ids[4] = { 16, 25, 36, 49 }; pthread_t thread[4]; - cali::Annotation("local", CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED).set(99); + cali::Annotation("local", CALI_ATTR_SCOPE_THREAD | CALI_ATTR_UNALIGNED).set(99); cali::Annotation("global", CALI_ATTR_SCOPE_PROCESS | CALI_ATTR_UNALIGNED).set(999); for (int i = 0; i < 4; ++i)