/**
       Enumeration Helper for the Solaris platform. Not all disks are available from
       MNTTAB on this platform, this it is necessary to perform some additional
       searching of the file system.
    */
    void StaticPhysicalDiskEnumeration::UpdateSolarisHelper()
    {
        // workaround for unknown FS/devices
        // try to get a list of disks from /dev/dsk
        std::vector<SCXCoreLib::SCXHandle<SCXCoreLib::SCXFileInfo> > disk_infos = m_deps->GetDevDskInfo();
        std::map< std::wstring, int > found_devices;

        // iterate through all devices 
        for ( unsigned int i = 0; i < disk_infos.size(); i++ ){
            std::wstring dev_name = disk_infos[i]->GetFullPath().GetFilename();

            dev_name = dev_name.substr(0,dev_name.find_last_not_of(L"0123456789"));

            if ( found_devices.find( dev_name ) != found_devices.end() )
                continue; // already considered

            found_devices[dev_name] = 0;

            try {
                SCXCoreLib::SCXHandle<StaticPhysicalDiskInstance> disk = GetInstance(dev_name);

                if ( disk == 0 ){
                    disk = new StaticPhysicalDiskInstance(m_deps);
                    disk->SetId(dev_name);
                    disk->m_device = disk_infos[i]->GetDirectoryPath().Get() + dev_name;
                    disk->m_online = true;
                    // NOTE: Update will throw in case if disk is removable media, so 
                    // we will skip it (no call to AddInstance)
                    disk->Update();
                    AddInstance(disk);
                   
                } else {
                    disk->Update(); // check if disk is still 'alive'
                    // if disk goes off-line, Update throws and status remains 'false'
                    disk->m_online = true;
                }
            } catch ( SCXCoreLib::SCXException& e )
            {
                //std::wcout << L"excp in dsk update: " << e.What() << endl << e.Where() << endl;
                // ignore errors, since disk may not be accessible and it's fine
            }
        }
    }
    /**
       Update all instances.

    */
    void StatisticalLogicalDiskEnumeration::UpdateInstances()
    {
        scxulong total_reads = 0;
        scxulong total_writes = 0;
#if defined(hpux)
        scxulong total_tTime = 0;
#endif
        scxulong total_rTime = 0;
        scxulong total_wTime = 0;
        scxulong total_transfers = 0;
        scxulong total_rPercent = 0;
        scxulong total_wPercent = 0;
        scxulong total_tPercent = 0;
        SCXCoreLib::SCXHandle<StatisticalLogicalDiskInstance> total = GetTotalInstance();
        if (0 != total)
        {
            total->Reset();
            total->m_online = true;
        }
        
        for (EntityIterator iter = Begin(); iter != End(); iter++)
        {
            SCXCoreLib::SCXHandle<StatisticalLogicalDiskInstance> disk = *iter;
            disk->Update();
            if (0 != total)
            {
                total->m_readsPerSec += disk->m_readsPerSec;
                total->m_writesPerSec += disk->m_writesPerSec;
                total->m_transfersPerSec += disk->m_transfersPerSec;
                total->m_rBytesPerSec += disk->m_rBytesPerSec;
                total->m_wBytesPerSec += disk->m_wBytesPerSec;
                total->m_tBytesPerSec += disk->m_tBytesPerSec;
                total->m_rTime += disk->m_rTime;
                total->m_wTime += disk->m_wTime;
                total->m_tTime += disk->m_tTime;
                total->m_runTime += disk->m_runTime;
                total->m_waitTime += disk->m_waitTime;
                total->m_mbUsed += disk->m_mbUsed;
                total->m_mbFree += disk->m_mbFree;
                total_reads += disk->m_reads.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_writes += disk->m_writes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#if defined (hpux)
                total_transfers += disk->m_transfers.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_tTime += disk->m_tTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#elif defined (linux)
                total_transfers += disk->m_reads.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES) + disk->m_writes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_rTime += disk->m_rTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_wTime += disk->m_wTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#elif defined (sun)
                total_transfers += disk->m_reads.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES) + disk->m_writes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_rTime += disk->m_runTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_wTime += disk->m_waitTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#endif
                total_rPercent += disk->m_rPercentage;
                total_wPercent += disk->m_wPercentage;
                total_tPercent += disk->m_tPercentage;
            }
        }

        if (0 != total)
        {
            if (Size() > 0)
            {
                total->m_rPercentage = total_rPercent/Size();
                total->m_wPercentage = total_wPercent/Size();
                total->m_tPercentage = total_tPercent/Size();
            }
                    
            if (total_reads != 0)
            {
                total->m_secPerRead = static_cast<double>(total_rTime) / static_cast<double>(total_reads) / 1000.0;
            }
            if (total_writes != 0)
            {
                total->m_secPerWrite = static_cast<double>(total_wTime) / static_cast<double>(total_writes) / 1000.0;
            }
            if (total_transfers != 0)
            {
#if defined(hpux)
                total->m_secPerTransfer = static_cast<double>(total_tTime) / static_cast<double>(total_transfers) / 1000.0;
#elif defined(linux) || defined(sun)
                total->m_secPerTransfer = static_cast<double>(total_rTime+total_wTime) / static_cast<double>(total_transfers) / 1000.0;
#endif
            }
        }
    }
    /**
       Update all instances.

    */
    void StatisticalLogicalDiskEnumeration::UpdateInstances()
    {
        scxulong total_reads = 0;
        scxulong total_writes = 0;
#if defined(hpux)
        scxulong total_tTime = 0;
#endif
        scxulong total_rTime = 0;
        scxulong total_wTime = 0;
        scxulong total_transfers = 0;
        scxulong total_rPercent = 0;
        scxulong total_wPercent = 0;
        scxulong total_tPercent = 0;

        set<wstring> diskSet;
        pair<set<wstring>::iterator,bool> Pair;

        SCXCoreLib::SCXHandle<StatisticalLogicalDiskInstance> total = GetTotalInstance();
        if (0 != total)
        {
            total->Reset();
            SCX_LOGTRACE(m_log, L"Device being set to ONLINE for TOTAL instance");
            total->m_online = true;
        }

        for (EntityIterator iter = Begin(); iter != End(); ++iter)
        {
            SCXCoreLib::SCXHandle<StatisticalLogicalDiskInstance> disk = *iter;
            disk->Update();

            Pair = diskSet.insert(disk->m_device);
            if(Pair.second == false)
                continue;

            if (0 != total)
            {
                total->m_readsPerSec += disk->m_readsPerSec;
                total->m_writesPerSec += disk->m_writesPerSec;
                total->m_transfersPerSec += disk->m_transfersPerSec;
                total->m_rBytesPerSec += disk->m_rBytesPerSec;
                total->m_wBytesPerSec += disk->m_wBytesPerSec;
                total->m_tBytesPerSec += disk->m_tBytesPerSec;
                total->m_rTime += disk->m_rTime;
                total->m_wTime += disk->m_wTime;
                total->m_tTime += disk->m_tTime;
                total->m_runTime += disk->m_runTime;
                total->m_waitTime += disk->m_waitTime;
                total->m_mbUsed += disk->m_mbUsed;
                total->m_mbFree += disk->m_mbFree;
                total_reads += disk->m_reads.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_writes += disk->m_writes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#if defined (hpux)
                total_transfers += disk->m_transfers.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_tTime += disk->m_tTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#elif defined (linux)
                total_transfers += disk->m_reads.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES) + disk->m_writes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_rTime += disk->m_rTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_wTime += disk->m_wTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#elif defined (sun)
                total_transfers += disk->m_reads.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES) + disk->m_writes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_rTime += disk->m_runTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
                total_wTime += disk->m_waitTimes.GetDelta(MAX_DISKINSTANCE_DATASAMPER_SAMPLES);
#endif
                total_rPercent += disk->m_rPercentage;
                total_wPercent += disk->m_wPercentage;
                total_tPercent += disk->m_tPercentage;
            }
        }

        if (0 != total)
        {
            if (Size() > 0)
            {
                total->m_rPercentage = total_rPercent/Size();
                total->m_wPercentage = total_wPercent/Size();
                total->m_tPercentage = total_tPercent/Size();
            }

            if (total_reads != 0)
            {
                total->m_secPerRead = static_cast<double>(total_rTime) / static_cast<double>(total_reads) / 1000.0;
            }
            if (total_writes != 0)
            {
                total->m_secPerWrite = static_cast<double>(total_wTime) / static_cast<double>(total_writes) / 1000.0;
            }
            if (total_transfers != 0)
            {
#if defined(hpux)
                total->m_secPerTransfer = static_cast<double>(total_tTime) / static_cast<double>(total_transfers) / 1000.0;
#elif defined(linux) || defined(sun)
                total->m_secPerTransfer = static_cast<double>(total_rTime+total_wTime) / static_cast<double>(total_transfers) / 1000.0;
#endif
            }
        }
    }