/*************************************************************************
 * Copyright (C) [2023-2024] by Cambricon, Inc.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *************************************************************************/

#include "cnpx.h"
#include <sstream>
#include "torch_ops_api.h"

namespace tmo {
namespace torch_api {

static cnpxDomainHandle_t domain = cnpxDomainCreate("CNPERF_KERNEL_TMO");
static bool cnperf_kernel_analysis = getenv("CNPERF_KERNEL_ANALYSIS");

void cnpxPush(const OpTheory &op) {
  if (cnperf_kernel_analysis) {
    size_t calc = op.getTheoryCalc();
    cnnlDataType_t calc_dtype = op.getCalcDtype();
    size_t io = op.getTheoryIO();
    auto op_name = op.getOpName();
    std::ostringstream jsonStream;
    jsonStream << "{\"name\":\"" << op_name << "\", \"theo_calc\":" << calc
               << ", \"theo_bytes\":" << io << ", \"calc_type\":" << calc_dtype << "}";
    std::string json = jsonStream.str();
    // std::cout << json.c_str() << std::endl;
    cnpxDomainRangePush(domain, json.c_str());
  }
}

void cnpxPop() {
  if (cnperf_kernel_analysis) {
    cnpxDomainRangePop(domain);
  }
}

}  //  namespace torch_api
}  //  namespace tmo
