Click here to Skip to main content
15,867,568 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Uhhh... Pin
Greg Utas7-May-21 14:41
professionalGreg Utas7-May-21 14:41 
GeneralRe: Uhhh... Pin
Gary Wheeler7-May-21 15:01
Gary Wheeler7-May-21 15:01 
GeneralThere are times when I really wonder exactly what I was thinking ... Pin
OriginalGriff29-Mar-21 23:26
mveOriginalGriff29-Mar-21 23:26 
GeneralRe: There are times when I really wonder exactly what I was thinking ... Pin
Marc Clifton6-Apr-21 14:45
mvaMarc Clifton6-Apr-21 14:45 
GeneralRe: There are times when I really wonder exactly what I was thinking ... Pin
Sander Rossel11-Apr-21 0:32
professionalSander Rossel11-Apr-21 0:32 
GeneralRe: There are times when I really wonder exactly what I was thinking ... Pin
Bernhard Hiller6-Apr-21 20:52
Bernhard Hiller6-Apr-21 20:52 
GeneralRe: There are times when I really wonder exactly what I was thinking ... Pin
Dan Neely20-Apr-21 5:39
Dan Neely20-Apr-21 5:39 
GeneralC++ & Thinking about SSD & wear level Pin
raddevus27-Mar-21 11:58
mvaraddevus27-Mar-21 11:58 
I was thinking about the SSD in my wife's laptop. 120GB and it's been running for 4-5 years.

Kind of fell down a rabbit hole...

A Windows App?: Not really
I started looking for apps to tell me the situation.

Found an article that explains some things:
Find out how much longer your SSD will last - CNET[^]

The article mentions this app: Crystal Disk Mark -- not great[^]

All Comes Down to Reads & Writes
Basically the article tells you that most likely it will take you 15 years to wear a good SSD out so don't worry too much.

Found Some Sample Code
Then I found a sample program that makes some API calls and tells you how many bytes you've read and written since the OS was started.

Here's the output for my wife's computer that has been running for over 16 days without rebooting:
http://i.stack.imgur.com/3jTsD.png[^]

Compare those read/write bytes to mine that has only been running[^] for a little over 2 hours.

Here's the code (alterations by me) from c++ - How to get global Windows I/O statistics? - Stack Overflow[^]

NOTE: hard-coded to check only the C:\ drive
Update: Updated method to display the number of days computer has been running.
C++
#include <windows.h>
#include <iostream>
#include <locale>
#include <sysinfoapi.h>

std::__cxx11::string displayValue(LONGLONG);
void getOsRunTime();

int main() { 
    HANDLE dev = CreateFile(LPCSTR("\\\\.\\C:"), 
        FILE_READ_ATTRIBUTES, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, 
        OPEN_EXISTING, 
        0, 
        NULL);

    DISK_PERFORMANCE disk_info { };
    DWORD bytes;

    if (dev == INVALID_HANDLE_VALUE) {
        std::cerr << "Error opening disk\n";
        return 1;
    }

    if (!DeviceIoControl(dev, 
            IOCTL_DISK_PERFORMANCE, 
            NULL, 
            0, 
            &disk_info, 
            sizeof(disk_info), 
            &bytes, 
            NULL))
    {
        std::cerr << "Failure in DeviceIoControl\n";
        return 1;
    }
    //std::cout << (disk_info.BytesRead.QuadPart).Type() << std::endl;
    //displayValue(disk_info.BytesRead.QuadPart );
    //std::cout.imbue(std::locale(""));
    //std::cout.imbue(std::locale::global(std::locale("")));
    std::cout << "Bytes read: " << displayValue(disk_info.BytesRead.QuadPart) << "\n";
    std::cout << "Bytes written: " << displayValue(disk_info.BytesWritten.QuadPart) << "\n";
    getOsRunTime();
}

std::__cxx11::string displayValue(LONGLONG byteCount){
   auto s = std::to_string(byteCount);
   int n = s.length() - 3;
   while (n > 0) {
      s.insert(n, ",");
      n -= 3;
   }
   return s;
}

void getOsRunTime(){
    ULONGLONG milli = GetTickCount64();
    //3600000 milliseconds in an hour
    long days = milli / (3600000 *24);
    milli = milli - (3600000 *24) * days;
    long hr = milli / 3600000;
    milli = milli - 3600000 * hr;
    //60000 milliseconds in a minute
    long min = milli / 60000;
    milli = milli - 60000 * min;

    //1000 milliseconds in a second
    long sec = milli / 1000;
    milli = milli - 1000 * sec;
    std::cout << "OS has been running " << days << " days " << hr << " hours " << min << " minutes " << sec << " seconds " << milli << " ms." << std::endl;
}


modified 27-Mar-21 23:50pm.

GeneralRe: C++ & Thinking about SSD & wear level (updated) Pin
Peter_in_278027-Mar-21 16:03
professionalPeter_in_278027-Mar-21 16:03 
GeneralRe: C++ & Thinking about SSD & wear level (updated) Pin
raddevus27-Mar-21 17:47
mvaraddevus27-Mar-21 17:47 
GeneralRe: C++ & Thinking about SSD & wear level (updated) Pin
David O'Neil28-Mar-21 3:29
professionalDavid O'Neil28-Mar-21 3:29 
GeneralRe: C++ & Thinking about SSD & wear level (updated) Pin
raddevus28-Mar-21 3:37
mvaraddevus28-Mar-21 3:37 
GeneralRe: C++ & Thinking about SSD & wear level Pin
David O'Neil28-Mar-21 4:14
professionalDavid O'Neil28-Mar-21 4:14 
GeneralRe: C++ & Thinking about SSD & wear level Pin
raddevus28-Mar-21 5:11
mvaraddevus28-Mar-21 5:11 
GeneralRe: C++ & Thinking about SSD & wear level Pin
David O'Neil28-Mar-21 11:56
professionalDavid O'Neil28-Mar-21 11:56 
GeneralRe: C++ & Thinking about SSD & wear level Pin
raddevus28-Mar-21 12:50
mvaraddevus28-Mar-21 12:50 
GeneralRe: C++ & Thinking about SSD & wear level Pin
Dan Neely20-Apr-21 5:41
Dan Neely20-Apr-21 5:41 
GeneralAre you tired of keeping up with the latest changes? Pin
Slow Eddie20-Mar-21 2:58
professionalSlow Eddie20-Mar-21 2:58 
GeneralRe: Are you tired of keeping up with the latest changes? Pin
Julian Ragan20-Mar-21 3:22
Julian Ragan20-Mar-21 3:22 
GeneralRe: Are you tired of keeping up with the latest changes? Pin
GuyThiebaut24-Mar-21 0:32
professionalGuyThiebaut24-Mar-21 0:32 
GeneralRe: Are you tired of keeping up with the latest changes? Pin
Gary R. Wheeler5-May-21 14:20
Gary R. Wheeler5-May-21 14:20 
GeneralRe: Are you tired of keeping up with the latest changes? Pin
Super Lloyd5-May-21 15:11
Super Lloyd5-May-21 15:11 
GeneralRe: Are you tired of keeping up with the latest changes? Pin
obermd18-Aug-21 3:16
obermd18-Aug-21 3:16 
GeneralAbsolutely Horrific Pin
Brisingr Aerowing18-Mar-21 7:00
professionalBrisingr Aerowing18-Mar-21 7:00 
GeneralRe: Absolutely Horrific Pin
PIEBALDconsult18-Mar-21 7:13
mvePIEBALDconsult18-Mar-21 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.