From c4afd618c4ff0abecae8a2d0cd83e80813771e29 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 6 Feb 2017 08:38:25 -0800 Subject: [PATCH] Add USDT for operator execution Summary: Import relevant headers from folly. Reviewed By: azzolini Differential Revision: D4342793 fbshipit-source-id: 77471e1afd70e399805e4c46e5320ccc3e39d69c --- caffe2/core/net.cc | 10 ++- caffe2/core/static_tracepoint.h | 27 ++++++ caffe2/core/static_tracepoint_elfx86.h | 116 +++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 caffe2/core/static_tracepoint.h create mode 100644 caffe2/core/static_tracepoint_elfx86.h diff --git a/caffe2/core/net.cc b/caffe2/core/net.cc index 1a3d763fd68..5e1d4f5131b 100644 --- a/caffe2/core/net.cc +++ b/caffe2/core/net.cc @@ -6,6 +6,7 @@ #include #include "caffe2/core/operator.h" +#include "caffe2/core/static_tracepoint.h" #include "caffe2/core/timer.h" #include "caffe2/proto/caffe2.pb.h" #include "caffe2/utils/proto_utils.h" @@ -756,8 +757,13 @@ class DAGNet : public DAGNetBase { protected: bool RunAt(const std::vector& chain) override { bool success = true; - for (const auto idx : chain) { - success &= operator_nodes_[idx].operator_->Run(); + const auto& net_name = name_.c_str(); + for (const auto i : chain) { + const auto& op_name = operator_nodes_[i].operator_->def().name().c_str(); + const auto& op_type = operator_nodes_[i].operator_->def().type().c_str(); + CAFFE_SDT(operator_start, net_name, op_name, op_type); + success &= operator_nodes_[i].operator_->Run(); + CAFFE_SDT(operator_done, net_name, op_name, op_type); } return success; } diff --git a/caffe2/core/static_tracepoint.h b/caffe2/core/static_tracepoint.h new file mode 100644 index 00000000000..5c3d3a1b4df --- /dev/null +++ b/caffe2/core/static_tracepoint.h @@ -0,0 +1,27 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#if defined(__ELF__) && (defined(__x86_64__) || defined(__i386__)) +#include + +#define CAFFE_SDT(name, ...) \ + CAFFE_SDT_PROBE_N( \ + caffe2, name, CAFFE_SDT_NARG(0, ##__VA_ARGS__), ##__VA_ARGS__) +#else +#define CAFFE_SDT(name, ...) do {} while(0) +#endif diff --git a/caffe2/core/static_tracepoint_elfx86.h b/caffe2/core/static_tracepoint_elfx86.h new file mode 100644 index 00000000000..7752801af61 --- /dev/null +++ b/caffe2/core/static_tracepoint_elfx86.h @@ -0,0 +1,116 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// Default constraint for the probe arguments as operands. +#ifndef CAFFE_SDT_ARG_CONSTRAINT +#define CAFFE_SDT_ARG_CONSTRAINT "nor" +#endif + +// Instruction to emit for the probe. +#define CAFFE_SDT_NOP nop + +// Note section properties. +#define CAFFE_SDT_NOTE_NAME "stapsdt" +#define CAFFE_SDT_NOTE_TYPE 3 + +// Size of address depending on platform. +#ifdef __LP64__ +#define CAFFE_SDT_ASM_ADDR .8byte +#else +#define CAFFE_SDT_ASM_ADDR .4byte +#endif + +// Assembler helper Macros. +#define CAFFE_SDT_S(x) #x +#define CAFFE_SDT_ASM_1(x) CAFFE_SDT_S(x) "\n" +#define CAFFE_SDT_ASM_2(a, b) CAFFE_SDT_S(a) "," CAFFE_SDT_S(b) "\n" +#define CAFFE_SDT_ASM_3(a, b, c) CAFFE_SDT_S(a) "," CAFFE_SDT_S(b) "," \ + CAFFE_SDT_S(c) "\n" +#define CAFFE_SDT_ASM_STRING(x) CAFFE_SDT_ASM_1(.asciz CAFFE_SDT_S(x)) + +// Helper to determine the size of an argument. +#define CAFFE_SDT_ISARRAY(x) (__builtin_classify_type(x) == 14) +#define CAFFE_SDT_ARGSIZE(x) (CAFFE_SDT_ISARRAY(x) ? sizeof(void*) : sizeof(x)) + +// Format of each probe arguments as operand. +// Size of the arugment tagged with CAFFE_SDT_Sn, with "n" constraint. +// Value of the argument tagged with CAFFE_SDT_An, with configured constraint. +#define CAFFE_SDT_ARG(n, x) \ + [CAFFE_SDT_S##n] "n" ((size_t)CAFFE_SDT_ARGSIZE(x)), \ + [CAFFE_SDT_A##n] CAFFE_SDT_ARG_CONSTRAINT (x) + +// Templates to append arguments as operands. +#define CAFFE_SDT_OPERANDS_0() [__sdt_dummy] "g" (0) +#define CAFFE_SDT_OPERANDS_1(_1) CAFFE_SDT_ARG(1, _1) +#define CAFFE_SDT_OPERANDS_2(_1, _2) \ + CAFFE_SDT_OPERANDS_1(_1), CAFFE_SDT_ARG(2, _2) +#define CAFFE_SDT_OPERANDS_3(_1, _2, _3) \ + CAFFE_SDT_OPERANDS_2(_1, _2), CAFFE_SDT_ARG(3, _3) +#define CAFFE_SDT_OPERANDS_4(_1, _2, _3, _4) \ + CAFFE_SDT_OPERANDS_3(_1, _2, _3), CAFFE_SDT_ARG(4, _4) +#define CAFFE_SDT_OPERANDS_5(_1, _2, _3, _4, _5) \ + CAFFE_SDT_OPERANDS_4(_1, _2, _3, _4), CAFFE_SDT_ARG(5, _5) +#define CAFFE_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6) \ + CAFFE_SDT_OPERANDS_5(_1, _2, _3, _4, _5), CAFFE_SDT_ARG(6, _6) +#define CAFFE_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7) \ + CAFFE_SDT_OPERANDS_6(_1, _2, _3, _4, _5, _6), CAFFE_SDT_ARG(7, _7) +#define CAFFE_SDT_OPERANDS_8(_1, _2, _3, _4, _5, _6, _7, _8) \ + CAFFE_SDT_OPERANDS_7(_1, _2, _3, _4, _5, _6, _7), CAFFE_SDT_ARG(8, _8) + +// Templates to reference the arguments from operands in note section. +#define CAFFE_SDT_ARGFMT(no) %n[CAFFE_SDT_S##no]@%[CAFFE_SDT_A##no] +#define CAFFE_SDT_ARG_TEMPLATE_0 /*No arguments*/ +#define CAFFE_SDT_ARG_TEMPLATE_1 CAFFE_SDT_ARGFMT(1) +#define CAFFE_SDT_ARG_TEMPLATE_2 CAFFE_SDT_ARG_TEMPLATE_1 CAFFE_SDT_ARGFMT(2) +#define CAFFE_SDT_ARG_TEMPLATE_3 CAFFE_SDT_ARG_TEMPLATE_2 CAFFE_SDT_ARGFMT(3) +#define CAFFE_SDT_ARG_TEMPLATE_4 CAFFE_SDT_ARG_TEMPLATE_3 CAFFE_SDT_ARGFMT(4) +#define CAFFE_SDT_ARG_TEMPLATE_5 CAFFE_SDT_ARG_TEMPLATE_4 CAFFE_SDT_ARGFMT(5) +#define CAFFE_SDT_ARG_TEMPLATE_6 CAFFE_SDT_ARG_TEMPLATE_5 CAFFE_SDT_ARGFMT(6) +#define CAFFE_SDT_ARG_TEMPLATE_7 CAFFE_SDT_ARG_TEMPLATE_6 CAFFE_SDT_ARGFMT(7) +#define CAFFE_SDT_ARG_TEMPLATE_8 CAFFE_SDT_ARG_TEMPLATE_7 CAFFE_SDT_ARGFMT(8) + +// Structure of note section for the probe. +#define CAFFE_SDT_NOTE_CONTENT(provider, name, arg_template) \ + CAFFE_SDT_ASM_1(990: CAFFE_SDT_NOP) \ + CAFFE_SDT_ASM_3( .pushsection .note.stapsdt,"","note") \ + CAFFE_SDT_ASM_1( .balign 4) \ + CAFFE_SDT_ASM_3( .4byte 992f-991f, 994f-993f, CAFFE_SDT_NOTE_TYPE) \ + CAFFE_SDT_ASM_1(991: .asciz CAFFE_SDT_NOTE_NAME) \ + CAFFE_SDT_ASM_1(992: .balign 4) \ + CAFFE_SDT_ASM_1(993: CAFFE_SDT_ASM_ADDR 990b) \ + CAFFE_SDT_ASM_1( CAFFE_SDT_ASM_ADDR 0) /*Reserved for Semaphore address*/\ + CAFFE_SDT_ASM_1( CAFFE_SDT_ASM_ADDR 0) /*Reserved for Semaphore name*/ \ + CAFFE_SDT_ASM_STRING(provider) \ + CAFFE_SDT_ASM_STRING(name) \ + CAFFE_SDT_ASM_STRING(arg_template) \ + CAFFE_SDT_ASM_1(994: .balign 4) \ + CAFFE_SDT_ASM_1( .popsection) + +// Main probe Macro. +#define CAFFE_SDT_PROBE(provider, name, n, arglist) \ + __asm__ __volatile__ ( \ + CAFFE_SDT_NOTE_CONTENT(provider, name, CAFFE_SDT_ARG_TEMPLATE_##n) \ + :: CAFFE_SDT_OPERANDS_##n arglist \ + ) \ + +// Helper Macros to handle variadic arguments. +#define CAFFE_SDT_NARG_(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N +#define CAFFE_SDT_NARG(...) \ + CAFFE_SDT_NARG_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0) +#define CAFFE_SDT_PROBE_N(provider, name, N, ...) \ + CAFFE_SDT_PROBE(provider, name, N, (__VA_ARGS__))