3939
4040# define SYSTEMD_PROPERTY_PREFIX "org.systemd.property."
4141
42+ # define CGROUP_BFQ_WEIGHT_MIN ((uint64_t) 1)
43+ # define CGROUP_BFQ_WEIGHT_DEFAULT ((uint64_t) 100)
44+ # define CGROUP_BFQ_WEIGHT_MAX ((uint64_t) 1000)
45+
46+ # define CGROUP_WEIGHT_MIN ((uint64_t) 1)
47+ # define CGROUP_WEIGHT_DEFAULT ((uint64_t) 100)
48+ # define CGROUP_WEIGHT_MAX ((uint64_t) 10000)
49+
4250int
4351cpuset_string_to_bitmask (const char * str , char * * out , size_t * out_size , libcrun_error_t * err )
4452{
@@ -806,7 +814,7 @@ get_memory_limit (runtime_spec_schema_config_linux_resources *resources, uint64_
806814}
807815
808816static inline int
809- get_weight (runtime_spec_schema_config_linux_resources * resources , uint64_t * weight , libcrun_error_t * err )
817+ get_cpu_weight (runtime_spec_schema_config_linux_resources * resources , uint64_t * weight , libcrun_error_t * err )
810818{
811819 if (resources -> cpu && resources -> cpu -> shares_present )
812820 {
@@ -820,6 +828,37 @@ get_weight (runtime_spec_schema_config_linux_resources *resources, uint64_t *wei
820828 return get_value_from_unified_map (resources , "cpu.weight" , weight , err );
821829}
822830
831+ /* Convert io.bfq.weight to io.weight doing the inverse conversion performed by systemd with BFQ_WEIGHT. */
832+ static inline uint64_t
833+ IO_WEIGHT (uint64_t bfq_weight )
834+ {
835+ return bfq_weight <= CGROUP_BFQ_WEIGHT_DEFAULT ? CGROUP_WEIGHT_DEFAULT - (CGROUP_BFQ_WEIGHT_DEFAULT - bfq_weight ) * (CGROUP_WEIGHT_DEFAULT - CGROUP_WEIGHT_MIN ) / (CGROUP_BFQ_WEIGHT_DEFAULT - CGROUP_BFQ_WEIGHT_MIN ) : CGROUP_WEIGHT_DEFAULT + (bfq_weight - CGROUP_BFQ_WEIGHT_DEFAULT ) * (CGROUP_WEIGHT_MAX - CGROUP_WEIGHT_DEFAULT ) / (CGROUP_BFQ_WEIGHT_MAX - CGROUP_BFQ_WEIGHT_DEFAULT );
836+ }
837+
838+ static inline int
839+ get_io_weight (runtime_spec_schema_config_linux_resources * resources , uint64_t * weight , libcrun_error_t * err )
840+ {
841+ int found ;
842+
843+ if (resources -> block_io && resources -> block_io -> weight_present )
844+ {
845+ * weight = IO_WEIGHT (resources -> block_io -> weight );
846+ return 1 ;
847+ }
848+
849+ found = get_value_from_unified_map (resources , "io.bfq.weight" , weight , err );
850+ if (found )
851+ {
852+ if (found > 0 )
853+ * weight = IO_WEIGHT (* weight );
854+ return found ;
855+ }
856+
857+ /* If io.weight was provided, then it is expected to already be
858+ in the range [1, 10000] so IO_WEIGHT() is not needed. */
859+ return get_value_from_unified_map (resources , "io.weight" , weight , err );
860+ }
861+
823862/* Adapted from systemd. */
824863static int
825864bus_append_byte_array (sd_bus_message * m , const char * field , const void * buf , size_t n , libcrun_error_t * err )
@@ -904,10 +943,19 @@ append_resources (sd_bus_message *m,
904943 {
905944 uint64_t weight ;
906945
907- ret = get_weight (resources , & weight , err );
946+ ret = get_io_weight (resources , & weight , err );
908947 if (UNLIKELY (ret < 0 ))
909948 return ret ;
949+ if (ret )
950+ {
951+ sd_err = sd_bus_message_append (m , "(sv)" , "IOWeight" , "t" , weight );
952+ if (UNLIKELY (sd_err < 0 ))
953+ return crun_make_error (err , - sd_err , "sd-bus message append IOWeight" );
954+ }
910955
956+ ret = get_cpu_weight (resources , & weight , err );
957+ if (UNLIKELY (ret < 0 ))
958+ return ret ;
911959 if (ret )
912960 {
913961 sd_err = sd_bus_message_append (m , "(sv)" , "CPUWeight" , "t" , weight );
0 commit comments