<commit_before>// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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 <dlfcn.h>

#include <glog/logging.h>

#include <nvidia/gdk/nvml.h>

#include <map>
#include <string>

#include <process/once.hpp>

#include <stout/error.hpp>
#include <stout/exit.hpp>
#include <stout/nothing.hpp>
#include <stout/option.hpp>
#include <stout/stringify.hpp>
#include <stout/try.hpp>

#include <stout/posix/dynamiclibrary.hpp>

#include "logging/logging.hpp"

#include "slave/containerizer/mesos/isolators/gpu/nvml.hpp"

using process::Once;

using std::map;
using std::string;

namespace nvml {

constexpr char LIBRARY_NAME[] = "libnvidia-ml.so.1";


struct NvidiaManagementLibrary
{
  NvidiaManagementLibrary(
      nvmlReturn_t (*_deviceGetCount)(unsigned int*),
      nvmlReturn_t (*_deviceGetHandleByIndex)(unsigned int, nvmlDevice_t*),
      nvmlReturn_t (*_deviceGetMinorNumber)(nvmlDevice_t, unsigned int*),
      const char* (*_errorString)(nvmlReturn_t))
    : deviceGetCount(_deviceGetCount),
      deviceGetHandleByIndex(_deviceGetHandleByIndex),
      deviceGetMinorNumber(_deviceGetMinorNumber),
      errorString(_errorString) {}

  nvmlReturn_t (*deviceGetCount)(unsigned int*);
  nvmlReturn_t (*deviceGetHandleByIndex)(unsigned int, nvmlDevice_t*);
  nvmlReturn_t (*deviceGetMinorNumber)(nvmlDevice_t, unsigned int*);
  const char* (*errorString)(nvmlReturn_t);
};


const NvidiaManagementLibrary* nvml = nullptr;


Once* initialized = new Once();
Option<Error>* error = new Option<Error>();
DynamicLibrary* library = new DynamicLibrary();


Try<Nothing> initialize()
{
  if (initialized->once()) {
    if (error->isSome()) {
      return error->get();
    }
    return Nothing();
  }

  Try<Nothing> open = library->open(LIBRARY_NAME);
  if (open.isError()) {
    *error = Error("Failed to open '" + stringify(LIBRARY_NAME) + "': " +
                   open.error());
    initialized->done();
    return error->get();
  }

  // Load the needed symbols.
  //<commit_msg>Changed all global variable declarations in nvml.cpp to static.<commit_after>// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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 <dlfcn.h>

#include <glog/logging.h>

#include <nvidia/gdk/nvml.h>

#include <map>
#include <string>

#include <process/once.hpp>

#include <stout/error.hpp>
#include <stout/exit.hpp>
#include <stout/nothing.hpp>
#include <stout/option.hpp>
#include <stout/stringify.hpp>
#include <stout/try.hpp>

#include <stout/posix/dynamiclibrary.hpp>

#include "logging/logging.hpp"

#include "slave/containerizer/mesos/isolators/gpu/nvml.hpp"

using process::Once;

using std::map;
using std::string;

namespace nvml {

static constexpr char LIBRARY_NAME[] = "libnvidia-ml.so.1";


struct NvidiaManagementLibrary
{
  NvidiaManagementLibrary(
      nvmlReturn_t (*_deviceGetCount)(unsigned int*),
      nvmlReturn_t (*_deviceGetHandleByIndex)(unsigned int, nvmlDevice_t*),
      nvmlReturn_t (*_deviceGetMinorNumber)(nvmlDevice_t, unsigned int*),
      const char* (*_errorString)(nvmlReturn_t))
    : deviceGetCount(_deviceGetCount),
      deviceGetHandleByIndex(_deviceGetHandleByIndex),
      deviceGetMinorNumber(_deviceGetMinorNumber),
      errorString(_errorString) {}

  nvmlReturn_t (*deviceGetCount)(unsigned int*);
  nvmlReturn_t (*deviceGetHandleByIndex)(unsigned int, nvmlDevice_t*);
  nvmlReturn_t (*deviceGetMinorNumber)(nvmlDevice_t, unsigned int*);
  const char* (*errorString)(nvmlReturn_t);
};


static const NvidiaManagementLibrary* nvml = nullptr;


static Once* initialized = new Once();
static Option<Error>* error = new Option<Error>();
static DynamicLibrary* library = new DynamicLibrary();


Try<Nothing> initialize()
{
  if (initialized->once()) {
    if (error->isSome()) {
      return error->get();
    }
    return Nothing();
  }

  Try<Nothing> open = library->open(LIBRARY_NAME);
  if (open.isError()) {
    *error = Error("Failed to open '" + stringify(LIBRARY_NAME) + "': " +
                   open.error());
    initialized->done();
    return error->get();
  }

  // Load the needed symbols.
  //
{'fraction_non_alphanumeric': 0.1214530995843123, 'fraction_numerical': 0.0018073377914332189, 'mean_word_length': 4.2906309751434035, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3430170', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1655, 'n_words': 481}