VirtualBox

Ignore:
Timestamp:
Jun 20, 2012 11:46:31 AM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
78670
Message:

Main,VBoxManage,docs: bandwidth units changed to bytes (#5582)

Location:
trunk/src/VBox/Frontends/VBoxManage
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageBandwidthControl.cpp

    r40541 r41842  
    4444
    4545/**
     46 * Parses a string in the following format "n[k|m|g|K|M|G]". Stores the value
     47 * of n expressed in bytes to *pLimit. k meas kilobit, while K means kilobyte.
     48 *
     49 * @returns Error message or NULL if successful.
     50 * @param   pcszLimit       The string to parse.
     51 * @param   pLimit          Where to store the result.
     52 */
     53static const char *parseLimit(const char *pcszLimit, LONG64 *pLimit)
     54{
     55    int iMultiplier = _1M;
     56    char *pszNext = NULL;
     57    int rc = RTStrToInt64Ex(pcszLimit, &pszNext, 10, pLimit);
     58
     59    switch (rc)
     60    {
     61        case VINF_SUCCESS:
     62            break;
     63        case VWRN_NUMBER_TOO_BIG:
     64            return "Limit is too big\n";
     65        case VWRN_TRAILING_CHARS:
     66            switch (*pszNext)
     67            {
     68                case 'G': iMultiplier = _1G;       break;
     69                case 'M': iMultiplier = _1M;       break;
     70                case 'K': iMultiplier = _1K;       break;
     71                case 'g': iMultiplier = 125000000; break;
     72                case 'm': iMultiplier = 125000;    break;
     73                case 'k': iMultiplier = 125;       break;
     74                default:  return "Invalid unit suffix. Valid suffixes are: k, m, g, K, M, G\n";
     75            }
     76            break;
     77        case VWRN_TRAILING_SPACES:
     78            return "Trailing spaces in limit!\n";
     79        case VERR_NO_DIGITS:
     80            return "No digits in limit specifier\n";
     81        default:
     82            return "Invalid limit specifier\n";
     83    }
     84    if (*pLimit <= 0)
     85        return "Limit must be positive\n";
     86    if (*pLimit > INT64_MAX / iMultiplier)
     87        return "Limit is too big\n";
     88    *pLimit *= iMultiplier;
     89
     90    return NULL;
     91}
     92
     93/**
    4694 * Handles the 'bandwidthctl myvm add' sub-command.
    4795 * @returns Exit code.
     
    55103        {
    56104            { "--type",   't', RTGETOPT_REQ_STRING },
    57             { "--limit",  'l', RTGETOPT_REQ_UINT32 }
     105            { "--limit",  'l', RTGETOPT_REQ_STRING }
    58106        };
    59107
     
    61109    Bstr name(a->argv[2]);
    62110    const char *pszType  = NULL;
    63     ULONG cMaxMbPerSec   = UINT32_MAX;
     111    LONG64 cMaxBytesPerSec   = INT64_MAX;
    64112
    65113    int c;
     
    85133            case 'l': // limit
    86134            {
    87                 cMaxMbPerSec = ValueUnion.u32;
     135                if (ValueUnion.psz)
     136                {
     137                    const char *pcszError = parseLimit(ValueUnion.psz, &cMaxBytesPerSec);
     138                    if (pcszError)
     139                    {
     140                        errorArgument(pcszError);
     141                        return RTEXITCODE_FAILURE;
     142                    }
     143                }
     144                else
     145                    rc = E_FAIL;
    88146                break;
    89147            }
     
    110168    }
    111169   
    112     CHECK_ERROR2_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, cMaxMbPerSec), RTEXITCODE_FAILURE);
     170    CHECK_ERROR2_RET(bwCtrl, CreateBandwidthGroup(name.raw(), enmType, cMaxBytesPerSec), RTEXITCODE_FAILURE);
    113171
    114172    return RTEXITCODE_SUCCESS;
     
    126184    static const RTGETOPTDEF g_aBWCtlAddOptions[] =
    127185        {
    128             { "--limit",  'l', RTGETOPT_REQ_UINT32 }
     186            { "--limit",  'l', RTGETOPT_REQ_STRING }
    129187        };
    130188
    131189
    132190    Bstr name(a->argv[2]);
    133     ULONG cMaxMbPerSec   = UINT32_MAX;
     191    LONG64 cMaxBytesPerSec   = INT64_MAX;
    134192
    135193    int c;
     
    146204            case 'l': // limit
    147205            {
    148                 cMaxMbPerSec = ValueUnion.u32;
     206                if (ValueUnion.psz)
     207                {
     208                    const char *pcszError = parseLimit(ValueUnion.psz, &cMaxBytesPerSec);
     209                    if (pcszError)
     210                    {
     211                        errorArgument(pcszError);
     212                        return RTEXITCODE_FAILURE;
     213                    }
     214                }
     215                else
     216                    rc = E_FAIL;
    149217                break;
    150218            }
     
    160228
    161229   
    162     if (cMaxMbPerSec != UINT32_MAX)
     230    if (cMaxBytesPerSec != INT64_MAX)
    163231    {
    164232        ComPtr<IBandwidthGroup> bwGroup;
    165233        CHECK_ERROR2_RET(bwCtrl, GetBandwidthGroup(name.raw(), bwGroup.asOutParam()), RTEXITCODE_FAILURE);
    166234        if (SUCCEEDED(rc))
    167             CHECK_ERROR2_RET(bwGroup, COMSETTER(MaxMbPerSec)(cMaxMbPerSec), RTEXITCODE_FAILURE);
     235            CHECK_ERROR2_RET(bwGroup, COMSETTER(MaxBytesPerSec)(cMaxBytesPerSec), RTEXITCODE_FAILURE);
    168236    }
    169237
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp

    r41371 r41842  
    514514        RTStrmPrintf(pStrm,
    515515                     "VBoxManage bandwidthctl     <uuid|vmname>\n"
    516                      "                            add <name> --type <disk|network> --limit <megabytes per second> |\n"
    517                      "                            set <name> --limit <megabytes per second> |\n"
     516                     "                            add <name> --type disk|network --limit <megabytes per second>[k|m|g|K|M|G] |\n"
     517                     "                            set <name> --limit <megabytes per second>[k|m|g|K|M|G] |\n"
    518518                     "                            remove <name> |\n"
    519519                     "                            list [--machinereadable]\n"
     520                     "                            (limit units: k=kilobit, m=megabit, g=gigabit, K=kilobyte, M=megabyte, G=gigabyte)\n"
    520521                     "\n");
    521522
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageInfo.cpp

    r41584 r41842  
    260260    {
    261261        Bstr strName;
    262         ULONG cMaxMbPerSec;
     262        LONG64 cMaxBytesPerSec;
    263263        BandwidthGroupType_T enmType;
    264264
    265265        CHECK_ERROR_RET(bwGroups[i], COMGETTER(Name)(strName.asOutParam()), rc);
    266266        CHECK_ERROR_RET(bwGroups[i], COMGETTER(Type)(&enmType), rc);
    267         CHECK_ERROR_RET(bwGroups[i], COMGETTER(MaxMbPerSec)(&cMaxMbPerSec), rc);
     267        CHECK_ERROR_RET(bwGroups[i], COMGETTER(MaxBytesPerSec)(&cMaxBytesPerSec), rc);
    268268
    269269        const char *pszType = bwGroupTypeToString(enmType);
    270270        if (details == VMINFO_MACHINEREADABLE)
    271             RTPrintf("BandwidthGroup%zu=%ls,%s,%d\n", i, strName.raw(), pszType, cMaxMbPerSec);
     271            RTPrintf("BandwidthGroup%zu=%ls,%s,%lld\n", i, strName.raw(), pszType, cMaxBytesPerSec);
    272272        else
    273             RTPrintf("Name: '%ls', Type: %s, Limit: %d Mbytes/sec\n", strName.raw(), pszType, cMaxMbPerSec);
     273        {
     274            const char *pszUnits = "";
     275            LONG64 cBytes = cMaxBytesPerSec;
     276            if (!(cBytes % _1G))
     277            {
     278                pszUnits = "G";
     279                cBytes /= _1G;
     280            }
     281            else if (!(cBytes % _1M))
     282            {
     283                pszUnits = "M";
     284                cBytes /= _1M;
     285            }
     286            else if (!(cBytes % _1K))
     287            {
     288                pszUnits = "K";
     289                cBytes /= _1K;
     290            }
     291            const char *pszNetUnits = NULL;
     292            if (enmType == BandwidthGroupType_Network)
     293            {
     294                /*
     295                 * We want to report network rate limit in bits/s, not bytes.
     296                 * Only if it cannot be express it in kilobits we will fall
     297                 * back to reporting it in bytes.
     298                 */
     299                LONG64 cBits = cMaxBytesPerSec;
     300                if (!(cBits % 125))
     301                {
     302                    cBits /= 125;
     303                    pszNetUnits = "k";
     304                    if (!(cBits % 1000000))
     305                    {
     306                        cBits /= 1000000;
     307                        pszNetUnits = "g";
     308                    }
     309                    else if (!(cBits % 1000))
     310                    {
     311                        cBits /= 1000;
     312                        pszNetUnits = "m";
     313                    }
     314                    RTPrintf("Name: '%ls', Type: %s, Limit: %lld %sbits/sec (%lld %sbytes/sec)\n", strName.raw(), pszType, cBits, pszNetUnits, cBytes, pszUnits);
     315                }
     316            }
     317            if (!pszNetUnits)
     318                RTPrintf("Name: '%ls', Type: %s, Limit: %lld %sbytes/sec\n", strName.raw(), pszType, cBytes, pszUnits);
     319        }
    274320    }
    275321    if (details != VMINFO_MACHINEREADABLE)
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette