1010#include <unistd.h>
1111#include <dirent.h>
1212
13+ #define FF_CPUINFO_PATH "/proc/cpuinfo"
14+
1315static double parseHwmonDir (FFstrbuf * dir , FFstrbuf * buffer )
1416{
1517 //https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
@@ -437,8 +439,8 @@ FF_MAYBE_UNUSED static uint16_t getPackageCount(FFstrbuf* cpuinfo)
437439FF_MAYBE_UNUSED static const char * detectCPUX86 (const FFCPUOptions * options , FFCPUResult * cpu )
438440{
439441 FF_STRBUF_AUTO_DESTROY cpuinfo = ffStrbufCreateA (PROC_FILE_BUFFSIZ );
440- if (!ffReadFileBuffer ("/proc/cpuinfo" , & cpuinfo ) || cpuinfo .length == 0 )
441- return "ffReadFileBuffer(\"/proc/cpuinfo \") failed" ;
442+ if (!ffReadFileBuffer (FF_CPUINFO_PATH , & cpuinfo ) || cpuinfo .length == 0 )
443+ return "ffReadFileBuffer(\"" FF_CPUINFO_PATH " \") failed" ;
442444
443445 FF_STRBUF_AUTO_DESTROY physicalCoresBuffer = ffStrbufCreate ();
444446 FF_STRBUF_AUTO_DESTROY cpuMHz = ffStrbufCreate ();
@@ -567,6 +569,30 @@ FF_MAYBE_UNUSED static void detectSocName(FFCPUResult* cpu)
567569 }
568570}
569571
572+ #ifdef __loongarch__
573+ FF_MAYBE_UNUSED static uint16_t getLoongarchPropCount (FFstrbuf * cpuinfo , const char * key )
574+ {
575+ const char * p = cpuinfo -> chars ;
576+ uint64_t low = 0 , high = 0 ;
577+ uint32_t keylen = (uint32_t ) strlen (key );
578+
579+ while ((p = memmem (p , cpuinfo -> length - (uint32_t ) (p - cpuinfo -> chars ), key , keylen )))
580+ {
581+ if (!p ) break ;
582+ p += keylen ;
583+ char * pend ;
584+ unsigned long id = strtoul (p , & pend , 10 );
585+ if (__builtin_expect (id > 64 , false))
586+ high |= 1 << (id - 64 );
587+ else
588+ low |= 1 << id ;
589+ p = pend ;
590+ }
591+
592+ return (uint16_t ) (__builtin_popcountll (low ) + __builtin_popcountll (high ));
593+ }
594+ #endif
595+
570596FF_MAYBE_UNUSED static const char * detectCPUOthers (const FFCPUOptions * options , FFCPUResult * cpu )
571597{
572598 cpu -> coresPhysical = cpu -> coresLogical = (uint16_t ) get_nprocs_conf ();
@@ -583,8 +609,8 @@ FF_MAYBE_UNUSED static const char* detectCPUOthers(const FFCPUOptions* options,
583609 if (cpu -> name .length == 0 )
584610 {
585611 FF_STRBUF_AUTO_DESTROY cpuinfo = ffStrbufCreateA (PROC_FILE_BUFFSIZ );
586- if (!ffReadFileBuffer ("/proc/cpuinfo" , & cpuinfo ) || cpuinfo .length == 0 )
587- return "ffReadFileBuffer(\"/proc/cpuinfo \") failed" ;
612+ if (!ffReadFileBuffer (FF_CPUINFO_PATH , & cpuinfo ) || cpuinfo .length == 0 )
613+ return "ffReadFileBuffer(\"" FF_CPUINFO_PATH " \") failed" ;
588614
589615 FF_STRBUF_AUTO_DESTROY cpuMHz = ffStrbufCreate ();
590616 FF_STRBUF_AUTO_DESTROY cpuIsa = ffStrbufCreate ();
@@ -621,6 +647,10 @@ FF_MAYBE_UNUSED static const char* detectCPUOthers(const FFCPUOptions* options,
621647 ffStrbufAppend (& cpu -> name , & cpuIsa );
622648 }
623649 }
650+ #elif __loongarch__
651+ cpu -> packages = getLoongarchPropCount (& cpuinfo , "\npackage\t\t\t:" );
652+ cpu -> coresPhysical = getLoongarchPropCount (& cpuinfo , "\ncore\t\t\t:" );
653+ if (cpu -> packages > 1 ) cpu -> coresPhysical *= cpu -> packages ;
624654 #endif
625655 }
626656
0 commit comments