aboutsummaryrefslogblamecommitdiff
path: root/src/xz/hardware.c
blob: d91b4cee1500458d6eb1ff2e675014ab5bca92c0 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                                               
                            
  

                                                  



                                                                               
                            



                                                                
                            
 

                         
 
 

                                                  
 

                                                                    
                                                




                                              




               

                              
 
                           


 

                                            
 







                                                                       


 

                                                     
 


                                  
                                      
 

                                                                       
                     
                                                           
 
                                          



               
               
                           
 
                        


 


                   

                                    

               
///////////////////////////////////////////////////////////////////////////////
//
/// \file       hardware.c
/// \brief      Detection of available hardware resources
//
//  Author:     Lasse Collin
//
//  This file has been put into the public domain.
//  You can do whatever you want with this file.
//
///////////////////////////////////////////////////////////////////////////////

#include "private.h"
#include "tuklib_cpucores.h"


/// Maximum number of free *coder* threads. This can be set with
/// the --threads=NUM command line option.
static uint32_t threadlimit;

/// Memory usage limit
static uint64_t memlimit;


extern void
hardware_threadlimit_set(uint32_t new_threadlimit)
{
	if (new_threadlimit == 0) {
		// The default is the number of available CPU cores.
		threadlimit = tuklib_cpucores();
		if (threadlimit == 0)
			threadlimit = 1;
	} else {
		threadlimit = new_threadlimit;
	}

	return;
}


extern uint32_t
hardware_threadlimit_get(void)
{
	return threadlimit;
}


extern void
hardware_memlimit_set(uint64_t new_memlimit)
{
	if (new_memlimit == 0) {
		// The default is 40 % of total installed physical RAM.
		hardware_memlimit_set_percentage(40);
	} else {
		memlimit = new_memlimit;
	}

	return;
}


extern void
hardware_memlimit_set_percentage(uint32_t percentage)
{
	assert(percentage > 0);
	assert(percentage <= 100);

	uint64_t mem = lzma_physmem();

	// If we cannot determine the amount of RAM, use the assumption
	// defined by the configure script.
	if (mem == 0)
		mem = (uint64_t)(ASSUME_RAM) * 1024 * 1024;

	memlimit = percentage * mem / 100;
	return;
}


extern uint64_t
hardware_memlimit_get(void)
{
	return memlimit;
}


extern void
hardware_init(void)
{
	hardware_memlimit_set(0);
	hardware_threadlimit_set(0);
	return;
}
'n747' href='#n747'>747 748 749 750 751 752 753 754 755 756 757 758 759 760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
                                                                        




































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
 


































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              













































































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 



















                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      




                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    









































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              


















































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    















































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 















                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
¯