// Copyright 2021 Google LLC
//
// 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.

#include "util.hpp"

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include <phosphor-logging/log.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/message.hpp>

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>

namespace metric_blob
{

using phosphor::logging::log;
using level = phosphor::logging::level;

char controlCharsToSpace(char c)
{
    if (c < 32)
    {
        c = ' ';
    }
    return c;
}

long getTicksPerSec()
{
    return sysconf(_SC_CLK_TCK);
}

std::string readFileThenGrepIntoString(const std::string_view fileName,
                                       const std::string_view grepStr)
{
    std::stringstream ss;
    std::ifstream ifs(fileName.data());
    while (ifs.good())
    {
        std::string line;
        std::getline(ifs, line);
        if (line.find(grepStr) != std::string::npos)
        {
            ss << line;
        }
        if (ifs.good())
            ss << std::endl;
    }
    return ss.str();
}

bool isNumericPath(const std::string_view path, int& value)
{
    size_t p = path.rfind('/');
    if (p == std::string::npos)
    {
        return false;
    }
    int id = 0;
    for (size_t i = p + 1; i < path.size(); ++i)
    {
        const char ch = path[i];
        if (ch < '0' || ch > '9')
            return false;
        else
        {
            id = id * 10 + (ch - '0');
        }
    }
    value = id;
    return true;
}

// Trims all control characters at the end of a string.
std::string trimStringRight(std::string_view s)
{
    std::string ret(s.data());
    while (!ret.empty())
    {
        if (ret.back() <= 32)
            ret.pop_back();
        else
            break;
    }
    return ret;
}

std::string getCmdLine(const int pid)
{
    const std::string& cmdlinePath = "/proc/" + std::to_string(pid) +
                                     "/cmdline";

    std::string cmdline = readFileThenGrepIntoString(cmdlinePath);
    for (size_t i = 0; i < cmdline.size(); ++i)
    {
        cmdline[i] = controlCharsToSpace(cmdline[i]);
    }

    // Trim empty strings
    cmdline = trimStringRight(cmdline);

    return cmdline;
}

// strtok is used in this function in order to avoid usage of <sstream>.
// However, that would require us to create a temporary std::string.
TcommUtimeStime parseTcommUtimeStimeString(std::string_view content,
                                           const long ticksPerSec)
{
    TcommUtimeStime ret;
    ret.tcomm = "";
    ret.utime = ret.stime = 0;

    const float invTicksPerSec = 1.0f / static_cast<float>(ticksPerSec);

    // pCol now points to the first part in content after content is split by
    // space.
    // This is not ideal,
    std::string temp(content);
    char* pCol = strtok(temp.data(), " ");

    if (pCol != nullptr)
    {
        const int fields[] = {1, 13, 14}; // tcomm, utime, stime
        int fieldIdx = 0;
        for (int colIdx = 0; colIdx < 15; ++colIdx)
        {
            if (fieldIdx < 3 && colIdx == fields[fieldIdx])
            {
                switch (fieldIdx)
                {
                    case 0:
                    {
                        ret.tcomm = std::string(pCol);
                        break;
                    }
                    case 1:
                        [[fallthrough]];
                    case 2:
                    {
                        int ticks = std::atoi(pCol);
                        float t = static_cast<float>(ticks) * invTicksPerSec;

                        if (fieldIdx == 1)
                        {
                            ret.utime = t;
                        }
                        else if (fieldIdx == 2)
                        {
                            ret.stime = t;
                        }
                        break;
                    }
                }
                ++fieldIdx;
            }
            pCol = strtok(nullptr, " ");
        }
    }

    if (ticksPerSec <= 0)
    {
        log<level::ERR>("ticksPerSec is equal or less than zero");
    }

    return ret;
}

TcommUtimeStime getTcommUtimeStime(const int pid, const long ticksPerSec)
{
    const std::string& statPath = "/proc/" + std::to_string(pid) + "/stat";
    return parseTcommUtimeStimeString(readFileThenGrepIntoString(statPath),
                                      ticksPerSec);
}

// Returns true if successfully parsed and false otherwise. If parsing was
// successful, value is set accordingly.
// Input: "MemAvailable:      1234 kB"
// Returns true, value set to 1234
bool parseMeminfoValue(const std::string_view content,
                       const std::string_view keyword, int& value)
{
    size_t p = content.find(keyword);
    if (p != std::string::npos)
    {
        std::string_view v = content.substr(p + keyword.size());
        p = v.find("kB");
        if (p != std::string::npos)
        {
            v = v.substr(0, p);
            value = std::atoi(v.data());
            return true;
        }
    }
    return false;
}

bool parseProcUptime(const std::string_view content, double& uptime,
                     double& idleProcessTime)
{
    double t0, t1; // Attempts to parse uptime and idleProcessTime
    int ret = sscanf(content.data(), "%lf %lf", &t0, &t1);
    if (ret == 2 && std::isfinite(t0) && std::isfinite(t1))
    {
        uptime = t0;
        idleProcessTime = t1;
        return true;
    }
    return false;
}

bool readMem(const uint32_t target, uint32_t& memResult)
{
    int fd = open("/dev/mem", O_RDONLY | O_SYNC);
    if (fd < 0)
    {
        return false;
    }

    int pageSize = getpagesize();
    uint32_t pageOffset = target & ~static_cast<uint32_t>(pageSize - 1);
    uint32_t offsetInPage = target & static_cast<uint32_t>(pageSize - 1);

    void* mapBase = mmap(NULL, pageSize * 2, PROT_READ, MAP_SHARED, fd,
                         pageOffset);
    if (mapBase == MAP_FAILED)
    {
        close(fd);
        return false;
    }

    char* virtAddr = reinterpret_cast<char*>(mapBase) + offsetInPage;
    memResult = *(reinterpret_cast<uint32_t*>(virtAddr));
    close(fd);
    munmap(mapBase, pageSize * 2);
    return true;
}

// clang-format off
/*
 *  power-on
 *  counter(start)                 uptime(start)
 *  firmware(Neg)    loader(Neg)   kernel(always 0)    initrd                 userspace              finish
 *  |----------------|-------------|-------------------|----------------------|----------------------|
 *  |----------------| <--- firmwareTime=firmware-loader
 *                   |-------------| <--- loaderTime=loader
 *  |------------------------------| <--- firmwareTime(Actually is firmware+loader)=counter-uptime \
 *                                        (in this case we can treat this as firmware time \
 *                                         since firmware consumes most of the time)
 *                                 |-------------------| <--- kernelTime=initrd (if initrd present)
 *                                 |------------------------------------------| <--- kernelTime=userspace (if no initrd)
 *                                                     |----------------------| <--- initrdTime=userspace-initrd (if initrd present)
 *                                                                            |----------------------| <--- userspaceTime=finish-userspace
 */
// clang-format on
bool getBootTimesMonotonic(BootTimesMonotonic& btm)
{
    // Timestamp name and its offset in the struct.
    std::vector<std::pair<std::string_view, size_t>> timeMap = {
        {"FirmwareTimestampMonotonic",
         offsetof(BootTimesMonotonic, firmwareTime)}, // negative value
        {"LoaderTimestampMonotonic",
         offsetof(BootTimesMonotonic, loaderTime)},   // negative value
        {"InitRDTimestampMonotonic", offsetof(BootTimesMonotonic, initrdTime)},
        {"UserspaceTimestampMonotonic",
         offsetof(BootTimesMonotonic, userspaceTime)},
        {"FinishTimestampMonotonic", offsetof(BootTimesMonotonic, finishTime)}};

    auto b = sdbusplus::bus::new_default_system();
    auto m = b.new_method_call("org.freedesktop.systemd1",
                               "/org/freedesktop/systemd1",
                               "org.freedesktop.DBus.Properties", "GetAll");
    m.append("");
    auto reply = b.call(m);
    std::vector<std::pair<std::string, std::variant<uint64_t>>> timestamps;
    reply.read(timestamps);

    // Parse timestamps from dbus result.
    auto btmPtr = reinterpret_cast<char*>(&btm);
    unsigned int recordCnt = 0;
    for (auto& t : timestamps)
    {
        for (auto& tm : timeMap)
        {
            if (tm.first.compare(t.first) == 0)
            {
                auto temp = std::get<uint64_t>(t.second);
                memcpy(btmPtr + tm.second, reinterpret_cast<char*>(&temp),
                       sizeof(temp));
                recordCnt++;
                break;
            }
        }
        if (recordCnt == timeMap.size())
        {
            break;
        }
    }
    if (recordCnt != timeMap.size())
    {
        log<level::ERR>("Didn't get desired timestamps");
        return false;
    }

    std::string cpuinfo = readFileThenGrepIntoString("/proc/cpuinfo",
                                                     "Hardware");
    // Nuvoton NPCM7XX chip has a counter which starts from power-on.
    if (cpuinfo.find("NPCM7XX") != std::string::npos)
    {
        // Get elapsed seconds from SEC_CNT register
        const uint32_t SEC_CNT_ADDR = 0xf0801068;
        uint32_t memResult = 0;
        if (readMem(SEC_CNT_ADDR, memResult))
        {
            btm.powerOnSecCounterTime = static_cast<uint64_t>(memResult);
        }
        else
        {
            log<level::ERR>("Read memory SEC_CNT_ADDR(0xf0801068) failed");
            return false;
        }
    }

    return true;
}

} // namespace metric_blob
