File: MLUtils.cpp

package info (click to toggle)
firefox 134.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,345,684 kB
  • sloc: cpp: 7,244,582; javascript: 6,236,669; ansic: 3,654,775; python: 1,359,774; xml: 618,542; asm: 426,944; java: 183,315; sh: 66,206; makefile: 19,398; perl: 13,009; objc: 12,453; yacc: 4,583; cs: 3,846; pascal: 2,989; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (102 lines) | stat: -rw-r--r-- 3,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MLUtils.h"

#include "prsystem.h"
#include "mozilla/Casting.h"
#include <sys/types.h>

#if defined(XP_WIN)
#  include <sysinfoapi.h>
#endif
#if defined(XP_MACOSX)
#  include <sys/sysctl.h>
#  include <mach/mach.h>
#endif
#if defined(XP_LINUX)
#  include <sys/sysinfo.h>
#endif

namespace mozilla::ml {

NS_IMPL_ISUPPORTS(MLUtils, nsIMLUtils)

/**
 * See nsIMLUtils for the method documentation.
 */
NS_IMETHODIMP MLUtils::HasEnoughMemoryToInfer(uint64_t aModelSizeInMemory,
                                              uint32_t aThresholdPercentage,
                                              uint64_t aMinMemoryRequirement,
                                              bool* _retval) {
  // Check for the physical memory. On devices with less than
  // `aMinMemoryRequirement`, we give up.
  uint64_t totalMemory = PR_GetPhysicalMemorySize();

  if (totalMemory <= aMinMemoryRequirement) {
    // We are not doing inference if the device has less than what is required.
    *_retval = false;
    return NS_OK;
  }

  // Ensure threshold is valid and within 0-100 range
  if (aThresholdPercentage <= 0 || aThresholdPercentage > 100) {
    *_retval = false;
    return NS_ERROR_FAILURE;  // throw an error
  }

  // Convert the threshold percentage to a usable value (e.g., 80% becomes 0.8)
  double threshold = static_cast<double>(aThresholdPercentage) / 100.0f;

  // Determin the available resident memory on different platforms.
  uint64_t availableResidentMemory = 0;
#if defined(XP_WIN)
  MEMORYSTATUSEX memStatus = {sizeof(memStatus)};

  if (GlobalMemoryStatusEx(&memStatus)) {
    availableResidentMemory = memStatus.ullAvailPhys;
  } else {
    return NS_ERROR_FAILURE;
  }
#endif

#if defined(XP_MACOSX)
  mach_port_t host_port = mach_host_self();
  vm_size_t page_size;
  vm_statistics64_data_t vm_stats;
  mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;

  if (host_page_size(host_port, &page_size) != KERN_SUCCESS ||
      host_statistics64(host_port, HOST_VM_INFO64,
                        reinterpret_cast<host_info64_t>(&vm_stats),
                        &count) != KERN_SUCCESS) {
    *_retval = false;
    return NS_ERROR_FAILURE;
  }

  availableResidentMemory =
      static_cast<uint64_t>(vm_stats.free_count + vm_stats.inactive_count) *
      page_size;
#endif

#if defined(XP_LINUX)
  struct sysinfo memInfo {
    0
  };
  if (sysinfo(&memInfo) != 0) {
    *_retval = false;
    return NS_ERROR_FAILURE;
  }
  availableResidentMemory = memInfo.freeram * memInfo.mem_unit;
#endif

  // Check if the modelSize fits within memory using the threshold
  *_retval = AssertedCast<double>(aModelSizeInMemory) <=
             AssertedCast<double>(availableResidentMemory) * threshold;
  return NS_OK;
}

}  // namespace mozilla::ml