Repository: matszpk/amdcovc Branch: master Commit: 50afb3178de8 Files: 6 Total size: 154.2 KB Directory structure: gitextract_4ny57t0x/ ├── .gitignore ├── CONTRIBUTORS ├── COPYING ├── Makefile ├── README.md └── amdcovc.cpp ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *~ *.o amdcovc ================================================ FILE: CONTRIBUTORS ================================================ CONTRIBUTORS list: ----------------------------------------- Rian Hunter - rianhunter - fixes and buffer overflow fix Tit Petric - titpetric - fix ADL SDK include's paths Jan Ziak - atomsymbol - report issue with ncurses on some Linux distro and fix proposal ================================================ FILE: COPYING ================================================ GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. ================================================ FILE: Makefile ================================================ ### # Makefile # Mateusz Szpakowski ### # 1 - if you need AMD Catalyst support, 0 - if you won't HAVE_ADLSDK = 0 HAVE_TERMINFO = 1 CXX = g++ CXXFLAGS = -Wall -O3 -std=c++11 LDFLAGS = -Wall -O3 -std=c++11 ifeq ($(HAVE_TERMINFO),1) TINFOLIB = -lncurses -ltinfo TINFODEFINES = -DHAVE_TERMINFO=1 else TINFOLIB = TINFODEFINES = endif ifeq ($(HAVE_ADLSDK),1) ADLSDKDIR = /home/mat/docs/src/ADL_SDK9 INCDIRS = -I$(ADLSDKDIR)/include DEFINES = -DHAVE_ADLSDK=1 $(TINFODEFINES) # for AMDGPU-PRO SLES/OpenSUSE LIBDIRS = -L/opt/amdgpu-pro/lib64 # for AMDGPU-PRO Ubuntu #LIBDIRS = -L/opt/amdgpu-pro/lib/x86_64-linux-gnu #LIBDIRS = LIBS = -ldl -lpci -lm $(TINFOLIB) -lOpenCL -pthread else LIBS = -ldl -lpci -lm $(TINFOLIB) -pthread LIBDIRS = INCDIRS = DEFINES = $(TINFODEFINES) endif .PHONY: all clean all: amdcovc amdcovc: amdcovc.o $(CXX) $(LDFLAGS) $(LIBDIRS) -o $@ $^ $(LIBS) %.o: %.cpp $(CXX) $(CXXFLAGS) $(INCDIRS) $(DEFINES) -c -o $@ $< clean: rm -f *.o amdcovc ================================================ FILE: README.md ================================================ ## AMDCOVC This is program to control AMD Overdrive settings that is working if graphics cards are in console-mode. This program has been written to replace `amdconfig` utility and allow to overclock Radeon GPU's if no running X11 server. An `amdconfig` utility requires a running X11 server and X11 kept configuration to be working correctly. Thus, this program ease overclocking of Radeon GPU's under console environment (in SSH or Linux console). Nevertheless, this program can works under X11 environment. ### WARNING This program changes graphics card's clocks and voltages. This program can easily DAMAGE your hardware if it will be used CARELESSLY. Before any setting of AMD Overdrive parameters (clocks, voltages, fan speeds), please STOP ANY the GPU computations and the GPU renderings. YOU USE THIS PROGRAM AT YOUR OWN RISK! WE DO NOT GIVE ANY WARRANTY THAT PROGRAM WILL BE WORKING CORRECTLY IN ANY CASE! Read license before using this program. ### DONATION If you want to donate this project please send Bitcoins to following address: 3M6z45UGXgimFs3QGQgbmqVZzK9s3RnfLL ### Preliminary requirements Program to work requires following things: * C++ environment compliant with C++11 standard (libraries) * pciutils library (libpci). Optional components that can be used by program for AMD Catalyst drivers: * OpenCL environment (to force initializing of devices) * libadlxx.so library (AMD ADL library) * ncurses library to coloring and bolding terminal outputs To build program you need: * compiler compliant with C++11 standard * OpenCL headers (for AMD Catalyst drivers) * AMD ADL SDK (in developer.amd.com site) (for AMD Catalyst drivers) * pciutils developer package (includes) ### Building program If you want to use amdovc with AMD Catalyst drivers, you should enable ADLSDK by replacing line in Makefile: ``` HAVE_ADLSDK = 0 ``` to ``` HAVE_ADLSDK = 1 ``` and you should set correct place of the ADLSDK in Makefile in line: ``` ADLSDKDIR = /home/mat/docs/src/ADL_SDK9 ``` by replacing `/home/mat/docs/src/ADL_SDK9` by your correct path. You can also disable or enable terminal output coloring and bolding by disabling TERMINFO in Makefile by setting HAVE_TERMINFO variable: ``` HAVE_TERMINFO = 0 ``` Now. To build program, just type: ``` make ``` ### Invoking program NOTE for AMD Crimson/Catalyst drivers): If no X11 server is running, then this program requires root privileges. NOTE for AMDGPU(-PRO) drivers: Any parameter settings requires root privileges. To run program, just type: ``` ./amdcovc ``` This command prints current status of all adapters (graphics cards). ``` ./amdcovc -v ``` Prints more informations about graphics cards. To print help, type: ``` ./amdcovc -? ``` To set core clock and memory clock for first adapter, just type: ``` ./amdcovc coreclk:0=1000 memclk:0=1200 ``` Sets core clock to 1000 MHz and memory clock to 1200 MHz. ### Understanding info printed by program The AMDCOVC by default prints following informations about graphics card: ``` Adapter 1: PCI 1:0:0: Pitcairn PRO [Radeon HD 7850 / R7 265 / R9 270 1024SP] Core: 1010 MHz, Mem: 1235 MHz, Vddc: 1.21 V, Load: 98%, Temp: 67 C, Fan: 54% Max Ranges: Core: 150 - 1050 MHz, Mem: 75 - 1450 MHz, Vddc: 0.8 - 1.225 V PerfLevels: Core: 300 - 1010 MHz, Mem: 150 - 1235 MHz, Vddc: 0.825 - 1.21 V ``` The first line below adapter is current state of graphics card (core clock, memory clock, voltage, load, temperature and fan speed). The new version add additonal info about device topology (for example `PCI 3:0:0`). The `PerfLevels` is current performance level settings from lowest to highest. Ofcourse, the highest performance levels is used while some computations/rendering. First level will be used in idle mode (if no work). The `Max Ranges` is minimal and maximal possible settings for graphics card. For AMDGPU mode, the AMDCOVC prints: ``` Adapter 0: PCI 1:0:0: Ellesmere [Radeon RX 470/480/570/570X/580/580X/590] Core: 1077 MHz, Mem: 1750 MHz, CoreOD: 0, MemOD: 0, Vddc: 1000 mV PerfCtrl: manual, Load: 100%, MemLoad: 23% Temp: 58°C, T4: 60°C, Fan: 41.9608% Power: 54.008 W (cap: 127 W) Core Clocks: 300 608 910 1077 1145 1191 1236 1290 Memory Clocks: 300 1750 ``` Likely as in AMD Catalyst mode, an AMDCOVC prints core and memory clocks, but also prints core and memory overdrive factors (0-20). The `PerfCtrl` is current performance control (can be low, manual or high). The line `Core Clocks` contains all possible clocks for core. The line `Memory Clocks` contains all possible clocks for memory. The verbose informations contains: * current state of graphics (Current CoreClock,...) * minimal and maximal fan speed settings * minimal and maximal clocks * minimal and maximal voltages (only AMD Catalyst mode) * current performance level settings (only AMD Catalyst mode) * default performance level settings (only AMD Catalyst mode) also, in AMDGPU mode: * list of possible core clocks * list of possible memory clocks ### List of parameters List of parameters that can be set: * coreclk[:[ADAPTERS][:LEVEL]]=CLOCK - set core clock in MHz * memclk[:[ADAPTERS][:LEVEL]]=CLOCK - set memory clock in MHz * ccoreclk[:[ADAPTERS][:LEVEL]]=CLOCK - set current core clock in MHz (AMDGPU) * cmemclk[:[ADAPTERS][:LEVEL]]=CLOCK - set current memory clock in MHz (AMDGPU) * csocclk[:[ADAPTERS][:LEVEL]]=CLOCK - set current SOC clock in MHz (AMDGPU) (vega) * cdcefclk[:[ADAPTERS][:LEVEL]]=CLOCK - set current DCEF clock in MHz (AMDGPU) (vega) * cfclk[:[ADAPTERS][:LEVEL]]=CLOCK - set current F clock in MHz (AMDGPU) (vega2) * coreod[:[ADAPTERS][:LEVEL]]=PERCENT - set core clock in percents (AMDGPU) * memod[:[ADAPTERS][:LEVEL]]=PERCENT - set memory clock in perecents (AMDGPU) * corepl[:[ADAPTERS][:LEVEL]]=LEVEL - set current core performance level (AMDGPU) * mempl[:[ADAPTERS][:LEVEL]]=LEVEL - set current memory performance level (AMDGPU) * vcore[:[ADAPTERS][:LEVEL]]=VOLTAGE - set Vddc voltage in Volts * icoreclk[:ADAPTERS]=CLOCK - set core clock in MHz for idle level * imemclk[:ADAPTERS]=CLOCK - set memory clock in MHz for idle level * ivcore[:ADAPTERS]=VOLTAGE - set Vddc voltage in Volts for idle level * fanspeed[:[ADAPTERS][:THID]]=PERCENT - set fanspeed in percents Extra specifiers in parameters: * ADAPTERS - adapter (device) index list (default is 0) or `all` for all adapters * LEVEL - performance level (typically 0 or 1, default is last) * THID - thermal controller index (must be 0) The adapter's list can be a single number (0), a single range (0-1) or list of numbers and ranges, or `all` specifier (choose all adapters). You can use 'default' in value place to set default value. For fanspeed 'default' value force automatic speed setup. To overclock graphic card on AMDGPU(-PRO) driver, can you use `coreod` and `memod` parameters. Parameters `coreclk` and `memclk` is available in AMDGPU(-PRO) driver mode. AMDCOVC under AMDGPU(-PRO) the `coreclk` and `memclk` paramters sets GPU core overdrive and a memory overdirve parameters which determines overclocking in percent. These paramaters does not impact on current GPU and memory clocks, but these paramters impact on their maximal values of clocks. To underclock core and memory you should use `ccoreclk` and `cmemclk` parameters or `corepl` and `mempl` parameters. These parameters set current core and memory performance level. The value default sets default automatic performance control. ### List of options List of options: * -a, --adapters=LIST - print informations only for these adapters * -v, --verbose - print verbose informations * -w, --watch=SECONDS - update status on terminal for every SECONDS - replaces command 'watch' * --version - print version * -?, --help - print help ### Text styling and coloring By default AMDCOVC uses text coloring and styling (bolding) if the terminal/console able to display colors or text styles and if output is not the standard output/error. Following environment variables control whether coloring or text styling will be used: * AMDCOVC_NOCOLOR - disables coloring * AMDCOVC_NOBOLD - disables text bolding Set value of an environment variable to '1','on','y','true','t', 'on' or 'enable' to enable its function. For example just try by running AMDCOVC in following way: ``` AMDCOVC_NOCOLOR=1 ./amdcovc ``` ================================================ FILE: amdcovc.cpp ================================================ /* * AMDCOVC - AMD Console OVerdrive control utility * Copyright (C) 2016 Mateusz Szpakowski * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _DEFAULT_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_ADLSDK #include #endif extern "C" { #include } #ifdef HAVE_TERMINFO #include #endif #ifdef __linux__ #define LINUX 1 #endif #ifdef HAVE_ADLSDK #include #endif #define AMDCOVC_VERSION "0.4.1.2" enum TermColor { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }; enum SaneColor { GOOD = TermColor::GREEN, WARNING = TermColor::YELLOW, BAD = TermColor::RED }; #ifdef HAVE_TERMINFO static const char* tinfoClrScr = nullptr; // clear screen static const char* tinfoSetAF = nullptr; // set a foreground static const char* tinfoSetF = nullptr; // set foreground static const char* tinfoSgr0 = nullptr; // exit to std mode static const char* tinfoBold = nullptr; // bold mode static const char* tinfoOrigColors = nullptr; static const char* tinfoOrigPairs = nullptr; static bool tinfoNoColor = false; static bool tinfoNoBold = false; static void setTermNormal(); static const char* envTrueStrings[] = { "1", "y", "yes", "true", "t", "on", "enable" }; static bool parseBoolEnv(const char* strValue) { if (strValue == nullptr) return false; while(*strValue == ' ') strValue++; if (*strValue==0) return false; const char* strEnd = strValue; while(*strEnd != ' ' && *strEnd != 0) strEnd++; for (const char* trueStr: envTrueStrings) if (strncasecmp(trueStr, strValue, strEnd-strValue)==0) return true; return false; } static void initializeTermModes() { tinfoNoColor = parseBoolEnv(getenv("AMDCOVC_NOCOLOR")); tinfoNoBold = parseBoolEnv(getenv("AMDCOVC_NOBOLD")); if (!isatty(1)) return; // if no terminal // setup terminal int err; if (setupterm(nullptr, 1, &err) != 0) return; // get tparams tinfoClrScr = tigetstr("clear"); tinfoSetAF = tigetstr("setaf"); tinfoSetF = tigetstr("setf"); tinfoBold = tigetstr("bold"); tinfoSgr0 = tigetstr("sgr0"); tinfoOrigColors = tigetstr("oc"); tinfoOrigPairs = tigetstr("op"); // initialize to standard terminal mode setTermNormal(); } static void flushTermOutputs() { std::cout.flush(); std::cerr.flush(); } static void clearScreen() { if (tinfoClrScr != nullptr) { flushTermOutputs(); putp(tinfoClrScr); } } static void setTermForeground(int color) { if (tinfoNoColor) return; if (tinfoSetAF != nullptr) { const char* p = tiparm(tinfoSetAF, color); flushTermOutputs(); putp(p); } else if (tinfoSetF != nullptr) { const char* p = tiparm(tinfoSetF, (color&2) | ((color&4)>>2) | ((color&1)<<2)); flushTermOutputs(); putp(p); } // do nothing if other } static void setTermBold() { if (tinfoNoBold) return; if (tinfoBold != nullptr) { flushTermOutputs(); putp(tinfoBold); } } static void setTermStdForeground() { if (tinfoOrigColors != nullptr || tinfoOrigPairs != nullptr) flushTermOutputs(); if (tinfoOrigColors != nullptr) putp(tinfoOrigColors); if (tinfoOrigPairs != nullptr) putp(tinfoOrigPairs); } static void setTermNormal() { if (tinfoSgr0 != nullptr) { flushTermOutputs(); putp(tinfoSgr0); } } #else // if no terminal support static void initializeTermModes() { } static void clearScreen() { } static void setTermForeground(int color) { } static void setTermBold() { } static void setTermStdForeground() { } static void setTermNormal() { } #endif static bool terminalUTF8 = false; static void beforePrintWatch(int watch) { clearScreen(); time_t t; time(&t); std::cout << "Watch every " << watch << " seconds. Time: " << ctime(&t) << "\n"; } static void printTemperature(double temp) { if (temp < 75.0) setTermForeground(SaneColor::GOOD); else if (temp < 90.0) setTermForeground(SaneColor::WARNING); else setTermForeground(SaneColor::BAD); setTermBold(); if (terminalUTF8) std::cout << temp << "°C"; else std::cout << temp << "*C"; setTermStdForeground(); setTermNormal(); } static void printFanSpeed(double fanspeed) { if (fanspeed < 60.0) setTermForeground(SaneColor::GOOD); else if (fanspeed < 80.0) setTermForeground(SaneColor::WARNING); else setTermForeground(SaneColor::BAD); setTermBold(); std::cout << fanspeed << "%"; setTermStdForeground(); setTermNormal(); } #ifdef HAVE_ADLSDK // Memory allocation function void* __stdcall ADL_Main_Memory_Alloc (int iSize) { void* lpBuffer = malloc (iSize); return lpBuffer; } // Optional Memory de-allocation function void __stdcall ADL_Main_Memory_Free (void** lpBuffer) { if (nullptr != *lpBuffer) { free (*lpBuffer); *lpBuffer = nullptr; } } #endif class Error: public std::exception { private: std::string description; public: explicit Error(const char* _description) : description(_description) { } Error(int error, const char* _description) { char errorBuf[32]; snprintf(errorBuf, 32, "code %d: ", error); description = errorBuf; description += _description; } virtual ~Error() noexcept { } const char* what() const noexcept { return description.c_str(); } }; #ifdef HAVE_ADLSDK class ATIADLHandle { private: typedef int (*ADL_ConsoleMode_FileDescriptor_Set_T)(int fileDescriptor); typedef int (*ADL_Main_Control_Create_T)(ADL_MAIN_MALLOC_CALLBACK, int); typedef int (*ADL_Main_Control_Destroy_T)(); typedef int (*ADL_Adapter_NumberOfAdapters_Get_T)(int* numAdapters); typedef int (*ADL_Adapter_Active_Get_T)(int adapterIndex, int *status); typedef int (*ADL_Adapter_AdapterInfo_Get_T)(LPAdapterInfo info, int inputSize); typedef int (*ADL_Overdrive5_CurrentActivity_Get_T)(int adapterIndex, ADLPMActivity* activity); typedef int (*ADL_Overdrive5_Temperature_Get_T)(int adapterIndex, int thermalCtrlIndex, ADLTemperature *temperature); typedef int (*ADL_Overdrive5_FanSpeedInfo_Get_T)(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedInfo* fanSpeedInfo); typedef int (*ADL_Overdrive5_FanSpeed_Get_T)(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedValue* fanSpeedValue); typedef int (*ADL_Overdrive5_ODParameters_Get_T)(int adapterIndex, ADLODParameters* odParameters); typedef int (*ADL_Overdrive5_ODPerformanceLevels_Get_T)(int adapterIndex, int idefault, ADLODPerformanceLevels* odPerformanceLevels); typedef int (*ADL_Overdrive5_FanSpeed_Set_T)(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedValue* fanSpeedValue); typedef int (*ADL_Overdrive5_FanSpeedToDefault_Set_T)(int adapterIndex, int thermalCtrlIndex); typedef int (*ADL_Overdrive5_ODPerformanceLevels_Set_T)(int adapterIndex, ADLODPerformanceLevels* odPerformanceLevels); void* handle; void* getSym(const char* name); ADL_Main_Control_Create_T pADL_Main_Control_Create; ADL_Main_Control_Destroy_T pADL_Main_Control_Destroy; ADL_ConsoleMode_FileDescriptor_Set_T pADL_ConsoleMode_FileDescriptor_Set; ADL_Adapter_NumberOfAdapters_Get_T pADL_Adapter_NumberOfAdapters_Get; ADL_Adapter_Active_Get_T pADL_Adapter_Active_Get; ADL_Adapter_AdapterInfo_Get_T pADL_Adapter_AdapterInfo_Get; ADL_Overdrive5_CurrentActivity_Get_T pADL_Overdrive5_CurrentActivity_Get; ADL_Overdrive5_Temperature_Get_T pADL_Overdrive5_Temperature_Get; ADL_Overdrive5_FanSpeedInfo_Get_T pADL_Overdrive5_FanSpeedInfo_Get; ADL_Overdrive5_FanSpeed_Get_T pADL_Overdrive5_FanSpeed_Get; ADL_Overdrive5_ODParameters_Get_T pADL_Overdrive5_ODParameters_Get; ADL_Overdrive5_ODPerformanceLevels_Get_T pADL_Overdrive5_ODPerformanceLevels_Get; ADL_Overdrive5_FanSpeed_Set_T pADL_Overdrive5_FanSpeed_Set; ADL_Overdrive5_FanSpeedToDefault_Set_T pADL_Overdrive5_FanSpeedToDefault_Set; ADL_Overdrive5_ODPerformanceLevels_Set_T pADL_Overdrive5_ODPerformanceLevels_Set; public: ATIADLHandle(); bool open(); ~ATIADLHandle(); void Main_Control_Create(ADL_MAIN_MALLOC_CALLBACK callback, int iEnumConnectedAdapters) const; void Main_Control_Destroy() const; void ConsoleMode_FileDescriptor_Set(int fileDescriptor) const; void Adapter_NumberOfAdapters_Get(int* number) const; void Adapter_Active_Get(int adapterIndex, int* status) const; void Adapter_Info_Get(LPAdapterInfo info, int inputSize) const; void Overdrive5_CurrentActivity_Get(int adapterIndex, ADLPMActivity* activity) const; void Overdrive5_Temperature_Get(int adapterIndex, int thermalCtrlIndex, ADLTemperature* temperature) const; void Overdrive5_FanSpeedInfo_Get(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedInfo* fanSpeedInfo) const; void Overdrive5_FanSpeed_Get(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedValue* fanSpeedValue) const; void Overdrive5_ODParameters_Get(int adapterIndex, ADLODParameters* odParameters) const; void Overdrive5_ODPerformanceLevels_Get(int adapterIndex, int idefault, ADLODPerformanceLevels* odPerformanceLevels) const; void Overdrive5_FanSpeed_Set(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedValue* fanSpeedValue) const; void Overdrive5_FanSpeedToDefault_Set(int adapterIndex, int thermalCtrlIndex) const; void Overdrive5_ODPerformanceLevels_Set(int adapterIndex, ADLODPerformanceLevels* odPerformanceLevels) const; }; ATIADLHandle::ATIADLHandle() : handle(nullptr), pADL_Main_Control_Create(nullptr), pADL_Main_Control_Destroy(nullptr), pADL_ConsoleMode_FileDescriptor_Set(nullptr), pADL_Adapter_NumberOfAdapters_Get(nullptr), pADL_Adapter_Active_Get(nullptr), pADL_Adapter_AdapterInfo_Get(nullptr), pADL_Overdrive5_CurrentActivity_Get(nullptr), pADL_Overdrive5_Temperature_Get(nullptr), pADL_Overdrive5_FanSpeedInfo_Get(nullptr), pADL_Overdrive5_FanSpeed_Get(nullptr), pADL_Overdrive5_ODParameters_Get(nullptr), pADL_Overdrive5_ODPerformanceLevels_Get(nullptr), pADL_Overdrive5_FanSpeed_Set(nullptr), pADL_Overdrive5_FanSpeedToDefault_Set(nullptr), pADL_Overdrive5_ODPerformanceLevels_Set(nullptr) { } bool ATIADLHandle::open() try { dlerror(); // clear old errors handle = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL); if (handle == nullptr) return false; pADL_Main_Control_Create = (ADL_Main_Control_Create_T) getSym("ADL_Main_Control_Create"); pADL_Main_Control_Destroy = (ADL_Main_Control_Destroy_T) getSym("ADL_Main_Control_Destroy"); pADL_ConsoleMode_FileDescriptor_Set = (ADL_ConsoleMode_FileDescriptor_Set_T) getSym("ADL_ConsoleMode_FileDescriptor_Set"); pADL_Adapter_NumberOfAdapters_Get = (ADL_Adapter_NumberOfAdapters_Get_T) getSym("ADL_Adapter_NumberOfAdapters_Get"); pADL_Adapter_Active_Get = (ADL_Adapter_Active_Get_T) getSym("ADL_Adapter_Active_Get"); pADL_Adapter_AdapterInfo_Get = (ADL_Adapter_AdapterInfo_Get_T) getSym("ADL_Adapter_AdapterInfo_Get"); pADL_Overdrive5_CurrentActivity_Get = (ADL_Overdrive5_CurrentActivity_Get_T) getSym("ADL_Overdrive5_CurrentActivity_Get"); pADL_Overdrive5_Temperature_Get = (ADL_Overdrive5_Temperature_Get_T) getSym("ADL_Overdrive5_Temperature_Get"); pADL_Overdrive5_FanSpeedInfo_Get = (ADL_Overdrive5_FanSpeedInfo_Get_T) getSym("ADL_Overdrive5_FanSpeedInfo_Get"); pADL_Overdrive5_FanSpeed_Get = (ADL_Overdrive5_FanSpeed_Get_T) getSym("ADL_Overdrive5_FanSpeed_Get"); pADL_Overdrive5_ODParameters_Get = (ADL_Overdrive5_ODParameters_Get_T) getSym("ADL_Overdrive5_ODParameters_Get"); pADL_Overdrive5_ODPerformanceLevels_Get = (ADL_Overdrive5_ODPerformanceLevels_Get_T) getSym("ADL_Overdrive5_ODPerformanceLevels_Get"); pADL_Overdrive5_FanSpeed_Set = (ADL_Overdrive5_FanSpeed_Set_T) getSym("ADL_Overdrive5_FanSpeed_Set"); pADL_Overdrive5_FanSpeedToDefault_Set = (ADL_Overdrive5_FanSpeedToDefault_Set_T) getSym("ADL_Overdrive5_FanSpeedToDefault_Set"); pADL_Overdrive5_ODPerformanceLevels_Set = (ADL_Overdrive5_ODPerformanceLevels_Set_T) getSym("ADL_Overdrive5_ODPerformanceLevels_Set"); return true; } catch(...) { if (handle != nullptr) { dlerror(); // clear old errors if (dlclose(handle)) // if closing failed { handle = nullptr; throw Error(dlerror()); } handle = nullptr; } throw; } ATIADLHandle::~ATIADLHandle() { if (handle != nullptr) { dlerror(); if (dlclose(handle)) { handle = nullptr; throw Error(dlerror()); } handle = nullptr; } } void* ATIADLHandle::getSym(const char* symbolName) { void* symbol = nullptr; dlerror(); // clear old errors symbol = dlsym(handle, symbolName); const char* error = dlerror(); if (symbol == nullptr && error != nullptr) throw Error(error); return symbol; } void ATIADLHandle::Main_Control_Create(ADL_MAIN_MALLOC_CALLBACK callback, int iEnumConnectedAdapters) const { int error = pADL_Main_Control_Create(callback, iEnumConnectedAdapters); if (error != ADL_OK) throw Error(error, "ADL_Main_Control_Create error"); } void ATIADLHandle::Main_Control_Destroy() const { int error = pADL_Main_Control_Destroy(); if (error != ADL_OK) throw Error(error, "ADL_Main_Control_Destroy error"); } void ATIADLHandle::ConsoleMode_FileDescriptor_Set(int fileDescriptor) const { int error = pADL_ConsoleMode_FileDescriptor_Set(fileDescriptor); if (error != ADL_OK) throw Error(error, "ADL_ConsoleMode_FileDescriptor_Set error"); } void ATIADLHandle::Adapter_NumberOfAdapters_Get(int* number) const { int error = pADL_Adapter_NumberOfAdapters_Get(number); if (error != ADL_OK) throw Error(error, "ADL_Adapter_NumberOfAdapters_Get error"); } void ATIADLHandle::Adapter_Active_Get(int adapterIndex, int* status) const { int error = pADL_Adapter_Active_Get(adapterIndex, status); if (error != ADL_OK) throw Error(error, "ADL_Adapter_Active_Get error"); } void ATIADLHandle::Adapter_Info_Get(LPAdapterInfo info, int inputSize) const { int error = pADL_Adapter_AdapterInfo_Get(info, inputSize); if (error != ADL_OK) throw Error(error, "ADL_AdapterInfo_Get error"); } void ATIADLHandle::Overdrive5_CurrentActivity_Get(int adapterIndex, ADLPMActivity* activity) const { int error = pADL_Overdrive5_CurrentActivity_Get(adapterIndex, activity); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_CurrentActivity_Get error"); } void ATIADLHandle::Overdrive5_Temperature_Get(int adapterIndex, int thermalCtrlIndex, ADLTemperature *temperature) const { int error = pADL_Overdrive5_Temperature_Get(adapterIndex, thermalCtrlIndex, temperature); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_Temperature_Get error"); } void ATIADLHandle::Overdrive5_FanSpeedInfo_Get(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedInfo* fanSpeedInfo) const { int error = pADL_Overdrive5_FanSpeedInfo_Get(adapterIndex, thermalCtrlIndex, fanSpeedInfo); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_FanSpeedInfo_Get error"); } void ATIADLHandle::Overdrive5_FanSpeed_Get(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedValue* fanSpeedValue) const { int error = pADL_Overdrive5_FanSpeed_Get(adapterIndex, thermalCtrlIndex, fanSpeedValue); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_FanSpeed_Get error"); } void ATIADLHandle::Overdrive5_ODParameters_Get(int adapterIndex, ADLODParameters* odParameters) const { int error = pADL_Overdrive5_ODParameters_Get(adapterIndex, odParameters); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_ODParameters_Get error"); } void ATIADLHandle::Overdrive5_ODPerformanceLevels_Get(int adapterIndex, int idefault, ADLODPerformanceLevels* odPerformanceLevels) const { int error = pADL_Overdrive5_ODPerformanceLevels_Get(adapterIndex, idefault, odPerformanceLevels); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_ODPerformanceLevels_Get error"); } void ATIADLHandle::Overdrive5_FanSpeed_Set(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedValue* fanSpeedValue) const { int error = pADL_Overdrive5_FanSpeed_Set(adapterIndex, thermalCtrlIndex, fanSpeedValue); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_FanSpeed_Set error"); } void ATIADLHandle::Overdrive5_FanSpeedToDefault_Set(int adapterIndex, int thermalCtrlIndex) const { int error = pADL_Overdrive5_FanSpeedToDefault_Set(adapterIndex, thermalCtrlIndex); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_FanSpeedToDefault_Set error"); } void ATIADLHandle::Overdrive5_ODPerformanceLevels_Set(int adapterIndex, ADLODPerformanceLevels* odPerformanceLevels) const { int error = pADL_Overdrive5_ODPerformanceLevels_Set(adapterIndex, odPerformanceLevels); if (error != ADL_OK) throw Error(error, "ADL_Overdrive5_ODPerformanceLevels_Set error"); } class ADLMainControl { private: const ATIADLHandle& handle; int fd; bool mainControlCreated; bool withX; public: explicit ADLMainControl(const ATIADLHandle& handle, int devId); ~ADLMainControl(); int getAdaptersNum() const; bool isAdapterActive(int adapterIndex) const; void getAdapterInfo(AdapterInfo* infos) const; void getCurrentActivity(int adapterIndex, ADLPMActivity& activity) const; int getTemperature(int adapterIndex, int thermalCtrlIndex) const; void getFanSpeedInfo(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedInfo& info) const; int getFanSpeed(int adapterIndex, int thermalCtrlIndex) const; void getODParameters(int adapterIndex, ADLODParameters& odParameters) const; void getODPerformanceLevels(int adapterIndex, bool isDefault, int perfLevelsNum, ADLODPerformanceLevel* perfLevels) const; void setFanSpeed(int adapterIndex, int thermalCtrlIndex, int fanSpeed) const; void setFanSpeedToDefault(int adapterIndex, int thermalCtrlIndex) const; void setODPerformanceLevels(int adapterIndex, int perfLevelsNum, ADLODPerformanceLevel* perfLevels) const; }; ADLMainControl::ADLMainControl(const ATIADLHandle& _handle, int devId) try : handle(_handle), fd(-1), mainControlCreated(false), withX(true) { try { handle.Main_Control_Create(ADL_Main_Memory_Alloc, 0); } catch(const Error& error) { if (getuid()!=0) std::cout << "IMPORTANT: This program requires root privileges to be " "working correctly\nif no running X11 server." << std::endl; withX = false; char devName[64]; snprintf(devName, 64, "/dev/ati/card%u", devId); errno = 0; fd = open(devName, O_RDWR); if (fd==-1) { cl_uint platformsNum; /// force initializing these stupid devices clGetPlatformIDs(0, nullptr, &platformsNum); errno = 0; fd = open(devName, O_RDWR); if (fd==-1) throw Error(errno, "Can't open GPU device"); } handle.ConsoleMode_FileDescriptor_Set(fd); handle.Main_Control_Create(ADL_Main_Memory_Alloc, 0); } } catch(...) { if (mainControlCreated) handle.Main_Control_Destroy(); if (fd!=-1) close(fd); throw; } ADLMainControl::~ADLMainControl() { if (fd!=-1) close(fd); } int ADLMainControl::getAdaptersNum() const { int num = 0; handle.Adapter_NumberOfAdapters_Get(&num); return num; } bool ADLMainControl::isAdapterActive(int adapterIndex) const { if (!withX) return true; int status = 0; handle.Adapter_Active_Get(adapterIndex, &status); return status == ADL_TRUE; } void ADLMainControl::getAdapterInfo(AdapterInfo* infos) const { int num; handle.Adapter_NumberOfAdapters_Get(&num); for (int i = 0; i < num; i++) infos[i].iSize = sizeof(AdapterInfo); handle.Adapter_Info_Get(infos, num*sizeof(AdapterInfo)); } void ADLMainControl::getCurrentActivity(int adapterIndex, ADLPMActivity& activity) const { activity.iSize = sizeof(ADLPMActivity); handle.Overdrive5_CurrentActivity_Get(adapterIndex, &activity); } int ADLMainControl::getTemperature(int adapterIndex, int thermalCtrlIndex) const { ADLTemperature temp; temp.iSize = sizeof(ADLTemperature); handle.Overdrive5_Temperature_Get(adapterIndex, thermalCtrlIndex, &temp); return temp.iTemperature; } void ADLMainControl::getFanSpeedInfo(int adapterIndex, int thermalCtrlIndex, ADLFanSpeedInfo& info) const { info.iSize = sizeof(ADLFanSpeedInfo); handle.Overdrive5_FanSpeedInfo_Get(adapterIndex, thermalCtrlIndex, &info); } int ADLMainControl::getFanSpeed(int adapterIndex, int thermalCtrlIndex) const { ADLFanSpeedValue fanSpeedValue; fanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT; fanSpeedValue.iFlags = 0; fanSpeedValue.iSize = sizeof(ADLFanSpeedValue); handle.Overdrive5_FanSpeed_Get(adapterIndex, thermalCtrlIndex, &fanSpeedValue); return fanSpeedValue.iFanSpeed; } void ADLMainControl::getODParameters(int adapterIndex, ADLODParameters& odParameters) const { odParameters.iSize = sizeof(ADLODParameters); handle.Overdrive5_ODParameters_Get(adapterIndex, &odParameters); } void ADLMainControl::getODPerformanceLevels(int adapterIndex, bool isDefault, int perfLevelsNum, ADLODPerformanceLevel* perfLevels) const { const size_t odPLBufSize = sizeof(ADLODPerformanceLevels)+ sizeof(ADLODPerformanceLevel)*(perfLevelsNum-1); std::unique_ptr odPlBuf(new char[odPLBufSize]); ADLODPerformanceLevels* odPLevels = (ADLODPerformanceLevels*)odPlBuf.get(); odPLevels->iSize = odPLBufSize; handle.Overdrive5_ODPerformanceLevels_Get(adapterIndex, isDefault, odPLevels); std::copy(odPLevels->aLevels, odPLevels->aLevels+perfLevelsNum, perfLevels); } void ADLMainControl::setFanSpeed(int adapterIndex, int thermalCtrlIndex, int fanSpeed) const { ADLFanSpeedValue fanSpeedValue; fanSpeedValue.iSize = sizeof(ADLFanSpeedValue); fanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT; fanSpeedValue.iFanSpeed = fanSpeed; handle.Overdrive5_FanSpeed_Set(adapterIndex, thermalCtrlIndex, &fanSpeedValue); } void ADLMainControl::setFanSpeedToDefault(int adapterIndex, int thermalCtrlIndex) const { handle.Overdrive5_FanSpeedToDefault_Set(adapterIndex, thermalCtrlIndex); } void ADLMainControl::setODPerformanceLevels(int adapterIndex, int perfLevelsNum, ADLODPerformanceLevel* perfLevels) const { const size_t odPLBufSize = sizeof(ADLODPerformanceLevels)+ sizeof(ADLODPerformanceLevel)*(perfLevelsNum-1); std::unique_ptr odPlBuf(new char[odPLBufSize]); ADLODPerformanceLevels* odPLevels = (ADLODPerformanceLevels*)odPlBuf.get(); odPLevels->iSize = odPLBufSize; odPLevels->iReserved = 0; std::copy(perfLevels, perfLevels+perfLevelsNum, odPLevels->aLevels); handle.Overdrive5_ODPerformanceLevels_Set(adapterIndex, odPLevels); } #endif /* * AMD-GPU infos */ enum class AMDGPUPerfControl { AUTO = 0, LOW, MANUAL, HIGH, UNKNOWN }; struct AMDGPUAdapterInfo { unsigned int busNo; unsigned int deviceNo; unsigned int funcNo; unsigned int vendorId; unsigned int deviceId; std::string name; std::vector memoryClocks; std::vector coreClocks; std::vector socClocks; std::vector dcefClocks; std::vector fClocks; unsigned int minFanSpeed; unsigned int maxFanSpeed; bool defaultFanSpeed; unsigned int fanSpeed; unsigned int coreClock; unsigned int memoryClock; unsigned int socClock; unsigned int dcefClock; unsigned int fClock; unsigned int coreOD; unsigned int memoryOD; unsigned int voltage; AMDGPUPerfControl perfControl; unsigned int temperature; unsigned int temperature2; unsigned int temperature3; unsigned int temperature4; std::string tempLabel; std::string temp2Label; std::string temp3Label; std::string temp4Label; unsigned int tempCritical; unsigned int power; unsigned int powerCap; unsigned int busLanes; unsigned int busSpeed; int gpuLoad; int memLoad; unsigned int extraTemperatures; }; static pci_access* pciAccess = nullptr; static pci_filter pciFilter; static void pciAccessError(char* msg, ...) { va_list ap; va_start(ap, msg); vprintf(msg, ap); va_end(ap); exit(-1); } static void initializePCIAccess() { pciAccess = pci_alloc(); if (pciAccess==nullptr) throw Error("Can't allocate PCIAccess"); pciAccess->error = pciAccessError; pci_filter_init(pciAccess, &pciFilter); pci_init(pciAccess); pci_scan_bus(pciAccess); } #ifdef HAVE_ADLSDK static void getFromPCI(int deviceIndex, AdapterInfo& adapterInfo) { if (pciAccess==nullptr) initializePCIAccess(); char fnameBuf[64]; snprintf(fnameBuf, 64, "/proc/ati/%u/name", deviceIndex); std::string tmp, pciBusStr; { std::ifstream procNameIs(fnameBuf); procNameIs.exceptions(std::ios::badbit|std::ios::failbit); procNameIs >> tmp >> tmp >> pciBusStr; } unsigned int busNum, devNum, funcNum; if (pciBusStr.size() < 9) throw Error("Wrong PCI Bus string"); char* pciStrPtr = (char*)pciBusStr.data()+4; char* pciStrPtrNew; errno = 0; busNum = strtoul(pciStrPtr, &pciStrPtrNew, 10); if (errno!=0 || pciStrPtr==pciStrPtrNew) throw Error(errno, "Can't parse BusID"); pciStrPtr = pciStrPtrNew+1; errno = 0; devNum = strtoul(pciStrPtr, &pciStrPtrNew, 10); if (errno!=0 || pciStrPtr==pciStrPtrNew) throw Error(errno, "Can't parse DevID"); pciStrPtr = pciStrPtrNew+1; errno = 0; funcNum = strtoul(pciStrPtr, &pciStrPtrNew, 10); if (errno!=0 || pciStrPtr==pciStrPtrNew) throw Error(errno, "Can't parse FuncID"); pci_dev* dev = pciAccess->devices; for (; dev!=nullptr; dev=dev->next) if (dev->bus==busNum && dev->dev==devNum && dev->func==funcNum) { char deviceBuf[128]; deviceBuf[0] = 0; pci_lookup_name(pciAccess, deviceBuf, 128, PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); adapterInfo.iBusNumber = busNum; adapterInfo.iDeviceNumber = devNum; adapterInfo.iFunctionNumber = funcNum; adapterInfo.iVendorID = dev->vendor_id; strcpy(adapterInfo.strAdapterName, deviceBuf); break; } } #endif /* AMDGPU code */ static void getFromPCI_AMDGPU(const char* rlink, AMDGPUAdapterInfo& adapterInfo) { if (pciAccess==nullptr) initializePCIAccess(); unsigned int busNum, devNum, funcNum; size_t rlinkLen = strlen(rlink); if (rlinkLen < 18 || ::strncmp(rlink, "../../../", 9)!=0) throw Error("Wrong PCI Bus string"); char* pciStrPtr = (char*)rlink+9; char* pciStrPtrNew; while (isdigit(*pciStrPtr)) pciStrPtr++; if (*pciStrPtr!=':') throw Error(errno, "Can't parse PCI location"); pciStrPtr++; errno = 0; busNum = strtoul(pciStrPtr, &pciStrPtrNew, 16); if (errno!=0 || pciStrPtr==pciStrPtrNew) throw Error(errno, "Can't parse BusID"); pciStrPtr = pciStrPtrNew+1; errno = 0; devNum = strtoul(pciStrPtr, &pciStrPtrNew, 16); if (errno!=0 || pciStrPtr==pciStrPtrNew) throw Error(errno, "Can't parse DevID"); pciStrPtr = pciStrPtrNew+1; errno = 0; funcNum = strtoul(pciStrPtr, &pciStrPtrNew, 16); if (errno!=0 || pciStrPtr==pciStrPtrNew) throw Error(errno, "Can't parse FuncID"); pci_dev* dev = pciAccess->devices; for (; dev!=nullptr; dev=dev->next) if (dev->bus==busNum && dev->dev==devNum && dev->func==funcNum) { char deviceBuf[128]; deviceBuf[0] = 0; pci_lookup_name(pciAccess, deviceBuf, 128, PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); adapterInfo.busNo = busNum; adapterInfo.deviceNo = devNum; adapterInfo.funcNo = funcNum; adapterInfo.vendorId = dev->vendor_id; adapterInfo.deviceId = dev->device_id; adapterInfo.name = deviceBuf; break; } } class AMDGPUAdapterHandle { private: unsigned int totDeviceCount; std::vector amdDevices; std::vector hwmonIndices; public: AMDGPUAdapterHandle(); unsigned int getAdaptersNum() const { return amdDevices.size(); } AMDGPUAdapterInfo parseAdapterInfo(int index); void setFanSpeed(int index, int fanSpeed) const; void setFanSpeedToDefault(int adapterIndex) const; void setOverdriveCoreParam(int adapterIndex, unsigned int coreOD) const; void setOverdriveMemoryParam(int adapterIndex, unsigned int memoryOD) const; void setPerfCoreLevel(int adapterIndex, unsigned int corePLevel) const; void setPerfMemoryLevel(int adapterIndex, unsigned int memoryPLevel) const; void setPerfSOCLevel(int adapterIndex, unsigned int socPLevel) const; void setPerfDCEFLevel(int adapterIndex, unsigned int dcefPLevel) const; void setPerfFLevel(int adapterIndex, unsigned int fPLevel) const; void setPerformanceControl(int adapterIndex, AMDGPUPerfControl perfControl) const; void getPerformanceClocks(int adapterIndex, unsigned int& coreClock, unsigned int& memoryClock) const; }; static bool getFileContentValue(const char* filename, unsigned int& value) try { value = 0; std::ifstream ifs(filename, std::ios::binary); ifs.exceptions(std::ios::failbit); std::string line; std::getline(ifs, line); char* p = (char*)line.c_str(); char* p2; errno = 0; value = strtoul(p, &p2, 0); if (errno != 0) throw Error("Can't parse value from file"); return (p != p2); } catch(const std::exception& ex) { return false; } static bool getFileContentValue(const char* filename, int& value) try { value = -1; std::ifstream ifs(filename, std::ios::binary); ifs.exceptions(std::ios::failbit); std::string line; std::getline(ifs, line); char* p = (char*)line.c_str(); char* p2; errno = 0; value = strtol(p, &p2, 0); if (errno != 0) throw Error("Can't parse value from file"); return (p != p2); } catch(const std::exception& ex) { return false; } static bool getFileContentString(const char* filename, std::string& str) try { std::ifstream ifs(filename, std::ios::binary); ifs.exceptions(std::ios::failbit); str.clear(); std::getline(ifs, str); return true; } catch(const std::exception& ex) { return false; } static void writeFileContentValue(const char* filename, unsigned int value) { if (access(filename, F_OK|W_OK)) throw Error((std::string("Can't write file '")+filename+"'").c_str()); std::ofstream ofs(filename, std::ios::binary); try { ofs.exceptions(std::ios::failbit); ofs << value << std::endl; } catch(const std::exception& ex) { throw Error((std::string("Can't write file '")+filename+"'").c_str()); } } static void writeFileContent(const char* filename, const char* content) { if (access(filename, F_OK|W_OK)) throw Error((std::string("Can't write file '")+filename+"'").c_str()); std::ofstream ofs(filename, std::ios::binary); try { ofs.exceptions(std::ios::failbit); ofs << content << std::endl; } catch(const std::exception& ex) { throw Error((std::string("Can't write file '")+filename+"'").c_str()); } } AMDGPUAdapterHandle::AMDGPUAdapterHandle() : totDeviceCount(0) { errno = 0; DIR* dirp = opendir("/sys/class/drm"); if (dirp == nullptr) throw Error(errno, "Can't open 'sys/class/drm' directory"); errno = 0; struct dirent* dire; while ((dire = readdir(dirp)) != nullptr) { if (::strncmp(dire->d_name, "card", 4) != 0) continue; // is not card directory const char* p; for (p = dire->d_name + 4; ::isdigit(*p); p++); if (*p != 0) continue; // is not card directory errno = 0; unsigned int v = ::strtoul(dire->d_name + 4, nullptr, 10); totDeviceCount = std::max(totDeviceCount, v+1); } if (errno != 0) { closedir(dirp); throw Error(errno, "Can't read 'sys/class/drm' directory"); } closedir(dirp); // filter AMD GPU cards char dbuf[120]; for (unsigned int i = 0; i < totDeviceCount; i++) { snprintf(dbuf, 120, "/sys/class/drm/card%u/device/vendor", i); unsigned int vendorId = 0; if (!getFileContentValue(dbuf, vendorId)) continue; if (vendorId != 4098) // if not AMD continue; amdDevices.push_back(i); } /* hwmon indices */ for (unsigned int cardIndex: amdDevices) { // search hwmon errno = 0; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon", cardIndex); DIR* dirp = opendir(dbuf); if (dirp == nullptr) throw Error(errno, "Can't open 'sys/class/drm/card?/device/hwmon' directory"); errno = 0; struct dirent* dire; unsigned int hwmonIndex = UINT_MAX; while ((dire = readdir(dirp)) != nullptr) { if (::strncmp(dire->d_name, "hwmon", 5) != 0) continue; // is not hwmon directory const char* p; for (p = dire->d_name + 5; ::isdigit(*p); p++); if (*p != 0) continue; // is not hwmon directory errno = 0; unsigned int v = ::strtoul(dire->d_name + 5, nullptr, 10); hwmonIndex = std::min(hwmonIndex, v); } if (errno != 0) { closedir(dirp); throw Error(errno, "Can't open 'sys/class/drm/card?/hwmon' directory"); } closedir(dirp); if (hwmonIndex == UINT_MAX) throw Error("Can't find hwmon? directory"); hwmonIndices.push_back(hwmonIndex); } } static std::vector parseDPMFile(const char* filename, uint32_t& choosen) { std::vector out; std::ifstream ifs(filename, std::ios::binary); choosen = UINT32_MAX; while (ifs) { std::string line; std::getline(ifs, line); if (line.empty()) break; char* p = (char*)line.c_str(); char* p2 = (char*)line.c_str(); errno = 0; unsigned int index = strtoul(p, &p2, 10); if (errno!=0 || p==p2) throw Error(errno, "Can't parse index"); p = p2; if (*p!=':' || p[1]!=' ') throw Error(errno, "Can't parse next part of line"); p += 2; unsigned int clock = strtoul(p, &p2, 10); if (errno!=0 || p==p2) throw Error(errno, "Can't parse clock"); p = p2; if (::strncmp(p, "Mhz", 3) != 0) throw Error(errno, "Can't parse next part of line"); p += 3; if (*p==' ' && p[1]=='*') choosen = index; out.resize(index+1); out[index] = clock; } return out; } static AMDGPUPerfControl parsePerfControl(const char* filename) try { std::ifstream ifs(filename, std::ios::binary); ifs.exceptions(std::ios::failbit); std::string line; std::getline(ifs, line); if (line=="auto") return AMDGPUPerfControl::AUTO; else if (line=="low") return AMDGPUPerfControl::LOW; else if (line=="high") return AMDGPUPerfControl::HIGH; else if (line=="manual") return AMDGPUPerfControl::MANUAL; else return AMDGPUPerfControl::UNKNOWN; } catch(const std::exception& ex) { return AMDGPUPerfControl::UNKNOWN; } static void parseDPMPCIEFile(const char* filename, unsigned int& pcieMB, unsigned int& lanes) { std::ifstream ifs(filename, std::ios::binary); unsigned int ilanes = 0, ipcieMB = 0; while (ifs) { std::string line; std::getline(ifs, line); if (line.empty()) break; char* p = (char*)line.c_str(); char* p2 = (char*)line.c_str(); errno = 0; strtoul(p, &p2, 10); if (errno!=0 || p==p2) throw Error(errno, "Can't parse index"); p = p2; if (*p!=':' || p[1]!=' ') throw Error(errno, "Can't parse next part of line"); p += 2; double bandwidth = strtod(p, &p2); if (errno!=0 || p==p2) throw Error(errno, "Can't parse bandwidth"); p = p2; if (*p=='G' && p2[1]=='B') ipcieMB = bandwidth*1000; else if (*p=='M' && p2[1]=='B') ipcieMB = bandwidth; else if (*p=='M' && p2[1]=='B') ipcieMB = bandwidth/1000; else throw Error(errno, "Wrong bandwidth specifier"); p += 2; if (::strncmp(p, ", x", 3)!=0) throw Error(errno, "Can't parse next part of line"); errno = 0; ilanes = strtoul(p, &p2, 10); if (errno!=0 || p==p2) throw Error(errno, "Can't parse lanes"); if (*p==' ' && p[1]=='*') { lanes = ilanes; pcieMB = ipcieMB; break; } } } void AMDGPUAdapterHandle::getPerformanceClocks(int adapterIndex, unsigned int& coreClock, unsigned int& memoryClock) const { char dbuf[120]; unsigned int cardIndex = amdDevices[adapterIndex]; unsigned int coreOD = 0; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_sclk_od", cardIndex); getFileContentValue(dbuf, coreOD); unsigned int memoryOD = 0; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_mclk_od", cardIndex); getFileContentValue(dbuf, memoryOD); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_sclk", cardIndex); unsigned int activeClockIndex; std::vector clocks = parseDPMFile(dbuf, activeClockIndex); coreClock = 0; if (!clocks.empty()) coreClock = int(ceil(double(clocks.back()) / (1.0 + coreOD*0.01))); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_mclk", cardIndex); clocks = parseDPMFile(dbuf, activeClockIndex); memoryClock = 0; if (!clocks.empty()) memoryClock = int(ceil(double(clocks.back()) / (1.0 + memoryOD*0.01))); } AMDGPUAdapterInfo AMDGPUAdapterHandle::parseAdapterInfo(int index) { AMDGPUAdapterInfo adapterInfo{}; unsigned int cardIndex = amdDevices[index]; char dbuf[120]; char rlink[120]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device", cardIndex); auto linkRead = ::readlink(dbuf, rlink, sizeof(rlink) - 1); if (linkRead < 0) { throw Error(errno, "Can't readlink 'sys/class/drm/card?/device'"); } rlink[linkRead] = 0; getFromPCI_AMDGPU(rlink, adapterInfo); // if device_id and vendor_id is not set if (adapterInfo.vendorId==0 && adapterInfo.deviceId==0) { snprintf(dbuf, 120, "/sys/class/drm/card%u/device/vendor", cardIndex); getFileContentValue(dbuf, adapterInfo.vendorId); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/device", cardIndex); getFileContentValue(dbuf, adapterInfo.deviceId); char deviceBuf[128]; pci_lookup_name(pciAccess, deviceBuf, 128, PCI_LOOKUP_DEVICE, adapterInfo.vendorId, adapterInfo.deviceId); adapterInfo.name = deviceBuf; } // parse pp_dpm_sclk snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_sclk", cardIndex); unsigned int activeCoreClockIndex; adapterInfo.coreClocks = parseDPMFile(dbuf, activeCoreClockIndex); if (activeCoreClockIndex!=UINT_MAX) adapterInfo.coreClock = adapterInfo.coreClocks[activeCoreClockIndex]; else adapterInfo.coreClock = 0; // parse pp_dpm_mclk snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_mclk", cardIndex); unsigned int activeMemoryClockIndex; adapterInfo.memoryClocks = parseDPMFile(dbuf, activeMemoryClockIndex); if (activeMemoryClockIndex!=UINT_MAX) adapterInfo.memoryClock = adapterInfo.memoryClocks[activeMemoryClockIndex]; else adapterInfo.memoryClock = 0; // parse pp_dpm_socclk snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_socclk", cardIndex); unsigned int activeSocClockIndex; adapterInfo.socClocks = parseDPMFile(dbuf, activeSocClockIndex); if (activeSocClockIndex!=UINT_MAX) adapterInfo.socClock = adapterInfo.socClocks[activeSocClockIndex]; else adapterInfo.socClock = 0; // parse pp_dpm_dcefclk snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_dcefclk", cardIndex); unsigned int activeDcefClockIndex; adapterInfo.dcefClocks = parseDPMFile(dbuf, activeDcefClockIndex); if (activeDcefClockIndex!=UINT_MAX) adapterInfo.dcefClock = adapterInfo.dcefClocks[activeDcefClockIndex]; else adapterInfo.dcefClock = 0; // parse pp_dpm_fclk snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_fclk", cardIndex); unsigned int activeFClockIndex; adapterInfo.fClocks = parseDPMFile(dbuf, activeFClockIndex); if (activeFClockIndex!=UINT_MAX) adapterInfo.fClock = adapterInfo.fClocks[activeFClockIndex]; else adapterInfo.fClock = 0; unsigned int hwmonIndex = hwmonIndices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_sclk_od", cardIndex); getFileContentValue(dbuf, adapterInfo.coreOD); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_mclk_od", cardIndex); getFileContentValue(dbuf, adapterInfo.memoryOD); // get fanspeed snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_min", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.minFanSpeed); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_max", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.maxFanSpeed); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.fanSpeed); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_enable", cardIndex, hwmonIndex); unsigned int pwmEnable = 0; getFileContentValue(dbuf, pwmEnable); adapterInfo.defaultFanSpeed = pwmEnable==2; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp1_input", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.temperature); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp1_label", cardIndex, hwmonIndex); getFileContentString(dbuf, adapterInfo.tempLabel); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp1_crit", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.tempCritical); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp2_input", cardIndex, hwmonIndex); if (getFileContentValue(dbuf, adapterInfo.temperature2)) adapterInfo.extraTemperatures |= 1; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp2_label", cardIndex, hwmonIndex); getFileContentString(dbuf, adapterInfo.temp2Label); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp3_input", cardIndex, hwmonIndex); if (getFileContentValue(dbuf, adapterInfo.temperature3)) adapterInfo.extraTemperatures |= 2; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp3_label", cardIndex, hwmonIndex); getFileContentString(dbuf, adapterInfo.temp3Label); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp4_input", cardIndex, hwmonIndex); if (getFileContentValue(dbuf, adapterInfo.temperature4)) adapterInfo.extraTemperatures |= 4; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/temp4_label", cardIndex, hwmonIndex); getFileContentString(dbuf, adapterInfo.temp4Label); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/in0_label", cardIndex, hwmonIndex); std::string in0Label; if (getFileContentString(dbuf, in0Label)) { if (in0Label=="vddgfx") { snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/in0_input", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.voltage); } } snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/power1_average", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.power); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/power1_cap", cardIndex, hwmonIndex); getFileContentValue(dbuf, adapterInfo.powerCap); // parse GPU load adapterInfo.gpuLoad = -1; adapterInfo.memLoad = -1; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/gpu_busy_percent", cardIndex); getFileContentValue(dbuf, adapterInfo.gpuLoad); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/mem_busy_percent", cardIndex); getFileContentValue(dbuf, adapterInfo.memLoad); if (adapterInfo.gpuLoad == -1 || adapterInfo.memLoad == -1) { snprintf(dbuf, 120, "/sys/kernel/debug/dri/%u/amdgpu_pm_info", cardIndex); std::ifstream ifs(dbuf, std::ios::binary); while (ifs) { std::string line; std::getline(ifs, line); if (line.compare(0, 10, "GPU load: ")==0 || line.compare(0, 10, "GPU Load: ")==0) { errno = 0; char* endp; adapterInfo.gpuLoad = strtoul(line.c_str()+10, &endp, 10); if (errno != 0 || endp == line.c_str()+10) throw Error("Can't parse GPU load"); } if (line.compare(0, 10, "MEM load: ")==0 || line.compare(0, 10, "MEM Load: ")==0) { errno = 0; char* endp; adapterInfo.memLoad = strtoul(line.c_str()+10, &endp, 10); if (errno != 0 || endp == line.c_str()+10) throw Error("Can't parse MEM load"); } } } snprintf(dbuf, 120, "/sys/class/drm/card%u/pp_dpm_pcie", cardIndex); parseDPMPCIEFile(dbuf, adapterInfo.busLanes, adapterInfo.busSpeed); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/power_dpm_force_performance_level", cardIndex); adapterInfo.perfControl = parsePerfControl(dbuf); return adapterInfo; } void AMDGPUAdapterHandle::setFanSpeed(int index, int fanSpeed) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; unsigned int hwmonIndex = hwmonIndices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_enable", cardIndex, hwmonIndex); writeFileContentValue(dbuf, 1); unsigned int minFanSpeed, maxFanSpeed; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_min", cardIndex, hwmonIndex); getFileContentValue(dbuf, minFanSpeed); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_max", cardIndex, hwmonIndex); getFileContentValue(dbuf, maxFanSpeed); snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1", cardIndex, hwmonIndex); writeFileContentValue(dbuf, int(round( fanSpeed/100.0 * (maxFanSpeed-minFanSpeed) + minFanSpeed))); } void AMDGPUAdapterHandle::setFanSpeedToDefault(int index) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; unsigned int hwmonIndex = hwmonIndices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/hwmon/hwmon%u/pwm1_enable", cardIndex, hwmonIndex); writeFileContentValue(dbuf, 2); } void AMDGPUAdapterHandle::setOverdriveCoreParam(int index, unsigned int coreOD) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_sclk_od", cardIndex); writeFileContentValue(dbuf, coreOD); } void AMDGPUAdapterHandle::setOverdriveMemoryParam(int index, unsigned int memoryOD) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_mclk_od", cardIndex); writeFileContentValue(dbuf, memoryOD); } void AMDGPUAdapterHandle::setPerfCoreLevel(int index, unsigned int corePLevel) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_sclk", cardIndex); if (access(dbuf, F_OK)) { throw Error((std::string("File '")+dbuf+"' doesn't exist").c_str()); } setPerformanceControl(index, AMDGPUPerfControl::MANUAL); writeFileContentValue(dbuf, corePLevel); } void AMDGPUAdapterHandle::setPerfMemoryLevel(int index, unsigned int memoryPLevel) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_mclk", cardIndex); if (access(dbuf, F_OK)) { throw Error((std::string("File '")+dbuf+"' doesn't exist").c_str()); } setPerformanceControl(index, AMDGPUPerfControl::MANUAL); writeFileContentValue(dbuf, memoryPLevel); } void AMDGPUAdapterHandle::setPerfSOCLevel(int index, unsigned int socPLevel) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_socclk", cardIndex); if (access(dbuf, F_OK)) { throw Error((std::string("File '")+dbuf+"' doesn't exist").c_str()); } setPerformanceControl(index, AMDGPUPerfControl::MANUAL); writeFileContentValue(dbuf, socPLevel); } void AMDGPUAdapterHandle::setPerfDCEFLevel(int index, unsigned int dcefPLevel) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_dcefclk", cardIndex); if (access(dbuf, F_OK)) { throw Error((std::string("File '")+dbuf+"' doesn't exist").c_str()); } setPerformanceControl(index, AMDGPUPerfControl::MANUAL); writeFileContentValue(dbuf, dcefPLevel); } void AMDGPUAdapterHandle::setPerfFLevel(int index, unsigned int fPLevel) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/pp_dpm_fclk", cardIndex); if (access(dbuf, F_OK)) { throw Error((std::string("File '")+dbuf+"' doesn't exist").c_str()); } setPerformanceControl(index, AMDGPUPerfControl::MANUAL); writeFileContentValue(dbuf, fPLevel); } static const char* perfControlNames[] = { "auto", "low", "manual", "high", "unknown" }; void AMDGPUAdapterHandle::setPerformanceControl(int index, AMDGPUPerfControl perfControl) const { char dbuf[120]; unsigned int cardIndex = amdDevices[index]; snprintf(dbuf, 120, "/sys/class/drm/card%u/device/power_dpm_force_performance_level", cardIndex); writeFileContent(dbuf, perfControlNames[int(perfControl)]); } static void printAdaptersInfo(AMDGPUAdapterHandle& handle, const std::vector& choosenAdapters, bool useChoosen, int watch) { int adaptersNum = handle.getAdaptersNum(); auto choosenIter = choosenAdapters.begin(); int i = 0; std::vector adapterInfos(adaptersNum); for (int ai = 0; ai < adaptersNum; ai++) { if (useChoosen && (choosenIter==choosenAdapters.end() || *choosenIter!=i)) { i++; continue; } adapterInfos[ai] = handle.parseAdapterInfo(ai); } if (watch!=0) beforePrintWatch(watch); choosenIter = choosenAdapters.begin(); i = 0; for (int ai = 0; ai < adaptersNum; ai++) { if (useChoosen && (choosenIter==choosenAdapters.end() || *choosenIter!=i)) { i++; continue; } const AMDGPUAdapterInfo& adapterInfo = adapterInfos[ai]; setTermBold(); std::cout << "Adapter " << i << ": PCI " << adapterInfo.busNo << ":" << adapterInfo.deviceNo << ":" << adapterInfo.funcNo << ": " << adapterInfo.name << "\n"; setTermNormal(); std::cout << " Core: " << adapterInfo.coreClock << " MHz, " "Mem: " << adapterInfo.memoryClock << " MHz, " "CoreOD: " << adapterInfo.coreOD << ", " "MemOD: " << adapterInfo.memoryOD; if (adapterInfo.voltage!=0) std::cout << ", Vddc: " << adapterInfo.voltage << " mV"; if (adapterInfo.socClock!=0 || adapterInfo.dcefClock!=0) { std::cout << "\n "; if (adapterInfo.socClock!=0) { std::cout << "SOC: " << adapterInfo.socClock << " MHz"; if (adapterInfo.dcefClock!=0) std::cout << ", "; } if (adapterInfo.dcefClock!=0) { std::cout << "DCEF: " << adapterInfo.dcefClock << " MHz"; if (adapterInfo.fClock!=0) std::cout << ", "; } if (adapterInfo.fClock!=0) std::cout << "FClock: " << adapterInfo.fClock << " MHz"; } std::cout << "\n " "PerfCtrl: " << perfControlNames[int(adapterInfo.perfControl)]; if (adapterInfo.gpuLoad>=0) std::cout << ", Load: " << adapterInfo.gpuLoad << "%"; if (adapterInfo.memLoad>=0) std::cout << ", MemLoad: " << adapterInfo.memLoad << "%"; std::cout << "\n Temp: "; printTemperature(adapterInfo.temperature/1000.0); if ((adapterInfo.extraTemperatures&1) != 0) { std::cout << ", T2: "; printTemperature(adapterInfo.temperature2/1000.0); } if ((adapterInfo.extraTemperatures&2) != 0) { std::cout << ", T3: "; printTemperature(adapterInfo.temperature3/1000.0); } if ((adapterInfo.extraTemperatures&4) != 0) { std::cout << ", T4: "; printTemperature(adapterInfo.temperature4/1000.0); } std::cout << ", Fan: "; printFanSpeed(double(adapterInfo.fanSpeed-adapterInfo.minFanSpeed)/ double(adapterInfo.maxFanSpeed-adapterInfo.minFanSpeed)*100.0); std::cout << std::endl; if (adapterInfo.powerCap!=0) std::cout << " Power: " << (adapterInfo.power/1000000.0) << " W (cap: " << (adapterInfo.powerCap/1000000.0) << " W)\n"; if (!adapterInfo.coreClocks.empty()) { std::cout << " Core Clocks:"; for (uint32_t v: adapterInfo.coreClocks) std::cout << " " << v; std::cout << std::endl; } if (!adapterInfo.memoryClocks.empty()) { std::cout << " Memory Clocks:"; for (uint32_t v: adapterInfo.memoryClocks) std::cout << " " << v; std::cout << std::endl; } if (!adapterInfo.socClocks.empty()) { std::cout << " SOC Clocks:"; for (uint32_t v: adapterInfo.socClocks) std::cout << " " << v; std::cout << std::endl; } if (!adapterInfo.dcefClocks.empty()) { std::cout << " DCEF Clocks:"; for (uint32_t v: adapterInfo.dcefClocks) std::cout << " " << v; std::cout << std::endl; } if (!adapterInfo.fClocks.empty()) { std::cout << " F Clocks:"; for (uint32_t v: adapterInfo.fClocks) std::cout << " " << v; std::cout << std::endl; } if (useChoosen) ++choosenIter; i++; } } static void printAdaptersInfoVerbose(AMDGPUAdapterHandle& handle, const std::vector& choosenAdapters, bool useChoosen, int watch) { int adaptersNum = handle.getAdaptersNum(); auto choosenIter = choosenAdapters.begin(); int i = 0; std::vector adapterInfos(adaptersNum); for (int ai = 0; ai < adaptersNum; ai++) { if (useChoosen && (choosenIter==choosenAdapters.end() || *choosenIter!=i)) { i++; continue; } adapterInfos[ai] = handle.parseAdapterInfo(ai); } if (watch!=0) beforePrintWatch(watch); choosenIter = choosenAdapters.begin(); i = 0; for (int ai = 0; ai < adaptersNum; ai++) { if (useChoosen && (choosenIter==choosenAdapters.end() || *choosenIter!=i)) { i++; continue; } const AMDGPUAdapterInfo& adapterInfo = adapterInfos[ai]; setTermBold(); std::cout << "Adapter " << i << ": " << adapterInfo.name << "\n"; setTermNormal(); std::cout << " Device Topology: " << adapterInfo.busNo << ":" << adapterInfo.deviceNo << ":" << adapterInfo.funcNo << "\n" " Vendor ID: " << adapterInfo.vendorId << " (0x" << std::hex << adapterInfo.vendorId << std::dec << ")" << "\n" " Device ID: " << adapterInfo.deviceId << " (0x" << std::hex << adapterInfo.deviceId << std::dec << ")" << "\n" " Current CoreClock: " << adapterInfo.coreClock << " MHz\n" " Current MemoryClock: " << adapterInfo.memoryClock << " MHz\n"; if (adapterInfo.socClock!=0) std::cout << " Current SOCClock: " << adapterInfo.socClock << " MHz\n"; if (adapterInfo.dcefClock!=0) std::cout << " Current DCEFClock: " << adapterInfo.dcefClock << " MHz\n"; if (adapterInfo.fClock!=0) std::cout << " Current FClock: " << adapterInfo.fClock << " MHz\n"; std::cout << " Core Overdrive: " << adapterInfo.coreOD << "\n" " Memory Overdrive: " << adapterInfo.memoryOD << "\n"; if (adapterInfo.voltage!=0) std::cout << " Current Voltage: " << adapterInfo.voltage << " mV\n"; std::cout << " Performance Control: " << perfControlNames[int(adapterInfo.perfControl)] << "\n"; if (adapterInfo.gpuLoad>=0) std::cout << " GPU Load: " << adapterInfo.gpuLoad << "%\n"; if (adapterInfo.memLoad>=0) std::cout << " Memory Load: " << adapterInfo.memLoad << "%\n"; std::cout << " Current BusSpeed: " << adapterInfo.busSpeed << "\n" " Current BusLanes: " << adapterInfo.busLanes << "\n" " Temperature"; if (!adapterInfo.tempLabel.empty()) std::cout << " (" << adapterInfo.tempLabel << ")"; std::cout << ": "; printTemperature(adapterInfo.temperature/1000.0); std::cout << "\n"; if ((adapterInfo.extraTemperatures&1) != 0) { std::cout << " Temperature2"; if (!adapterInfo.temp2Label.empty()) std::cout << " (" << adapterInfo.temp2Label << ")"; std::cout << ": "; printTemperature(adapterInfo.temperature2/1000.0); std::cout << "\n"; } if ((adapterInfo.extraTemperatures&2) != 0) { std::cout << " Temperature3"; if (!adapterInfo.temp3Label.empty()) std::cout << " (" << adapterInfo.temp3Label << ")"; std::cout << ": "; printTemperature(adapterInfo.temperature3/1000.0); std::cout << "\n"; } if ((adapterInfo.extraTemperatures&4) != 0) { std::cout << " Temperature4"; if (!adapterInfo.temp4Label.empty()) std::cout << " (" << adapterInfo.temp4Label << ")"; std::cout << ": "; printTemperature(adapterInfo.temperature4/1000.0); std::cout << "\n"; } std::cout << " Critical temperature: " << adapterInfo.tempCritical/1000.0 << " C\n" " FanSpeed Min (Value): " << adapterInfo.minFanSpeed << "\n" " FanSpeed Max (Value): " << adapterInfo.maxFanSpeed << "\n" " Current FanSpeed: "; printFanSpeed((double(adapterInfo.fanSpeed-adapterInfo.minFanSpeed)/ double(adapterInfo.maxFanSpeed-adapterInfo.minFanSpeed)*100.0)); std::cout << "\n" " Controlled FanSpeed: " << (adapterInfo.defaultFanSpeed?"yes":"no") << "\n"; if (adapterInfo.powerCap!=0) std::cout << " Power: " << (adapterInfo.power/1000000.0) << " W (cap: " << (adapterInfo.powerCap/1000000.0) << " W)\n"; // print available core clocks if (!adapterInfo.coreClocks.empty()) { std::cout << " Core Clocks:\n"; for (uint32_t v: adapterInfo.coreClocks) std::cout << " " << v << "MHz\n"; } if (!adapterInfo.memoryClocks.empty()) { std::cout << " Memory Clocks:\n"; for (uint32_t v: adapterInfo.memoryClocks) std::cout << " " << v << "MHz\n"; } if (!adapterInfo.socClocks.empty()) { std::cout << " SOC Clocks:\n"; for (uint32_t v: adapterInfo.socClocks) std::cout << " " << v << "MHz\n"; } if (!adapterInfo.dcefClocks.empty()) { std::cout << " DCEF Clocks:\n"; for (uint32_t v: adapterInfo.dcefClocks) std::cout << " " << v << "MHz\n"; } if (!adapterInfo.fClocks.empty()) { std::cout << " F Clocks:\n"; for (uint32_t v: adapterInfo.fClocks) std::cout << " " << v << "MHz\n"; } if (useChoosen) ++choosenIter; i++; } } /* end of AMDGPU code */ #ifdef HAVE_ADLSDK static void getActiveAdaptersIndices(ADLMainControl& mainControl, int adaptersNum, std::vector& activeAdapters) { activeAdapters.clear(); for (int i = 0; i < adaptersNum; i++) if (mainControl.isAdapterActive(i)) activeAdapters.push_back(i); } static void printAdaptersInfo(ADLMainControl& mainControl, int adaptersNum, const std::vector& activeAdapters, const std::vector& choosenAdapters, bool useChoosen) { std::unique_ptr adapterInfos(new AdapterInfo[adaptersNum]); ::memset(adapterInfos.get(), 0, sizeof(AdapterInfo)*adaptersNum); mainControl.getAdapterInfo(adapterInfos.get()); int i = 0; auto choosenIter = choosenAdapters.begin(); for (int ai = 0; ai < adaptersNum; ai++) { if (!mainControl.isAdapterActive(ai)) continue; if (useChoosen && (choosenIter==choosenAdapters.end() || *choosenIter!=i)) { i++; continue; } if (adapterInfos[ai].strAdapterName[0]==0) getFromPCI(adapterInfos[ai].iAdapterIndex, adapterInfos[ai]); ADLPMActivity activity; mainControl.getCurrentActivity(ai, activity); setTermBold(); std::cout << "Adapter " << i << ": PCI " << adapterInfos[ai].iBusNumber << ":" << adapterInfos[ai].iDeviceNumber << ":" << adapterInfos[ai].iFunctionNumber << ": " << adapterInfos[ai].strAdapterName << "\n"; setTermNormal(); std::cout << " Core: " << activity.iEngineClock/100.0 << " MHz, " "Mem: " << activity.iMemoryClock/100.0 << " MHz, " "Vddc: " << activity.iVddc/1000.0 << " V, " "Load: " << activity.iActivityPercent << "%, " "Temp: "; printTemperature(mainControl.getTemperature(ai, 0)/1000.0); std::cout << ", Fan: "; printFanSpeed(mainControl.getFanSpeed(ai, 0)); std::cout << std::endl; ADLODParameters odParams; mainControl.getODParameters(ai, odParams); std::cout << " Max Ranges: Core: " << odParams.sEngineClock.iMin/100.0 << " - " << odParams.sEngineClock.iMax/100.0 << " MHz, " "Mem: " << odParams.sMemoryClock.iMin/100.0 << " - " << odParams.sMemoryClock.iMax/100.0 << " MHz, " << "Vddc: " << odParams.sVddc.iMin/1000.0 << " - " << odParams.sVddc.iMax/1000.0 << " V\n"; int levelsNum = odParams.iNumberOfPerformanceLevels; std::unique_ptr odPLevels( new ADLODPerformanceLevel[levelsNum]); mainControl.getODPerformanceLevels(ai, false, levelsNum, odPLevels.get()); std::cout << " PerfLevels: Core: " << odPLevels[0].iEngineClock/100.0 << " - " << odPLevels[levelsNum-1].iEngineClock/100.0 << " MHz, " "Mem: " << odPLevels[0].iMemoryClock/100.0 << " - " << odPLevels[levelsNum-1].iMemoryClock/100.0 << " MHz, " "Vddc: " << odPLevels[0].iVddc/1000.0 << " - " << odPLevels[levelsNum-1].iVddc/1000.0 << " V\n"; if (useChoosen) ++choosenIter; i++; } } static void printAdaptersInfoVerbose(ADLMainControl& mainControl, int adaptersNum, const std::vector& activeAdapters, const std::vector& choosenAdapters, bool useChoosen) { std::unique_ptr adapterInfos(new AdapterInfo[adaptersNum]); ::memset(adapterInfos.get(), 0, sizeof(AdapterInfo)*adaptersNum); mainControl.getAdapterInfo(adapterInfos.get()); int i = 0; auto choosenIter = choosenAdapters.begin(); for (int ai = 0; ai < adaptersNum; ai++) { if (!mainControl.isAdapterActive(ai)) continue; if (useChoosen && (choosenIter==choosenAdapters.end() || *choosenIter!=i)) { i++; continue; } if (adapterInfos[ai].strAdapterName[0]==0) getFromPCI(adapterInfos[ai].iAdapterIndex, adapterInfos[ai]); setTermBold(); std::cout << "Adapter " << i << ": " << adapterInfos[ai].strAdapterName << "\n"; setTermNormal(); std::cout << " Device Topology: " << adapterInfos[ai].iBusNumber << ":" << adapterInfos[ai].iDeviceNumber << ":" << adapterInfos[ai].iFunctionNumber << "\n" " Vendor ID: " << adapterInfos[ai].iVendorID << " (0x" << std::hex << adapterInfos[ai].iVendorID << std::dec << ")" << std::endl; ADLFanSpeedInfo fsInfo; ADLPMActivity activity; mainControl.getCurrentActivity(ai, activity); std::cout << " Current CoreClock: " << activity.iEngineClock/100.0 << " MHz\n" " Current MemoryClock: " << activity.iMemoryClock/100.0 << " MHz\n" " Current Voltage: " << activity.iVddc/1000.0 << " V\n" " GPU Load: " << activity.iActivityPercent << "%\n" " Current PerfLevel: " << activity.iCurrentPerformanceLevel << "\n" " Current BusSpeed: " << activity.iCurrentBusSpeed << "\n" " Current BusLanes: " << activity.iCurrentBusLanes<< "\n"; int temperature = mainControl.getTemperature(ai, 0); std::cout << " Temperature: "; printTemperature(temperature/1000.0); std::cout << "\n"; mainControl.getFanSpeedInfo(ai, 0, fsInfo); std::cout << " FanSpeed Min: " << fsInfo.iMinPercent << "%\n" " FanSpeed Max: " << fsInfo.iMaxPercent << "%\n" " FanSpeed MinRPM: " << fsInfo.iMinRPM << " RPM\n" " FanSpeed MaxRPM: " << fsInfo.iMaxRPM << " RPM" << "\n"; std::cout << " Current FanSpeed: "; printFanSpeed(mainControl.getFanSpeed(ai, 0)); std::cout << "\n"; ADLODParameters odParams; mainControl.getODParameters(ai, odParams); std::cout << " CoreClock: " << odParams.sEngineClock.iMin/100.0 << " - " << odParams.sEngineClock.iMax/100.0 << " MHz, step: " << odParams.sEngineClock.iStep/100.0 << " MHz\n" " MemClock: " << odParams.sMemoryClock.iMin/100.0 << " - " << odParams.sMemoryClock.iMax/100.0 << " MHz, step: " << odParams.sMemoryClock.iStep/100.0 << " MHz\n" " Voltage: " << odParams.sVddc.iMin/1000.0 << " - " << odParams.sVddc.iMax/1000.0 << " V, step: " << odParams.sVddc.iStep/1000.0 << " V\n"; std::unique_ptr odPLevels( new ADLODPerformanceLevel[odParams.iNumberOfPerformanceLevels]); mainControl.getODPerformanceLevels(ai, false, odParams.iNumberOfPerformanceLevels, odPLevels.get()); std::cout << " Performance levels: " << odParams.iNumberOfPerformanceLevels << "\n"; for (int j = 0; j < odParams.iNumberOfPerformanceLevels; j++) std::cout << " Performance Level: " << j << "\n" " CoreClock: " << odPLevels[j].iEngineClock/100.0 << " MHz\n" " MemClock: " << odPLevels[j].iMemoryClock/100.0 << " MHz\n" " Voltage: " << odPLevels[j].iVddc/1000.0 << " V\n"; mainControl.getODPerformanceLevels(ai, true, odParams.iNumberOfPerformanceLevels, odPLevels.get()); std::cout << " Default Performance levels: " << odParams.iNumberOfPerformanceLevels << "\n"; for (int j = 0; j < odParams.iNumberOfPerformanceLevels; j++) std::cout << " Performance Level: " << j << "\n" " CoreClock: " << odPLevels[j].iEngineClock/100.0 << " MHz\n" " MemClock: " << odPLevels[j].iMemoryClock/100.0 << " MHz\n" " Voltage: " << odPLevels[j].iVddc/1000.0 << " V\n"; std::cout.flush(); if (useChoosen) ++choosenIter; i++; } } #endif static void parseAdaptersList(const char* string, std::vector& adapters, bool& allAdapters) { adapters.clear(); allAdapters = false; if (::strcmp(string, "all")==0) { allAdapters = true; return; } while (true) { char* endptr; errno = 0; int adapterIndex = strtol(string, &endptr, 10); if (errno!=0 || endptr==string) throw Error("Can't parse adapter index"); string = endptr; if (*string == '-') { // if range string++; errno = 0; int adapterIndexEnd = strtol(string, &endptr, 10); if (errno!=0 || endptr==string) throw Error("Can't parse adapter index"); string = endptr; if (adapterIndex>adapterIndexEnd) throw Error("Wrong range of adapter indices in adapter list"); for (int i = adapterIndex; i <= adapterIndexEnd; i++) adapters.push_back(i); } else adapters.push_back(adapterIndex); if (*string==0) break; if (*string==',') string++; else throw Error("Garbages at adapter list"); } std::sort(adapters.begin(), adapters.end()); adapters.resize(std::unique(adapters.begin(), adapters.end()) - adapters.begin()); } enum class OVCParamType { CORE_CLOCK, MEMORY_CLOCK, VDDC_VOLTAGE, FAN_SPEED, CORE_OD, MEMORY_OD, CUR_CORE_CLOCK, CUR_MEMORY_CLOCK, CUR_SOC_CLOCK, CUR_DCEF_CLOCK, CUR_F_CLOCK, CORE_PERF_LEVEL, MEMORY_PERF_LEVEL }; enum: int { LAST_PERFLEVEL = -1 }; struct OVCParameter { OVCParamType type; std::vector adapters; bool allAdapters; int partId; double value; bool useDefault; std::string argText; }; static bool parseOVCParameter(const char* string, OVCParameter& param) { const char* afterName = strchr(string, ':'); if (afterName==nullptr) { afterName = strchr(string, '='); if (afterName==nullptr) { std::cerr << "This is not parameter: '" << string << "'!" << std::endl; return false; } } std::string name(string, afterName); param.argText = string; param.adapters.clear(); param.adapters.push_back(0); // default is 0 param.allAdapters = false; param.partId = 0; param.useDefault = false; bool partIdSet = false; if (name=="coreclk") { param.type = OVCParamType::CORE_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="memclk") { param.type = OVCParamType::MEMORY_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="ccoreclk") { param.type = OVCParamType::CUR_CORE_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="cmemclk") { param.type = OVCParamType::CUR_MEMORY_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="csocclk") { param.type = OVCParamType::CUR_SOC_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="cdcefclk") { param.type = OVCParamType::CUR_DCEF_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="cfclk") { param.type = OVCParamType::CUR_F_CLOCK; param.partId = LAST_PERFLEVEL; } else if (name=="coreod") { param.type = OVCParamType::CORE_OD; param.partId = LAST_PERFLEVEL; } else if (name=="memod") { param.type = OVCParamType::MEMORY_OD; param.partId = LAST_PERFLEVEL; } else if (name=="corepl") { param.type = OVCParamType::CORE_PERF_LEVEL; param.partId = LAST_PERFLEVEL; } else if (name=="mempl") { param.type = OVCParamType::MEMORY_PERF_LEVEL; param.partId = LAST_PERFLEVEL; } else if (name=="vcore") { param.type = OVCParamType::VDDC_VOLTAGE; param.partId = LAST_PERFLEVEL; } else if (name=="fanspeed") { param.type = OVCParamType::FAN_SPEED; partIdSet = false; } else if (name=="icoreclk") { param.type = OVCParamType::CORE_CLOCK; partIdSet = true; } else if (name=="imemclk") { param.type = OVCParamType::MEMORY_CLOCK; partIdSet = true; } else if (name=="ivcore") { param.type = OVCParamType::VDDC_VOLTAGE; partIdSet = true; } else { std::cerr << "Wrong parameter name in '" << string << "'!" << std::endl; return false; } char* next; if (*afterName==':') { // if is afterName++; try { const char* afterList = ::strchr(afterName, ':'); if (afterList==nullptr) afterList = ::strchr(afterName, '='); if (afterList==nullptr) afterList = afterName + strlen(afterName); // to end if (afterList!=afterName) { std::string listString(afterName, afterList); parseAdaptersList(listString.c_str(), param.adapters, param.allAdapters); afterName = afterList; } } catch(const Error& error) { std::cerr << "Can't parse adapter list for '" << string << "': " << error.what() << std::endl; return false; } } else if (*afterName==0) { std::cerr << "Unterminated parameter '" << string << "'!" << std::endl; return false; } if (*afterName==':' && !partIdSet) { afterName++; errno = 0; int value = strtol(afterName, &next, 10); if (errno!=0) { std::cerr << "Can't parse partId in '" << string << "'!" << std::endl; return false; } if (afterName != next) param.partId = value; afterName = next; } else if (*afterName==0) { std::cerr << "Unterminated parameter '" << string << "'!" << std::endl; return false; } if (*afterName=='=') { afterName++; errno = 0; if (::strcmp(afterName, "default")==0) { param.useDefault = true; afterName += 7; } else { param.value = strtod(afterName, &next); if (errno!=0 || afterName==next) { std::cerr << "Can't parse value in '" << string << "'!" << std::endl; return false; } if (std::isinf(param.value) || std::isnan(param.value)) { std::cerr << "Value of '" << string << "' is not finite!" << std::endl; return false; } afterName = next; } if (*afterName!=0) { std::cerr << "Garbages in '" << string << "'!" << std::endl; return false; } } else { std::cerr << "Unterminated parameter '" << string << "'!" << std::endl; return false; } /*std::cout << "param: " << int(param.type) << ", dev: " << param.adapterIndex << ", pid: " << param.partId << ", value=" << param.value << std::endl;*/ return true; } struct FanSpeedSetup { double value; bool useDefault; bool isSet; }; struct AdapterIterator { const std::vector& adapters; bool allAdapters; int allAdaptersNum; int position; AdapterIterator(const std::vector& _adapters, bool _allAdapters, int _allAdaptersNum) : adapters(_adapters), allAdapters(_allAdapters), allAdaptersNum(_allAdaptersNum), position(0) { } AdapterIterator& operator++() { position++; return *this; } operator bool() const { return (!allAdapters && position < int(adapters.size())) || (allAdapters && position < allAdaptersNum); } bool operator!() const { return !((!allAdapters && position < int(adapters.size())) || (allAdapters && position < allAdaptersNum)); } int operator*() const { return allAdapters ? position : adapters[position]; } }; #ifdef HAVE_ADLSDK static void setOVCParameters(ADLMainControl& mainControl, int adaptersNum, const std::vector& activeAdapters, const std::vector& ovcParams) { setTermBold(); std::cout << "WARNING: setting AMD Overdrive parameters!" << std::endl; std::cout << "\nIMPORTANT NOTICE: Before any setting of AMD Overdrive parameters,\n" "please STOP ANY GPU computations and GPU renderings.\n" "Please use this utility CAREFULLY, because it can DAMAGE your hardware!\n" << std::endl; setTermForeground(SaneColor::BAD); const int realAdaptersNum = activeAdapters.size(); std::vector odParams(realAdaptersNum); std::vector > perfLevels(realAdaptersNum); std::vector > defaultPerfLevels(realAdaptersNum); std::vector changedDevices(realAdaptersNum); std::fill(changedDevices.begin(), changedDevices.end(), false); bool failed = false; for (OVCParameter param: ovcParams) if (!param.allAdapters) { bool listFailed = false; for (int adapterIndex: param.adapters) if (!listFailed && (adapterIndex>=realAdaptersNum || adapterIndex<0)) { std::cerr << "Some adapter indices out of range in '" << param.argText << "'!" << std::endl; listFailed = failed = true; } } // check fanspeed for (OVCParameter param: ovcParams) if (param.type==OVCParamType::FAN_SPEED) { if(param.partId!=0) { std::cerr << "Thermal Control Index is not 0 in '" << param.argText << "'!" << std::endl; failed = true; } if(!param.useDefault && (param.value<0.0 || param.value>100.0)) { std::cerr << "FanSpeed value out of range in '" << param.argText << "'!" << std::endl; failed = true; } } for (int ai = 0; ai < realAdaptersNum; ai++) { int i = activeAdapters[ai]; mainControl.getODParameters(i, odParams[ai]); perfLevels[ai].resize(odParams[ai].iNumberOfPerformanceLevels); defaultPerfLevels[ai].resize(odParams[ai].iNumberOfPerformanceLevels); mainControl.getODPerformanceLevels(i, 0, odParams[ai].iNumberOfPerformanceLevels, perfLevels[ai].data()); mainControl.getODPerformanceLevels(i, 1, odParams[ai].iNumberOfPerformanceLevels, defaultPerfLevels[ai].data()); } // check other params for (OVCParameter param: ovcParams) if (param.type!=OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, realAdaptersNum); ait; ++ait) { int i = *ait; if (i>=realAdaptersNum) continue; int partId = (param.partId!=LAST_PERFLEVEL)?param.partId: odParams[i].iNumberOfPerformanceLevels-1; if (partId >= odParams[i].iNumberOfPerformanceLevels || partId < 0) { std::cerr << "Performance level out of range in '" << param.argText << "'!" << std::endl; failed = true; continue; } switch(param.type) { case OVCParamType::CORE_CLOCK: if (!param.useDefault && (param.value < odParams[i].sEngineClock.iMin/100.0 || param.value > odParams[i].sEngineClock.iMax/100.0)) { std::cerr << "Core clock out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::MEMORY_CLOCK: if (!param.useDefault && (param.value < odParams[i].sMemoryClock.iMin/100.0 || param.value > odParams[i].sMemoryClock.iMax/100.0)) { std::cerr << "Memory clock out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::VDDC_VOLTAGE: if (!param.useDefault && (param.value < odParams[i].sVddc.iMin/1000.0 || param.value > odParams[i].sVddc.iMax/1000.0)) { std::cerr << "Voltage out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; default: break; } } if (failed) { std::cerr << "NO ANY settings applied. Error in parameters!" << std::endl; throw Error("Wrong parameters!"); } setTermStdForeground(); // print what has been changed for (OVCParameter param: ovcParams) if (param.type==OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, realAdaptersNum); ait; ++ait) { std::cout << "Setting fanspeed to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << "%"; std::cout << " for adapter " << *ait << " at thermal controller " << param.partId << std::endl; } for (OVCParameter param: ovcParams) if (param.type!=OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, realAdaptersNum); ait; ++ait) { int i = *ait; int partId = (param.partId!=LAST_PERFLEVEL)?param.partId: odParams[i].iNumberOfPerformanceLevels-1; switch(param.type) { case OVCParamType::CORE_CLOCK: std::cout << "Setting core clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::MEMORY_CLOCK: std::cout << "Setting memory clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CUR_CORE_CLOCK: std::cout << "Current core clock available only for " "AMDGPU-(PRO) drivers." << std::endl; break; case OVCParamType::CUR_MEMORY_CLOCK: std::cout << "Current memory clock available only for " "AMDGPU-(PRO) drivers." << std::endl; break; case OVCParamType::CORE_OD: std::cout << "Core OD available only for " "AMDGPU-(PRO) drivers." << std::endl; break; case OVCParamType::MEMORY_OD: std::cout << "Memory OD available only for " "AMDGPU-(PRO) drivers." << std::endl; break; case OVCParamType::VDDC_VOLTAGE: std::cout << "Setting Vddc voltage to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " V"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CORE_PERF_LEVEL: std::cout << "Core perf level available only for " "AMDGPU-(PRO) drivers." << std::endl; break; case OVCParamType::MEMORY_PERF_LEVEL: std::cout << "Memory perf level available only for " "AMDGPU-(PRO) drivers." << std::endl; break; default: break; } } std::vector fanSpeedSetups(realAdaptersNum); std::fill(fanSpeedSetups.begin(), fanSpeedSetups.end(), FanSpeedSetup{ 0.0, false, false }); for (OVCParameter param: ovcParams) if (param.type==OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, realAdaptersNum); ait; ++ait) { fanSpeedSetups[*ait].value = param.value; fanSpeedSetups[*ait].useDefault = param.useDefault; fanSpeedSetups[*ait].isSet = true; } for (OVCParameter param: ovcParams) if (param.type!=OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, realAdaptersNum); ait; ++ait) { int i = *ait; int partId = (param.partId!=LAST_PERFLEVEL)?param.partId: odParams[i].iNumberOfPerformanceLevels-1; ADLODPerformanceLevel& perfLevel = perfLevels[i][partId]; const ADLODPerformanceLevel& defaultPerfLevel = defaultPerfLevels[i][partId]; switch(param.type) { case OVCParamType::CORE_CLOCK: if (param.useDefault) perfLevel.iEngineClock = defaultPerfLevel.iEngineClock; else perfLevel.iEngineClock = int(round(param.value*100.0)); break; case OVCParamType::MEMORY_CLOCK: if (param.useDefault) perfLevel.iMemoryClock = defaultPerfLevel.iMemoryClock; else perfLevel.iMemoryClock = int(round(param.value*100.0)); break; case OVCParamType::VDDC_VOLTAGE: if (param.useDefault) perfLevel.iVddc = defaultPerfLevel.iVddc; else if (perfLevel.iVddc==0) std::cout << "Voltage for adapter " << i << " is not set!" << std::endl; else perfLevel.iVddc = int(round(param.value*1000.0)); break; default: break; } changedDevices[i] = true; } setTermNormal(); // reset terminal mode /// set fan speeds for (int i = 0; i < realAdaptersNum; i++) if (fanSpeedSetups[i].isSet) { if (!fanSpeedSetups[i].useDefault) mainControl.setFanSpeed(activeAdapters[i], 0 /* must be zero */, int(round(fanSpeedSetups[i].value))); else mainControl.setFanSpeedToDefault(activeAdapters[i], 0); } // set od perflevels for (int i = 0; i < realAdaptersNum; i++) if (changedDevices[i]) mainControl.setODPerformanceLevels(activeAdapters[i], odParams[i].iNumberOfPerformanceLevels, perfLevels[i].data()); } #endif /* AMDGPU code */ struct PerfClocks { unsigned int coreClock; unsigned int memoryClock; unsigned int curCoreClock; unsigned int curMemoryClock; }; static void setOVCParameters(AMDGPUAdapterHandle& handle, const std::vector& ovcParams, const std::vector& perfClocks, const std::vector& adapterInfos) { setTermBold(); std::cout << "WARNING: setting AMD Overdrive parameters!" << std::endl; std::cout << "\nIMPORTANT NOTICE: Before any setting of AMD Overdrive parameters,\n" "please STOP ANY GPU computations and GPU renderings.\n" "Please use this utility CAREFULLY, because it can DAMAGE your hardware!\n" << std::endl; setTermForeground(SaneColor::BAD); bool failed = false; int adaptersNum = handle.getAdaptersNum(); for (OVCParameter param: ovcParams) if (!param.allAdapters) { bool listFailed = false; for (int adapterIndex: param.adapters) if (!listFailed && (adapterIndex>=adaptersNum || adapterIndex<0)) { std::cerr << "Some adapter indices out of range in '" << param.argText << "'!" << std::endl; listFailed = failed = true; } } // check fanspeed for (OVCParameter param: ovcParams) if (param.type==OVCParamType::FAN_SPEED) { if(param.partId!=0) { std::cerr << "Thermal Control Index is not 0 in '" << param.argText << "'!" << std::endl; failed = true; } if(!param.useDefault && (param.value<0.0 || param.value>100.0)) { std::cerr << "FanSpeed value out of range in '" << param.argText << "'!" << std::endl; failed = true; } } // check other params for (OVCParameter param: ovcParams) if (param.type!=OVCParamType::FAN_SPEED) { for (AdapterIterator ait(param.adapters, param.allAdapters, adaptersNum); ait; ++ait) { int i = *ait; if (i>=adaptersNum) continue; int partId = (param.partId!=LAST_PERFLEVEL)?param.partId:0; if (partId != 0) { std::cerr << "Performance level out of range in '" << param.argText << "'!" << std::endl; failed = true; continue; } const PerfClocks& perfClks = perfClocks[i]; const std::vector& coreClocks = adapterInfos[i].coreClocks; const std::vector& memoryClocks = adapterInfos[i].memoryClocks; const std::vector& socClocks = adapterInfos[i].socClocks; const std::vector& dcefClocks = adapterInfos[i].dcefClocks; const std::vector& fClocks = adapterInfos[i].fClocks; switch(param.type) { case OVCParamType::CORE_CLOCK: if (!param.useDefault && (param.value < perfClks.coreClock || param.value > perfClks.coreClock*1.20)) { std::cerr << "Core clock out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::MEMORY_CLOCK: if (!param.useDefault && (param.value < perfClks.memoryClock || param.value > perfClks.memoryClock*1.20)) { std::cerr << "Memory clock out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CUR_CORE_CLOCK: if (!param.useDefault && (size_t(param.value) != param.value || std::find(coreClocks.begin(), coreClocks.end(), (unsigned int)param.value) == coreClocks.end())) { std::cerr << "Wrong current core clock '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CUR_MEMORY_CLOCK: if (!param.useDefault && (size_t(param.value) != param.value || std::find(memoryClocks.begin(), memoryClocks.end(), (unsigned int)param.value) == memoryClocks.end())) { std::cerr << "Wrong current memory clock '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CUR_SOC_CLOCK: if (!param.useDefault && (size_t(param.value) != param.value || std::find(socClocks.begin(), socClocks.end(), (unsigned int)param.value) == socClocks.end())) { std::cerr << "Wrong current SOC clock '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CUR_DCEF_CLOCK: if (!param.useDefault && (size_t(param.value) != param.value || std::find(dcefClocks.begin(), dcefClocks.end(), (unsigned int)param.value) == dcefClocks.end())) { std::cerr << "Wrong current DCEF clock '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CUR_F_CLOCK: if (!param.useDefault && (size_t(param.value) != param.value || std::find(fClocks.begin(), fClocks.end(), (unsigned int)param.value) == fClocks.end())) { std::cerr << "Wrong current F clock '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CORE_OD: if (!param.useDefault && (param.value < 0.0 || param.value > 20.0)) { std::cerr << "Core Overdrive out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::MEMORY_OD: if (!param.useDefault && (param.value < 0.0 || param.value > 20.0)) { std::cerr << "Memory Overdrive out of range in '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::CORE_PERF_LEVEL: if (!param.useDefault && (param.value < 0.0 || param.value >= coreClocks.size() || size_t(param.value) != param.value)) { std::cerr << "Wrong core performance level '" << param.argText << "'!" << std::endl; failed = true; } break; case OVCParamType::MEMORY_PERF_LEVEL: if (!param.useDefault && (param.value < 0.0 || param.value >= memoryClocks.size() || size_t(param.value) != param.value)) { std::cerr << "Wrong memory performance level '" << param.argText << "'!" << std::endl; failed = true; } break; default: break; } } } if (failed) { std::cerr << "NO ANY settings applied. Error in parameters!" << std::endl; throw Error("Wrong parameters!"); } // print informations in bold setTermStdForeground(); // print what has been changed for (OVCParameter param: ovcParams) if (param.type==OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, adaptersNum); ait; ++ait) { std::cout << "Setting fanspeed to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << "%"; std::cout << " for adapter " << *ait << " at thermal controller " << param.partId << std::endl; } for (OVCParameter param: ovcParams) if (param.type!=OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, adaptersNum); ait; ++ait) { int i = *ait; int partId = (param.partId!=LAST_PERFLEVEL)?param.partId:0; switch(param.type) { case OVCParamType::CORE_CLOCK: std::cout << "Setting core clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::MEMORY_CLOCK: std::cout << "Setting memory clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CUR_CORE_CLOCK: std::cout << "Setting current core clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CUR_MEMORY_CLOCK: std::cout << "Setting current memory clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CUR_SOC_CLOCK: std::cout << "Setting current SOC clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CUR_DCEF_CLOCK: std::cout << "Setting current DCEF clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CUR_F_CLOCK: std::cout << "Setting current F clock to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value << " MHz"; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::CORE_OD: std::cout << "Setting core overdrive to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::MEMORY_OD: std::cout << "Setting memory overdrive to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::VDDC_VOLTAGE: std::cout << "VDDC voltage available only for " "AMD Catalyst/Crimson drivers." << std::endl; break; case OVCParamType::CORE_PERF_LEVEL: std::cout << "Setting core performance level to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; case OVCParamType::MEMORY_PERF_LEVEL: std::cout << "Setting memory performance level to "; if (param.useDefault) std::cout << "default"; else std::cout << param.value; std::cout << " for adapter " << i << " at performance level " << partId << std::endl; break; default: break; } } setTermNormal(); // reset terminal modes std::vector fanSpeedSetups(adaptersNum); std::fill(fanSpeedSetups.begin(), fanSpeedSetups.end(), FanSpeedSetup{ 0.0, false, false }); for (OVCParameter param: ovcParams) if (param.type==OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, adaptersNum); ait; ++ait) { fanSpeedSetups[*ait].value = param.value; fanSpeedSetups[*ait].useDefault = param.useDefault; fanSpeedSetups[*ait].isSet = true; } for (OVCParameter param: ovcParams) if (param.type!=OVCParamType::FAN_SPEED) for (AdapterIterator ait(param.adapters, param.allAdapters, adaptersNum); ait; ++ait) { int i = *ait; const PerfClocks& perfClks = perfClocks[i]; const std::vector& coreClocks = adapterInfos[i].coreClocks; const std::vector& memoryClocks = adapterInfos[i].memoryClocks; const std::vector& socClocks = adapterInfos[i].socClocks; const std::vector& dcefClocks = adapterInfos[i].dcefClocks; const std::vector& fClocks = adapterInfos[i].fClocks; switch(param.type) { case OVCParamType::CORE_CLOCK: if (param.useDefault) handle.setOverdriveCoreParam(i, 0); else handle.setOverdriveCoreParam(i, int(round((double(param.value - perfClks.coreClock) / perfClks.coreClock)*100.0))); break; case OVCParamType::MEMORY_CLOCK: if (param.useDefault) handle.setOverdriveMemoryParam(i, 0); else handle.setOverdriveMemoryParam(i, int(round((double(param.value - perfClks.memoryClock) / perfClks.memoryClock)*100.0))); break; case OVCParamType::CUR_CORE_CLOCK: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else { auto ccit = std::find(coreClocks.begin(), coreClocks.end(), (unsigned int)round(param.value)); if (ccit != coreClocks.end()) handle.setPerfCoreLevel(i, ccit-coreClocks.begin()); } break; case OVCParamType::CUR_MEMORY_CLOCK: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else { auto cmit = std::find(memoryClocks.begin(), memoryClocks.end(), (unsigned int)round(param.value)); if (cmit != memoryClocks.end()) handle.setPerfMemoryLevel(i, cmit-memoryClocks.begin()); } break; case OVCParamType::CUR_SOC_CLOCK: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else { auto cmit = std::find(socClocks.begin(), socClocks.end(), (unsigned int)round(param.value)); if (cmit != socClocks.end()) handle.setPerfSOCLevel(i, cmit-socClocks.begin()); } break; case OVCParamType::CUR_DCEF_CLOCK: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else { auto cmit = std::find(dcefClocks.begin(), dcefClocks.end(), (unsigned int)round(param.value)); if (cmit != dcefClocks.end()) handle.setPerfDCEFLevel(i, cmit-dcefClocks.begin()); } break; case OVCParamType::CUR_F_CLOCK: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else { auto cmit = std::find(fClocks.begin(), fClocks.end(), (unsigned int)round(param.value)); if (cmit != fClocks.end()) handle.setPerfFLevel(i, cmit-fClocks.begin()); } break; case OVCParamType::CORE_OD: if (param.useDefault) handle.setOverdriveCoreParam(i, 0); else handle.setOverdriveCoreParam(i, int(round(param.value))); break; case OVCParamType::MEMORY_OD: if (param.useDefault) handle.setOverdriveMemoryParam(i, 0); else handle.setOverdriveMemoryParam(i, int(round(param.value))); break; case OVCParamType::CORE_PERF_LEVEL: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else handle.setPerfCoreLevel(i, int(round(param.value))); break; case OVCParamType::MEMORY_PERF_LEVEL: if (param.useDefault) handle.setPerformanceControl(i, AMDGPUPerfControl::AUTO); else handle.setPerfMemoryLevel(i, int(round(param.value))); break; default: break; } } /// set fan speeds for (int i = 0; i < adaptersNum; i++) if (fanSpeedSetups[i].isSet) { if (!fanSpeedSetups[i].useDefault) handle.setFanSpeed(i, int(round(fanSpeedSetups[i].value))); else handle.setFanSpeedToDefault(i); } } /* AMDGPU code */ static const char* helpAndUsageString = "amdcovc " AMDCOVC_VERSION " by Mateusz Szpakowski (matszpk@interia.pl)\n" "Program is distributed under terms of the GPLv2.\n" "Program available at https://github.com/matszpk/amdcovc.\n" "\n" "Usage: amdcovc [--help|-?] [-v|--verbose] [-a LIST|--adapters=LIST]\n" "[-w N|--watch=N] [PARAM ...]\n" "Print AMD Overdrive informations if no parameter given.\n" "Set AMD Overdrive parameters (clocks, fanspeeds,...) if any parameter given.\n" "\n" "List of parameters:\n" " coreclk[:[ADAPTERS][:LEVEL]]=CLOCK set core clock in MHz\n" " memclk[:[ADAPTERS][:LEVEL]]=CLOCK set memory clock in MHz\n" " ccoreclk[:[ADAPTERS][:LEVEL]]=CLOCK set current core clock in MHz (AMDGPU)\n" " cmemclk[:[ADAPTERS][:LEVEL]]=CLOCK set current memory clock in MHz (AMDGPU)\n" " csocclk[:[ADAPTERS][:LEVEL]]=CLOCK set current SOC clock in MHz (AMDGPU) (vega)\n" " cdcefclk[:[ADAPTERS][:LEVEL]]=CLOCK set current DCEF clock in MHz (AMDGPU) (vega)\n" " cfclk[:[ADAPTERS][:LEVEL]]=CLOCK set current F clock in MHz (AMDGPU) (vega2)\n" " coreod[:[ADAPTERS][:LEVEL]]=PERCENT set core Overdrive in percent (AMDGPU)\n" " memod[:[ADAPTERS][:LEVEL]]=PERCENT set memory Overdrive in percent (AMDGPU)\n" " coreod[:[ADAPTERS][:LEVEL]]=LEVEL set core performance level (AMDGPU)\n" " memod[:[ADAPTERS][:LEVEL]]=LEVEL set memory performance level (AMDGPU)\n" " vcore[:[ADAPTERS][:LEVEL]]=VOLTAGE set Vddc voltage in Volts\n" " icoreclk[:ADAPTERS]=CLOCK set core clock in MHz for idle level\n" " imemclk[:ADAPTERS]=CLOCK set memory clock in MHz for idle level\n" " ivcore[:ADAPTERS]=VOLTAGE set Vddc voltage in Volts for idle level\n" " fanspeed[:[ADAPTERS][:THID]]=PERCENT set fanspeed in percents\n" "Extra specifiers in parameters:\n" " ADAPTERS adapter (devices) index list (default is 0)\n" " LEVEL performance level (typically 0 or 1, default is last)\n" " THID thermal controller index (must be 0)\n" "You can use 'default' in value place to set default value.\n" "For fanspeed 'default' value force automatic speed setup.\n" "\n" "List of options:\n" " -a, --adapters=LIST print informations only for these adapters\n" " -v, --verbose print verbose informations\n" " -w, --watch=SECONDS print status every SECONDS seconds to terminal\n" " --version print version\n" " -?, --help print help\n" "\n" "Adapter list specified in parameters and '--adapter' option is comma-separated list\n" "with ranges 'first-last' or 'all'. Examples: 'all', '0-2', '0,1,3-5'\n" "\n" "Sample usage:\n" "amdcovc\n" " print short informations about state of the all adapters\n" "amdcovc -a 1,2,4-6\n" " print short informations about adapter 1, 2 and 4 to 6\n" "amdcovc coreclk:1=900 coreclk=1000\n" " set core clock to 900 for adapter 1, set core clock to 1000 for adapter 0\n" "amdcovc coreclk:1:0=900 coreclk:0:1=1000\n" " set core clock to 900 for adapter 1 at performance level 0,\n" " set core clock to 1000 for adapter 0 at performance level 1\n" "amdcovc coreclk:1:0=default coreclk:0:1=default\n" " set core clock to default for adapter 0 and 1\n" "amdcovc fanspeed=75 fanspeed:2=60 fanspeed:1=default\n" " set fanspeed to 75% for adapter 0 and set fanspeed to 60% for adapter 2\n" " set fanspeed to default for adapter 1\n" "amdcovc vcore=1.111 vcore::0=0.81\n" " set Vddc voltage to 1.111 V for adapter 0\n" " set Vddc voltage to 0.81 for adapter 0 for performance level 0\n" "\n" "Coloring and text styles:\n" "AMDCOVC allow to control terminal text styling by two environment variables:\n" " AMDCOVC_NOBOLD disable bold fonts\n" " AMDCOVC_NOCOLOR disable text coloring\n" "Set value of an environment variable to '1','on','y','true','t', 'on' or 'enable'\n" "to enable its function.\n" "\n"; static const char* helpAndUsageStringCaution = "IMPORTANT NOTICE: Before any setting of AMD Overdrive parameters,\n" "please STOP ANY GPU computations and GPU renderings.\n" "Please use this utility CAREFULLY, because it can DAMAGE your hardware!\n" "\n"; static const char* helpAndUsageStringNotes = "NOTICE FOR AMD Crimson/Catalyst drivers:\n" "If no X11 server is running, then this program requires root privileges.\n" "NOTICE FOR AMDGPU(-PRO) drivers:\n" "Any parameter settings requires root privileges.\n"; int main(int argc, const char** argv) try { initializeTermModes(); bool printHelp = false; bool printVerbose = false; std::vector ovcParameters; std::vector choosenAdapters; bool useAdaptersList = false; bool chooseAllAdapters = false; int watch=0; { const char* loc = setlocale(LC_CTYPE, ""); const size_t locLen = loc != nullptr ? ::strlen(loc) : 0; terminalUTF8 = locLen>=6 && ::strcmp(loc+locLen-6, ".UTF-8")==0; loc = setlocale(LC_CTYPE, "C"); } bool failed = false; for (int i = 1; i < argc; i++) if (::strcmp(argv[i], "--help")==0 || ::strcmp(argv[i], "-?")==0) printHelp = true; else if (::strcmp(argv[i], "--verbose")==0 || ::strcmp(argv[i], "-v")==0) printVerbose = true; else if (::strncmp(argv[i], "--adapters=", 11)==0) { parseAdaptersList(argv[i]+11, choosenAdapters, chooseAllAdapters); useAdaptersList = true; } else if (::strcmp(argv[i], "--adapters")==0) { if (i+1 < argc) { parseAdaptersList(argv[++i], choosenAdapters, chooseAllAdapters); useAdaptersList = true; } else throw Error("Adapter list not supplied"); } else if (::strncmp(argv[i], "-a", 2)==0) { if (argv[i][2]!=0) parseAdaptersList(argv[i]+2, choosenAdapters, chooseAllAdapters); else if (i+1 < argc) parseAdaptersList(argv[++i], choosenAdapters, chooseAllAdapters); else throw Error("Adapter list not supplied"); useAdaptersList = true; } else if (::strncmp(argv[i], "--watch=", 8)==0) { errno = 0; watch = strtoul(argv[i]+8, nullptr, 10); if (errno!=0) throw Error("Can't parse watch seconds"); } else if (::strcmp(argv[i], "--watch")==0) { if (i+1 < argc) { errno = 0; watch = strtoul(argv[++i], nullptr, 10); if (errno!=0) throw Error("Can't parse watch seconds"); } else throw Error("Watch seconds not supplied"); } else if (::strncmp(argv[i], "-w", 2)==0) { const char* p = 0; if (argv[i][2]!=0) p = argv[i]+2; else if (i+1 < argc) p = argv[++i]; else throw Error("Watch seconds not supplied"); errno = 0; watch = strtoul(p, nullptr, 10); if (errno!=0) throw Error("Can't parse watch seconds"); } else if (::strcmp(argv[i], "--version")==0) { std::cout << "amdcovc " AMDCOVC_VERSION " by Mateusz Szpakowski (matszpk@interia.pl)\n" "Program is distributed under terms of the GPLv2.\n" "Program available at https://github.com/matszpk/amdcovc.\n" << std::endl; return 0; } else { OVCParameter param; setTermBold(); setTermForeground(SaneColor::BAD); if (parseOVCParameter(argv[i], param)) ovcParameters.push_back(param); else failed = true; setTermStdForeground(); setTermNormal(); } if (printHelp) { std::cout << helpAndUsageString; setTermBold(); std::cout << helpAndUsageStringCaution; setTermNormal(); std::cout << helpAndUsageStringNotes; std::cout.flush(); return 0; } if (failed) throw Error("Can't parse parameters"); #ifdef HAVE_ADLSDK ATIADLHandle handle; if (handle.open()) { // AMD Catalyst/Crimson ADLMainControl mainControl(handle, 0); int adaptersNum = mainControl.getAdaptersNum(); /* list for converting user indices to input indices to ADL interface */ std::vector activeAdapters; getActiveAdaptersIndices(mainControl, adaptersNum, activeAdapters); if (useAdaptersList) // sort and check adapter list for (int adapterIndex: choosenAdapters) if (adapterIndex>=int(activeAdapters.size()) || adapterIndex<0) throw Error("Some adapter indices out of range"); if (!ovcParameters.empty()) setOVCParameters(mainControl, adaptersNum, activeAdapters, ovcParameters); else { do { auto tstart = std::chrono::system_clock::now(); if (watch!=0) beforePrintWatch(watch); if (printVerbose) printAdaptersInfoVerbose(mainControl, adaptersNum, activeAdapters, choosenAdapters, useAdaptersList && !chooseAllAdapters); else printAdaptersInfo(mainControl, adaptersNum, activeAdapters, choosenAdapters, useAdaptersList && !chooseAllAdapters); if (watch!=0) { auto tend = std::chrono::system_clock::now(); auto dur = std::chrono::seconds(watch)-(tend-tstart); if (dur>std::chrono::seconds(0)) std::this_thread::sleep_for(dur); } } while (watch!=0); } } else #endif { // AMD GPU(-PRO) AMDGPUAdapterHandle handle; if (!ovcParameters.empty()) { std::vector perfClocks; for (unsigned int i = 0; i < handle.getAdaptersNum(); i++) { unsigned int coreClock, memoryClock; handle.getPerformanceClocks(i, coreClock, memoryClock); //std::cout << "PerfClocks: " << coreClock << ", " << memoryClock << std::endl; perfClocks.push_back(PerfClocks{ coreClock, memoryClock }); } std::vector adapterInfos; for (unsigned int i = 0; i < handle.getAdaptersNum(); i++) adapterInfos.push_back(handle.parseAdapterInfo(i)); setOVCParameters(handle, ovcParameters, perfClocks, adapterInfos); } else { if (useAdaptersList) // sort and check adapter list for (int adapterIndex: choosenAdapters) if (adapterIndex>=int(handle.getAdaptersNum()) || adapterIndex<0) throw Error("Some adapter indices out of range"); do { auto tstart = std::chrono::system_clock::now(); if (printVerbose) printAdaptersInfoVerbose(handle, choosenAdapters, useAdaptersList && !chooseAllAdapters, watch); else printAdaptersInfo(handle, choosenAdapters, useAdaptersList && !chooseAllAdapters, watch); if (watch!=0) { auto tend = std::chrono::system_clock::now(); auto dur = std::chrono::seconds(watch)-(tend-tstart); if (dur>std::chrono::seconds(0)) std::this_thread::sleep_for(dur); } } while (watch!=0); } } if (pciAccess!=nullptr) pci_cleanup(pciAccess); return 0; } catch(const std::exception& ex) { if (pciAccess!=nullptr) pci_cleanup(pciAccess); setTermBold(); setTermForeground(SaneColor::BAD); std::cerr << ex.what() << std::endl; setTermStdForeground(); setTermNormal(); return 1; }