midout.cpp 2.6 KB
Newer Older
王博文 已提交
1 2 3 4 5 6 7 8 9 10 11
/**
 * \file src/midout.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

J
jiakai 已提交
12 13
#include "midout.h"

王博文 已提交
14
// to avoid linking error for empty lib on some platforms
15 16 17
void midout_empty() {
}

J
jiakai 已提交
18
#ifdef MIDOUT_PROFILING
J
jiakai 已提交
19 20
#pragma message "midout profiling enabled"

J
jiakai 已提交
21
#include <string>
J
jiakai 已提交
22 23 24
#include <vector>
#include <unordered_set>
#include <algorithm>
J
jiakai 已提交
25
#include <mutex>
J
jiakai 已提交
26 27 28 29 30 31 32

#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <sys/types.h>
#include <unistd.h>
Z
zengping 已提交
33
#include <errno.h>
J
jiakai 已提交
34 35 36 37

namespace {
    class UsedTypes {
        std::unordered_set<const char*> m_types;
J
jiakai 已提交
38
        std::mutex m_mtx;
J
jiakai 已提交
39 40 41 42

        public:

            void add(const char *type) {
J
jiakai 已提交
43
                std::lock_guard<std::mutex> guard{m_mtx};
J
jiakai 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
                m_types.insert(type);
            }

            ~UsedTypes() {
                auto output_fname = getenv("MIDOUT_OUTPUT");
                char output_fname_storage[256];
                if (!output_fname) {
                    output_fname = output_fname_storage;
                    snprintf(output_fname, sizeof(output_fname_storage),
                            "midout_trace.%d", static_cast<int>(getpid()));
                }
                FILE *fout = fopen(output_fname, "w");
                if (!fout) {
                    fprintf(stderr,
                            "midout: failed to open output file %s: %s\n",
                            output_fname, strerror(errno));
                }
J
jiakai 已提交
61
                fprintf(fout, "midout_trace v1\n");
J
jiakai 已提交
62 63 64
                std::vector<std::string> sorted_types;
                sorted_types.reserve(m_types.size());
                for (auto &&i: m_types) {
J
jiakai 已提交
65
                    sorted_types.emplace_back(i);
J
jiakai 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
                }
                std::sort(sorted_types.begin(), sorted_types.end());
                for (auto &&i: sorted_types) {
                    fputs(i.c_str(), fout);
                    fputc('\n', fout);
                }
                fclose(fout);
                fprintf(stderr, "midout: %zu items written to %s\n",
                        sorted_types.size(), output_fname);
            }
    };
}

void midout::on_region_used(const std::type_info &type) {
J
jiakai 已提交
80 81
    static UsedTypes used_types;
    used_types.add(type.name());
J
jiakai 已提交
82 83 84
}

#endif  // MIDOUT_PROFILING
J
jiakai 已提交
85 86 87 88

#ifdef MIDOUT_GENERATED
#pragma message "stripping by midout enabled"
#endif